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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import sys
def build_structure(): N,m = input().split() N,m = int(N)//10,int(m) values,utility = [[0 for _ in range(3)] for s in range(0,m+1)],\ [[0 for _ in range(3)] for s in range(0,m+1)] i = 1 for line in sys.stdin: a = line.split() v,p,q = int(a[0])//10,int(a[1]),int(a[2]) if q != 0: if values[q][1]==0: values[q][1] = v utility[q][1] = p*v else: values[q][2] = v utility[q][2] = p*v else: values[i][0] = v utility[i][0] = p*v i += 1 return values,utility,N,m
def init_d(): d = [ [ 0 for b in range(32001) ] for a in range(61)] return d
def bag_0_1(d,values,utility,N,m): for i in range(1,m+1): for j in range(1,N+1): d[i][j] = d[i-1][j] if j >= values[i][0]: d[i][j] = max(d[i][j],d[i-1][j-values[i][0]] + utility[i][0] ) if j >= values[i][0]+values[i][1]: d[i][j] = max(d[i][j],d[i-1][j-values[i][0]-values[i][1]] + utility[i][0]+utility[i][1]) if j >= values[i][0]+values[i][2]: d[i][j] = max(d[i][j],d[i-1][j-values[i][0]-values[i][2]] + utility[i][0]+utility[i][2]) if j >= values[i][0]+values[i][1]+values[i][2]: d[i][j] = max(d[i][j],d[i-1][j-values[i][0]-values[i][1]-values[i][2]] + utility[i][0]+utility[i][1]+utility[i][2] ) return d[i][j]
if __name__=='__main__': d = init_d() values,utility,N,m = build_structure() result = bag_0_1(d,values,utility,N,m)*10 print(result)
|