-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpartition3.py
More file actions
28 lines (23 loc) · 729 Bytes
/
partition3.py
File metadata and controls
28 lines (23 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Uses python3
import sys
import itertools
def partition3(A):
W = 0 if sum(A)%3!=0 else sum(A)//3
if len(A)<3 or W==0:
return 0
else:
count = 0
weights = [[0]*(len(A)+1) for _ in range(W+1)]
for i in range(1,W+1):
for j in range(1,len(A)+1):
weights[i][j] = weights[i][j-1]
if A[j-1]<=i:
temp = weights[i-A[j-1]][j-1] + A[j-1]
if temp>weights[i][j] : weights[i][j] = temp
if weights[i][j] == W: count+=1
if count<3: return 0
else: return 1
if __name__ == '__main__':
input = sys.stdin.read()
n, *A = list(map(int, input.split()))
print(partition3(A))