-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha57_m3_tile.py
82 lines (67 loc) · 1.46 KB
/
a57_m3_tile.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def T(tile,n,dif,M):
if n < 0:
if dif!=0:
return 9999999
return 0
if dif > 0:
if (tuple(sorted(tile)) , n) not in M:
mini = 99999999
i = tile[n]
temp = tile[n]
while(True):
if i*i - temp*temp > dif:
break
tile[n] = i
newMini = T(tile,n-1,dif - (i*i - temp*temp),M ) + (temp-i)*(temp-i)
mini = min(mini,newMini)
i+=1
tile[n] = temp
M[(tuple(sorted(tile)) , n)] = mini
return M[(tuple(sorted(tile)) , n)]
elif dif < 0:
if (tuple(sorted(tile)) , n) not in M:
mini = 99999999
temp = tile[n]
for i in range( tile[n],0,-1 ):
if i*i - temp*temp < dif:
break
tile[n] = i
newMini = T(tile,n-1,dif - (i*i - temp*temp),M ) + (temp-i)*(temp-i)
mini = min(mini,newMini)
tile[n] = temp
M[(tuple(sorted(tile)) , n)] = mini
return M[(tuple(sorted(tile)) , n)]
else:
return 0
n,m = [int(i) for i in input().split()]
tile = []
s = 0
for i in range(n):
inp = int(input())
s += inp*inp
tile.append(inp)
dif = m-s
tile.sort()
M = dict()
re = T(tile,n-1,dif,M)
print(M)
if re == 9999999:
print(-1)
else:
print(re)
'''
3 6
3
3
1
3 30
5
6
7
5 5
1
2
3
4
5
'''