There is an extreme situation where your search is searched to the end. That is, the stack of the system will search for functions according to100 *100 =10000. Pascal only gives users 16MB of stack space. You have 8 longint per search, and the space of the search itself will explode. So there are two solutions:
1: Reduce the number of variables or change them to the smallest possible type, such as shortint, byte and integer.
2. Topology first, then dynamic planning.