I have a python code which solves the 3-Partition problem using Dynamic Programming method. But I don't really understand how he utilizes the DP method. Could anyone tell me what is the recursion formula in this code and why it works? (Especially the middle loop part)
import sys
def partition3(A):
n = len(A)
if sum(A)%3 != 0:
return 0
W = sum(A)//3
value = [[[0 for x in range(W+1)] for y in range(W+1)] for z in range(len(A)+1)]
#print("dimension,n:",len(value[0][0]), len(A))
value[0][0][0] = 1
for idx in range(len(A)+1):
value[idx][0][0] = 1
for i in range(1, n+1):
for j in range(W+1):
for k in range(W+1):
value[i][j][k] = value[i-1][j][k]
if (j >= A[i-1] and value[i-1][j-A[i-1]][k]):
value[i][j][k] = 1
if (k >= A[i-1] and value[i-1][j][k-A[i-1]]):
value[i][j][k] = 1
for idx in range(n+1):
print(value[idx][0][:])
if(value[len(A)][W][W] == 1):
return 1
else:
return 0
if __name__ == '__main__':
input = sys.stdin.read()
n, *A = list(map(int, input.split()))
print(partition3(A))