Hello, Kodt, you wrote: Hello, kov_serg, you wrote: And it is possible with explanations? _>> https://ideone.com/GTaAms I here see - amateur implementation bisect.bisect inside next, two times - amateur implementation of the generator and itertools.islice (it take) inside combine - a choice from 4 variants, and, for some reason, with rollbacks on 1 position back (why on 1 position?) Here that becomes? def creep (A, B, i, j): S=A [i] +B [j] s0=best (next (S, A, B, i, j-1), next (S, A, B, i, j+1)) s1=best (next (S, B, A, j, i-1), next (S, B, A, j, i+1)) res = best (s0, (s1 [0], s1 [2], s1 [1], s1 [3])) return res Here it is searched the least following candidate for total S' = A [i '] +B [j ']> S=A [i] +B [j], S'-> min 1 2 3 4 5 6 7 8 9 [A] 1 2 3 4 - 6 7 - - A 2 - - - - - - - / - 3 - - - - - - / - - 4 5 6 7 - 9 (A) - - * 5 - - - - / - - - - 6 7 8 9 / B D - - F 7 - - / - - - - - - 8 - / - - - - - - - 9 A/B a C - E F - - 12 [B] A = [1,2,3,5,6,9] B = [1,4,6,9] In grid A [x] +B [y] the following Choice is based on the least following total A [x] +B [y]/A+B=const / / (1) / | 1-------- + | / | (3) - to the right, (4) - downwards and under a diagonal (the equal total) but to them it is necessary to creep. Search is replaced with a dichotomy.