description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
ticket = [int(x) for x in input()] fst = ticket[:3] sec = ticket[3:] diff = abs(sum(fst) - sum(sec)) if sum(fst) > sum(sec): fst, sec = sec, fst if diff == 0: print(0) elif 9 - min(fst) >= diff or max(sec) >= diff: print(1) elif ( 18 - (sum(fst) - max(fst)) >= diff or sum(sec) - min(sec) >= diff or 9 - min(fst) + max(sec) >= diff ): print(2) else: print(3)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def horror_func(s): if s[0] + s[1] + s[2] == s[3] + s[4] + s[5]: return 0 else: for i in range(6): for n in range(10): c = s.copy() c[i] = n if c[0] + c[1] + c[2] == c[3] + c[4] + c[5]: return 1 for i in range(6): for j in range(6): for n1 in range(10): for n2 in range(10): c = s.copy() c[i] = n1 c[j] = n2 if c[0] + c[1] + c[2] == c[3] + c[4] + c[5]: return 2 for i in range(6): for j in range(6): for z in range(6): for n1 in range(10): for n2 in range(10): for n3 in range(10): c = s.copy() c[i] = n1 c[j] = n2 c[z] = n3 if c[0] + c[1] + c[2] == c[3] + c[4] + c[5]: return 3 s = list(map(int, input())) print(horror_func(s))
FUNC_DEF IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
l = list(input()) for i in range(6): l[i] = int(l[i]) l1 = l[:3] l2 = l[3:] s1 = sum(l[:3]) s2 = sum(l[3:]) if s1 == s2: print(0) elif s1 > s2: x = s1 - s2 ls = [] for i in range(3): ls.append(l1[i]) ls.append(9 - l2[i]) ls.sort(reverse=True) i = 0 jud = 0 ans = 0 while jud == 0: ans += ls[i] if ans < x: i += 1 else: print(i + 1) jud = 1 else: x = s2 - s1 ls = [] for i in range(3): ls.append(l2[i]) ls.append(9 - l1[i]) ls.sort(reverse=True) i = 0 jud = 0 ans = 0 while jud == 0: ans += ls[i] if ans < x: i += 1 else: print(i + 1) jud = 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() arr = [int(i) for i in s] count = 0 while count < 4: a = arr[0] + arr[1] + arr[2] b = arr[3] + arr[4] + arr[5] r = abs(a - b) if a == b: print(count) exit() if a < b: num_min = arr.index(min(arr[0], arr[1], arr[2])) num_max = arr.index(max(arr[3], arr[4], arr[5])) else: num_min = arr.index(min(arr[3], arr[4], arr[5])) num_max = arr.index(max(arr[0], arr[1], arr[2])) if r <= arr[num_max]: print(count + 1) exit() if r <= 9 - arr[num_min]: print(count + 1) exit() if 9 - arr[num_min] > arr[num_max]: arr[num_min] = 9 else: arr[num_max] = 0 count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
n = input() a, b, c, d, e, f = map(int, n) def g(a, b, c, s): m1, m2, m3 = sorted([a, b, c]) if s == a + b + c: return 0 elif s > a + b + c: s1 = a + b + c if m2 + m3 + 9 >= s: return 1 if m3 + 18 >= s: return 2 else: return 3 else: if m1 + m2 <= s: return 1 if m1 <= s: return 2 return 3 ll = [] for s in range(28): ll.append(g(a, b, c, s) + g(d, e, f, s)) print(min(ll))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR RETURN NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
n = input() f = list(map(int, n[:3])) l = list(map(int, n[3:])) sf = sum(f) sl = sum(l) f.sort() l.sort() s = 0 k = 0 if sf > sl: d = sf - sl if l[0] + d >= 10: d -= 9 - l[0] if f[2] - d < 0: if l[1] + d >= 10: s = 3 else: s = 2 else: s = 2 else: s = 1 d = sf - sl if f[2] - d < 0: d -= f[2] if f[1] - d < 0: k = 3 else: k = 2 else: k = 1 elif sl > sf: d = sl - sf if f[0] + d >= 10: d -= 9 - f[0] if l[2] - d < 0: if f[1] + d >= 10: s = 3 else: s = 2 else: s = 2 else: s = 1 d = sl - sf if l[2] - d < 0: d -= l[2] if l[1] - d < 0: k = 3 else: k = 2 else: k = 1 print(min(k, s))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
inp = input() minr = 999999 for i in range(10): for j in range(10): for k in range(10): for l in range(10): for m in range(10): for n in range(10): if i + j + k == l + m + n: r = 0 if i != int(inp[0]): r += 1 if j != int(inp[1]): r += 1 if k != int(inp[2]): r += 1 if l != int(inp[3]): r += 1 if m != int(inp[4]): r += 1 if n != int(inp[5]): r += 1 minr = min(minr, r) print(minr)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def main(): s = input() print(solver(s)) def solver(s): L1 = [int(x) for x in s[0:3]] L2 = [int(x) for x in s[3:6]] diff = sum(L2) - sum(L1) if diff == 0: return 0 elif diff < 0: L1, L2 = L2, L1 diff = -diff changes = [(9 - L1[i]) for i in range(3)] + [(L2[i] - 0) for i in range(3)] changes.sort(reverse=True) for i in range(6): diff -= changes[i] if diff <= 0: return i + 1 main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
from itertools import product s = [int(k) for k in input()] l = set([i for i in range(10)]) cnt = 6 ck = 0 for a in l: for b in l: for c in l: for d in l: for e in l: for f in l: if a + b + c != d + e + f: continue p = [a, b, c, d, e, f] nx = len([i for i in range(6) if p[i] != s[i]]) cnt = min(cnt, nx) print(cnt)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FOR VAR VAR FOR VAR VAR FOR VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = [] for i in range(10): for j in range(10): for k in range(10): for l in range(10): for m in range(10): for n in range(10): if i + j + k == l + m + n: a.append( str(i) + str(j) + str(k) + str(l) + str(m) + str(n) ) b = input() d = [] for i in range(len(a)): c = 0 for j in range(6): if a[i][j] != b[j]: c += 1 d.append(c) print(min(d))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
numbers = input() numbers = [n for n in map(lambda n: int(n), numbers)] def check_lucky(list): return sum(list[:3]) == sum(list[3:]) if check_lucky(numbers): print(0) exit() def incDigit(x): x += 1 if x > 9: x = 0 return x minReplaced = 7 replaced = 0 index = 0 def checkForDigit(index, list, replaced): global minReplaced replaced += 1 for x in range(10): if x != numbers[index]: list[index] = x if check_lucky(list): minReplaced = min(replaced, minReplaced) for i in range(6): newNumbers = [x for x in numbers] checkForDigit(i, newNumbers, 0) if minReplaced == 1: print(1) exit() for i in range(6): newNumbers = [x for x in numbers] for t in range(10): if t != numbers[i]: newNumbers[i] = t for z in range(i, 6, 1): newNumbers2 = [y for y in newNumbers] checkForDigit(z, newNumbers2, 1) if minReplaced == 2: print(2) exit() print(3)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() l = list() l1 = list(map(int, s[:3])) l2 = list(map(int, s[3:])) if sum(l1) > sum(l2): l1, l2 = l2, l1 suml, sumr = sum(l1), sum(l2) for i, j in zip(l1, l2): l.append(9 - i) l.append(j) l.sort(reverse=True) ans = 0 for i in l: if suml >= sumr: break suml += i ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
m = list(map(int, input())) ans = 0 if sum(m[:3]) > sum(m[3:]): m = m[3:] + m[:3] while sum(m[:3]) != sum(m[3:]): ans += 1 minsum1 = 10 maxsum2 = 0 mini = -1 maxi = -1 for i in range(3): if m[i] < minsum1: minsum1 = m[i] mini = i for i in range(3, 6): if m[i] > maxsum2: maxsum2 = m[i] maxi = i var1 = m[0:6] var1[mini] = sum(var1[3:]) - sum(var1[:3]) + minsum1 if var1[mini] > 9: var1[mini] = 9 var2 = m[0:6] var2[maxi] = maxsum2 - (sum(var2[3:]) - sum(var2[:3])) if var2[maxi] < 0: var2[maxi] = 0 if sum(var1[3:]) - sum(var1[:3]) < sum(var2[3:]) - sum(var2[:3]): m = var1 else: m = var2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
S = input() a = S[:3] b = S[3:] def Sum(st): n = 0 for x in st: n += int(x) return n def g(s, t): diff = int(t[0]) + int(t[1]) + int(t[2]) - int(s[0]) - int(s[1]) - int(s[2]) ma = 9 - int(s[0]) c = 0 if 9 - int(s[1]) > ma: c = 1 ma = 9 - int(s[c]) if 9 - int(s[2]) > ma: c = 2 ma = 9 - int(s[c]) mm = int(t[0]) c2 = 0 if int(t[1]) > mm: mm = int(t[1]) c2 = 1 if int(t[2]) > mm: mm = int(t[2]) c2 = 2 if ma > mm: C = str(int(s[c]) + min(diff, ma)) s = s[:c] + C + s[c + 1 :] else: C = str(int(t[c2]) - min(diff, mm)) t = t[:c2] + C + t[c2 + 1 :] return s, t def f(): nonlocal a, b if Sum(a) == Sum(b): print(0) return if Sum(a) < Sum(b): n = 0 while Sum(a) != Sum(b): a, b = g(a, b) n += 1 print(n) return n = 0 while Sum(a) != Sum(b): b, a = g(b, a) n += 1 print(n) f()
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
l = list(input()) a = list(map(int, l)) l1 = a[0:3] l2 = a[3:6] if sum(l1) > sum(l2): b = l2 l2 = l1 l1 = b l1.sort() l2.sort() d = sum(l2) - sum(l1) if d == 0: print("0") else: i = 0 j = 2 c = 0 while d > 0: if l1 == []: d -= l2[j] l2.remove(l2[j]) j -= 1 c += 1 elif l2 == []: d -= 9 - l1[0] l1.remove(l[0]) c += 1 elif 9 - l1[0] >= l2[j]: d -= 9 - l1[i] l1.remove(l1[i]) c += 1 else: d -= l2[j] l2.remove(l2[j]) j -= 1 c += 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR LIST VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
inp = input() li = [] for i in inp: li.append(int(i)) sum1 = sum(li[:3]) sum2 = sum(li[3:]) if sum2 > sum1: for i in range(3): li[i] = 9 - li[i] for i in range(3, 6): li[i] = li[i] else: for i in range(3): li[i] = li[i] for i in range(3, 6): li[i] = 9 - li[i] li.sort(reverse=True) dif = max(sum1, sum2) - min(sum1, sum2) cur = 0 for i in range(6): if cur >= dif: print(i) exit(0) cur += li[i]
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() a = list(s[:3]) b = list(s[3:6]) sa = int(a[0]) + int(a[1]) + int(a[2]) sb = int(b[0]) + int(b[1]) + int(b[2]) if sa == sb: print(0) exit() if sa > sb: c = a a = b b = c r = abs(sa - sb) ans = 1 for i in range(2): mina = min(int(a[0]), int(a[1]), int(a[2])) maxb = max(int(b[0]), int(b[1]), int(b[2])) mina = 9 - mina if mina >= r or maxb >= r: print(ans) exit() else: ans += 1 r -= max(mina, maxb) if mina >= maxb: ch = 1 else: ch = 0 mina = 9 - mina if ch: if int(a[0]) == mina: a[0] = 9 elif int(a[1]) == mina: a[1] = 9 else: a[2] = 9 elif int(b[0]) == maxb: b[0] = 0 elif int(b[1]) == maxb: b[1] = 0 else: b[2] = 0 print(3)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
import sys def debug(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) a = [int(ch) for ch in input()] assert len(a) == 6 b1 = a[0:3] b2 = a[3:6] if sum(b1) > sum(b2): b1, b2 = b2, b1 diff = sum(b2) - sum(b1) deltas = sorted([(9 - x) for x in b1 if x < 9] + [x for x in b2 if x > 0], reverse=True) cum_deltas = [0] + deltas[:] for i in range(1, len(cum_deltas)): cum_deltas[i] += cum_deltas[i - 1] for i, x in enumerate(cum_deltas): if cum_deltas[i] >= diff: break print(i)
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
entrada = list(map(int, input())) l = entrada[:3] r = entrada[3:] sl = l[0] + l[1] + l[2] sr = r[0] + r[1] + r[2] l.sort() r.sort() if sl == sr: print(0) elif sl < sr: dif = abs(sl - sr) if dif <= r[2] or dif + l[0] <= 9: print(1) elif 9 - l[0] == r[2]: dif -= r[2] if dif <= r[1] or dif + l[0] <= 9: print(2) else: print(3) elif 9 - l[0] < r[2]: dif -= r[2] if dif <= r[1] or dif + l[0] <= 9: print(2) else: print(3) elif 9 - l[0] > r[2]: dif -= 9 - l[0] if dif <= r[2] or dif + l[1] <= 9: print(2) else: print(3) else: l, r = r, l dif = abs(sl - sr) if dif <= r[2] or dif + l[0] <= 9: print(1) elif 9 - l[0] == r[2]: dif -= r[2] if dif <= r[1] or dif + l[0] <= 9: print(2) else: print(3) elif 9 - l[0] < r[2]: dif -= r[2] if dif <= r[1] or dif + l[0] <= 9: print(2) else: print(3) elif 9 - l[0] > r[2]: dif -= 9 - l[0] if dif <= r[2] or dif + l[1] <= 9: print(2) else: print(3)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def digitsum(x): s = 0 while x > 0: s += x % 10 x //= 10 return s def main(): ds = [digitsum(x) for x in range(1000)] n = input() ans = 6 for i in range(1000000): if ds[i // 1000] == ds[i % 1000]: s = "{:06d}".format(i) diff = sum(1 for i, c in enumerate(n) if c != s[i]) ans = min(ans, diff) print(ans) main()
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = [int(q) for q in list(input())] b = a[0:3] c = a[3:6] min = sum(b) max = sum(c) t = 5 if min > max: min, max = max, min b, c = c, b if min == max: print(0) else: b.sort() c.sort() if min in c and min != 0 and max - min > 9 - b[0]: if 0 in c or max - min <= 9 - b[0] or max - min <= c[2]: print(1) else: print(2) elif max - min <= 9 - b[0] or max - min <= c[2]: print(1) elif max - min - (9 - b[0]) <= 9 - b[1] or max - min - c[2] <= c[1]: print(2) elif 9 - b[0] + c[2] >= max - min: print(2) else: print(3)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER IF NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = [int(i) for i in input()] if sum(a[3:]) > sum(a[:3]): a[:3], a[3:] = a[3:], a[:3] a[:3] = sorted(a[:3], reverse=True) a[3:] = sorted(a[3:], reverse=True) ans = 0 i = 0 j = 5 while sum(a[:3]) > sum(a[3:]): ans += 1 if a[i] > 9 - a[j]: a[i] = 0 i += 1 else: a[j] = 9 j -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def count_min(a, b): count = 0 while sum(a) < sum(b): if 9 - min(a) > max(b): a.remove(min(a)) a.append(9) else: b.remove(max(b)) b.append(0) count += 1 return count a = input() b, c = [], [] for x in a[:3]: b.append(int(x)) for x in a[3:]: c.append(int(x)) if sum(b) > sum(c): a_min = c a_max = b else: a_min = b a_max = c print(count_min(a_min, a_max))
FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() a = list(map(int, s[:3])) b = list(map(int, s[3:])) al = sum(a) bl = sum(b) dif = al - bl cnt = 0 while dif < 0: cnt += 1 if 9 - min(a) > max(b): dif += min(-dif, 9 - min(a)) a[a.index(min(a))] = 9 else: dif += min(-dif, max(b)) b[b.index(max(b))] = 0 c = b[:] b = a[:] a = c[:] while dif > 0: cnt += 1 if 9 - min(a) > max(b): dif -= min(dif, 9 - min(a)) a[a.index(min(a))] = 9 else: dif -= min(dif, max(b)) b[b.index(max(b))] = 0 print(cnt)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
number = input() num1 = number[0:3] num2 = number[3:6] sum1 = 0 sum2 = 0 maxnum1 = int(num1[0]) minnum1 = int(num1[0]) maxnum2 = int(num2[0]) minnum2 = int(num2[0]) for i in num1: sum1 += int(i) maxnum1 = max(int(i), maxnum1) minnum1 = min(int(i), minnum1) for i in num2: sum2 += int(i) maxnum2 = max(int(i), maxnum2) minnum2 = min(int(i), minnum2) if sum1 >= sum2: diff = sum1 - sum2 if sum1 < sum2: diff = sum2 - sum1 if diff == 0: print(0) elif ( sum1 > sum2 and (maxnum1 - diff >= 0 or minnum2 + diff < 10) or sum1 < sum2 and (minnum1 + diff < 10 or maxnum2 - diff >= 0) ): print(1) elif ( sum1 > sum2 and (minnum1 > sum2 and sum1 - maxnum1 > sum2 - minnum2 + 9 and maxnum2 + 18 < sum1) or sum1 < sum2 and (minnum2 > sum1 and sum2 - maxnum2 > sum1 - minnum1 + 9 and maxnum1 + 18 < sum2) ): print(3) else: print(2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
ticket1 = input() ticket2 = ticket1[3:] ticket1 = ticket1[:3] ticket1 = list(map(int, list(ticket1))) ticket2 = list(map(int, list(ticket2))) t1 = list.copy(ticket1) t2 = list.copy(ticket2) t = list.copy(t1 + t2) ticket = list.copy(t) r = sum(ticket1) - sum(ticket2) r1, r2, k = r, r, 0 while r1 and r2 and k < 3: r1 -= max(max(t1), 9 - min(t2)) r2 += max(9 - min(ticket1), max(ticket2)) k += 1 if 9 - min(t2) > max(t1): t[t.index(min(t2))] = 9 else: t[t.index(max(t1))] = 0 if 9 - min(ticket1) > max(ticket2): ticket[ticket.index(min(ticket1))] = 9 else: ticket[ticket.index(max(ticket2))] = 0 ticket1 = ticket[:3] ticket2 = ticket[3:] t1 = t[:3] t2 = t[3:] if r1 * r2 < 0: r1, r2 = 0, 0 print(k)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
p = [int(x) for x in input()] ans = [] for a in range(10): for b in range(10): for c in range(10): for d in range(10): for e in range(10): f = a + b + c - d - e o = [a, b, c, d, e, f] delta = 0 if 0 <= f <= 9: for i in range(6): if p[i] != o[i]: delta += 1 ans.append(delta) print(min(ans))
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() m, t = 6, [[] for i in range(28)] for i in range(10): for j in range(10): for k in range(10): t[i + j + k].append(str(i) + str(j) + str(k)) for q in t: for u in q: for v in q: m = min(m, sum(x != y for x, y in zip(u + v, s))) print(m)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = input() b1, b2 = 0, 0 for i in range(3): b1 += int(a[i]) for i in range(3): b2 += int(a[3 + i]) if b1 > b2: p = 0 d = [] c = list(a[3:]) c = list(map(int, c)) for i in c: d.append(9 - i) c = list(a[:3]) c = list(map(int, c)) for i in c: d.append(i) d.sort() d.reverse() for i in range(len(d)): p += d[i] if p >= b1 - b2: print(i + 1) break elif b1 < b2: p = 0 d = [] c = list(a[:3]) c = list(map(int, c)) for i in c: d.append(9 - i) c = list(a[3:]) c = list(map(int, c)) for i in c: d.append(i) d.sort() d.reverse() for i in range(len(d)): p += d[i] if p >= b2 - b1: print(i + 1) break else: print(0)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
line = [int(x) for x in list(input())] l1 = line[0:3] l2 = line[3:] s1 = sum(l1) s2 = sum(l2) s = abs(s1 - s2) ans = 0 if s1 > s2: l2 = list(map(lambda x: 9 - x, l2)) l = l1 + l2 l.sort(reverse=True) for i in l: if s > 0: s -= i ans += 1 else: break elif s2 > s1: l1 = list(map(lambda x: 9 - x, l1)) l = l1 + l2 l.sort(reverse=True) for i in l: if s > 0: s -= i ans += 1 else: break print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
n = input() a = [] for i in n: a += [i] a = list(map(int, a)) x = a[0] + a[1] + a[2] - a[3] - a[4] - a[5] if x == 0: print(0) elif x > 0: a[3] = 9 - a[3] a[4] = 9 - a[4] a[5] = 9 - a[5] a.sort() i = 0 while x > 0: x -= a[5 - i] i += 1 print(i) else: a[0] = 9 - a[0] a[1] = 9 - a[1] a[2] = 9 - a[2] a.sort() x = -x i = 0 while x > 0: x -= a[5 - i] i += 1 print(i)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
digs = list(map(int, input())) l, r = min(digs[:3], digs[3:], key=sum), max(digs[:3], digs[3:], key=sum) ans = 0 while sum(r) - sum(l) > 0: if 9 - min(l) >= max(r): diff = 9 - min(l) l[l.index(min(l))] = 9 else: diff = max(r) r[r.index(max(r))] = 0 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() s = [int(i) for i in s] a, b = s[:3], s[3:] if sum(a) > sum(b): a, b = b, a t = sum(b) - sum(a) a = [(9 - i) for i in a] a = a + b a.sort(reverse=True) if t == 0: print(0) elif a[0] >= t: print(1) elif a[0] + a[1] >= t: print(2) else: print(3)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
inp = input() inp = str(inp) a = [0] * 6 ticket = [] for c in inp: ticket.append(int(c)) sum1 = 0 sum2 = 0 for i in range(3): sum1 += ticket[i] a[i] = ticket[i] for i in range(3, 6): sum2 += ticket[i] a[i] = ticket[i] sum3 = sum1 - sum2 if sum3 > 0: for i in range(3, 6): a[i] = 9 - a[i] else: for i in range(3): a[i] = 9 - a[i] sum3 = -sum3 a = sorted(a) printed = False for i in range(5, 1, -1): if sum3 <= 0: print(5 - i) printed = True break sum3 -= a[i] if not printed: print(0)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
n = list(input()) n = [int(n[i]) for i in range(6)] s1 = sum(n) s = sum(n[:3]) s1 -= s if s > s1: n = n[::-1] sw = [] sw.append(9 - n[0]) sw.append(9 - n[1]) sw.append(9 - n[2]) sw.append(n[-3]) sw.append(n[-2]) sw.append(n[-1]) ab = abs(s1 - s) sw.sort(reverse=True) i = 0 ans = 0 while ab > 0: ab -= sw[i] ans += 1 i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def readline(): return map(int, input().split()) def main(): number = list(map(int, input())) steps, line = 0, [number] def diff(n): return sum(n[0:3]) - sum(n[3:]) def next(n): d = diff(n) for i in range(6): if d > 0 and i < 3 or d < 0 and i >= 3: r = range(n[i]) else: r = range(n[i] + 1, 10) for digit in r: m = n.copy() m[i] = digit yield m while True: next_line = list() for number in line: if diff(number) == 0: print(steps) return next_line.extend(next(number)) steps, line = steps + 1, next_line main()
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER LIST VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = input() m = [] ans = 6 for x in a: m.append(int(x)) for d0 in range(10): for d1 in range(10): for d2 in range(10): for d3 in range(10): for d4 in range(10): d5 = d0 + d1 + d2 - d3 - d4 if 0 <= d5 <= 9: r = 0 d = [d0, d1, d2, d3, d4, d5] for i in range(6): if m[i] != d[i]: r += 1 ans = min(ans, r) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
t = list(input()) s = int(t[0]) + int(t[1]) + int(t[2]) s1 = int(t[3]) + int(t[4]) + int(t[5]) def first(s, s1, t): t = t[:3] todo = s1 - s if 9 - int(min(t, key=lambda x: int(x))) < todo: small = 9 - int(min(t[:2], key=lambda x: int(x))) t.remove(min(t, key=lambda x: int(x))) if 9 - int(min(t, key=lambda x: int(x))) + small < todo: return 3 else: return 2 else: return 1 def secend(s, s1, t): t = t[3:] todo = s - s1 if 9 - int(min(t, key=lambda x: int(x))) < todo: smaller = 9 - int(min(t, key=lambda x: int(x))) t.remove(min(t, key=lambda x: int(x))) if 9 - int(min(t, key=lambda x: int(x))) + smaller < todo: return 3 else: return 2 else: return 1 def third(s, s1, t): t = t[:3] todo = s - s1 if int(max(t, key=lambda x: int(x))) < todo: small = int(max(t, key=lambda x: int(x))) t.remove(max(t, key=lambda x: int(x))) if int(max(t, key=lambda x: int(x))) + small < todo: return 3 else: return 2 else: return 1 def fourth(s, s1, t): t = t[3:] todo = s1 - s if int(max(t, key=lambda x: int(x))) < todo: small = int(max(t, key=lambda x: int(x))) t.remove(max(t, key=lambda x: int(x))) if int(max(t, key=lambda x: int(x))) + small < todo: return 3 else: return 2 else: return 1 def fifth(s, s1, t): todo = s1 - s if ( int(max(t[3:], key=lambda x: int(x))) + (9 - int(min(t[:3], key=lambda x: int(x)))) >= todo ): return 2 else: return 3 def sixth(s, s1, t): todo = s - s1 if ( int(max(t[:3], key=lambda x: int(x))) + (9 - int(min(t[3:], key=lambda x: int(x)))) >= todo ): return 2 else: return 3 if s != s1: if s > s1: print(min(third(s, s1, t), secend(s, s1, t), sixth(s, s1, t))) else: print(min(fourth(s, s1, t), first(s, s1, t), fifth(s, s1, t))) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = [int(i) for i in input()] b = a[:3] c = a[3:] if sum(b) == sum(c): print(0) else: if sum(b) > sum(c): b, c = c[:], b[:] diff = sum(c) - sum(b) mx = [] for i in b: mx.append(9 - i) for i in c: mx.append(i) ans = 0 cur = 0 mx.sort(reverse=True) i = 0 while cur < diff: ans += 1 cur += mx[i] i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() p = s[:3] h = s[3:] l = [int(i) for i in p] k = [int(i) for i in h] q = [] if sum(k) < sum(l): q = [i for i in l] q += [(9 - i) for i in k] raz = sum(l) - sum(k) cnt = 0 while raz > 0: q = sorted(q) raz -= q[len(q) - 1] q[len(q) - 1] = 0 cnt += 1 print(cnt) elif sum(k) > sum(l): q = [i for i in k] q += [(9 - i) for i in l] raz = sum(k) - sum(l) cnt = 0 while raz > 0: q = sorted(q) raz -= q[len(q) - 1] q[len(q) - 1] = 0 cnt += 1 print(cnt) else: print(0)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = [int(x) for x in input()] a, b = s[:3], s[3:] sa, sb = sum(a), sum(b) if sa > sb: a, b = b, a r = abs(sb - sa) if r == 0: ans = 0 else: breva = b[:] for x in a: breva.append(9 - x) td = 0 breva.sort(reverse=True) for i, j in enumerate(breva): td += j if td >= r: ans = i + 1 break print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
import sys def returndiff(s1, s2, s3, s4, s5, s6): ct = 0 if s1 + s2 + s3 == s4 + s5 + s6: if s1 != n[0]: ct += 1 if s2 != n[1]: ct += 1 if s3 != n[2]: ct += 1 if s4 != n[3]: ct += 1 if s5 != n[4]: ct += 1 if s6 != n[5]: ct += 1 return ct n = list(map(int, input())) t = [] n1, n2, n3, n4, n5, n6 = n[0], n[1], n[2], n[3], n[4], n[5] if sum(n[:3]) == sum(n[3:]): print(0) sys.exit(0) for a in range(10): n1 = a if n1 + n2 + n3 == n4 + n5 + n6: t.append(returndiff(n1, n2, n3, n4, n5, n6)) for b in range(10): n2 = b if n1 + n2 + n3 == n4 + n5 + n6: t.append(returndiff(n1, n2, n3, n4, n5, n6)) for c in range(10): n3 = c if n1 + n2 + n3 == n4 + n5 + n6: t.append(returndiff(n1, n2, n3, n4, n5, n6)) for d in range(10): n4 = d if n1 + n2 + n3 == n4 + n5 + n6: t.append(returndiff(n1, n2, n3, n4, n5, n6)) for e in range(10): n5 = e if n1 + n2 + n3 == n4 + n5 + n6: t.append(returndiff(n1, n2, n3, n4, n5, n6)) for f in range(10): n6 = f if n1 + n2 + n3 == n4 + n5 + n6: t.append(returndiff(n1, n2, n3, n4, n5, n6)) print(min(t))
IMPORT FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() min_d = 6 for a in range(10): for b in range(10): for c in range(10): for d in range(10): for e in range(10): for f in range(10): if a + b + c == d + e + f: k = 0 t = str(a) + str(b) + str(c) + str(d) + str(e) + str(f) for i in range(6): if s[i] != t[i]: k += 1 min_d = min(min_d, k) print(min_d)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
ticket = input() num1 = int(ticket[0]) num2 = int(ticket[1]) num3 = int(ticket[2]) num4 = int(ticket[3]) num5 = int(ticket[4]) num6 = int(ticket[5]) minDif = 7 for l in range(1000): leftDif = 0 if l % 10 != num3: leftDif += 1 if l // 10 % 10 != num2: leftDif += 1 if l // 100 != num1: leftDif += 1 if leftDif < minDif: numSum = l % 10 + l // 10 % 10 + l // 100 i = 0 while i <= numSum and i <= 9: j = 0 while j <= numSum - i and j <= 9: k = numSum - i - j if k <= 9: rightDif = 0 if i != num4: rightDif += 1 if j != num5: rightDif += 1 if k != num6: rightDif += 1 if leftDif + rightDif < minDif: minDif = leftDif + rightDif j += 1 i += 1 print(minDif)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
import sys from itertools import product n = list(map(int, input())) ans = 6 for a in product(range(10), repeat=6): if a[0] + a[1] + a[2] == a[3] + a[4] + a[5]: x = sum(0 if n[i] == a[i] else 1 for i in range(6)) ans = min(ans, x) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() a = [0, 0, 0, 0, 0, 0] t = [] ans = 3 for i in s: t.append(ord(i) - ord("0")) for a[0] in range(10): for a[1] in range(10): for a[2] in range(10): for a[3] in range(10): for a[4] in range(10): for a[5] in range(10): anss = 6 if a[0] + a[1] + a[2] == a[3] + a[4] + a[5]: for i in range(6): if a[i] == t[i]: anss = anss - 1 if anss < ans: ans = anss print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def f(x): return sum(x[:3]) == sum(x[3:]) def main(): a = [int(i) for i in input()] if f(a): return 0 for i in range(6): for j in range(10): b = a[:] b[i] = j if f(b): return 1 for i in range(6): for j in range(i): for k in range(10): for l in range(10): b = a[:] b[i] = k b[j] = l if f(b): return 2 return 3 print(main())
FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = [int(i) for i in input()] b = sorted([(9 - a[i]) for i in range(3)]) d = sorted([(9 - a[i]) for i in range(3, 6)]) c = sum(a[:3]) - sum(a[3:]) s = 0 if c < 0: c = -c t = 2 r = 2 q = [] q += a[3:] q.sort() while c > 0: if q[r] > b[t]: c -= q[r] s += 1 r -= 1 else: c -= b[t] s += 1 t -= 1 else: t = 2 r = 2 q = [] q += a[:3] q.sort() while c > 0: if q[r] > d[t]: c -= q[r] s += 1 r -= 1 else: c -= d[t] s += 1 t -= 1 print(s)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
digits = [int(i) for i in input()] sm = digits[:3] gr = digits[3:] if sum(sm) > sum(gr): sm, gr = gr, sm iters = 0 diff = sum(gr) - sum(sm) while diff: iters += 1 bestSm = sm.index(min(sm)) bestGr = gr.index(max(gr)) if 9 - sm[bestSm] > gr[bestGr]: sm[bestSm] = 9 if sm[bestSm] + diff > 9 else sm[bestSm] + diff else: gr[bestGr] = 0 if gr[bestGr] - diff < 0 else gr[bestGr] - diff diff = sum(gr) - sum(sm) print(iters)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
i = list(map(lambda c: int(c), input())) d = sum([i[3] - i[0], i[4] - i[1], i[5] - i[2]]) if d < 0: i.reverse() d *= -1 for z in range(3): i[z] = 9 - i[z] i = sorted(i) i.reverse() ir = 0 for ind, z in enumerate(i): if ir >= d: print(ind) break ir += z
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = str(input()) r = [] for el in s: r.append(int(el)) l1 = r[0:3] l2 = r[3:6] if sum(l1) < sum(l2): l1, l2 = l2, l1 s = abs(sum(l1) - sum(l2)) if s == 0: print(0) else: usable = [] for e in l1: usable.append(e) for e in l2: usable.append(9 - e) usable.sort(reverse=True) i = 0 while s > 0: s -= usable[i] i += 1 print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() l1 = [int(s[i]) for i in range(3)] l2 = [int(s[3 + i]) for i in range(3)] s1 = sum(l1) s2 = sum(l2) if s1 < s2: l1, l2 = l2, l1 s1, s2 = s2, s1 l1.sort(reverse=True) l2.sort() ans = 0 p1 = 0 p2 = 0 while s1 > s2: if l1[p1] > 9 - l2[p2]: s1 -= l1[p1] p1 += 1 else: s2 += 9 - l2[p2] p2 += 1 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = list(input()) a = [] b = [] for i in range(0, 3): a.append(int(s[i])) for i in range(3, 6): b.append(int(s[i])) s1 = sum(a) s2 = sum(b) a.sort() b.sort() for i in range(0, 3): a.append(s1 - a[i]) for i in range(0, 3): b.append(s2 - b[i]) if s1 == s2: print(0) else: for i in range(0, 3): if s1 - b[i + 3] >= 0 and s1 - b[i + 3] <= 9: print(1) exit() for i in range(0, 3): if s2 - a[i + 3] >= 0 and s2 - a[i + 3] <= 9: print(1) exit() for i in range(0, 3): if s1 - b[i] >= 0 and s1 - b[i] <= 18: print(2) exit() for i in range(0, 3): if s2 - a[i] >= 0 and s2 - a[i] <= 18: print(2) exit() for i in range(3, 6): x = a[i] for j in range(3, 6): if abs(x - b[j]) <= 9: print(2) exit() print(3)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
A = [int(x) for x in list(input())] B = sorted(A[:3]) C = sorted(A[3:6]) b = sum(B) c = sum(C) a = b - c if a != 0: if a < 0: D = sorted([(9 - x) for x in B] + C) elif a > 0: D = sorted(B + [(9 - x) for x in C]) D.reverse() d = 0 result = 0 for x in D: d += x result += 1 if d >= abs(a): break print(result) else: print(0)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def comp(n, m): rtn = 0 for i in range(6): if n[i] != m[i]: rtn += 1 return rtn n = list(map(int, list(input()))) ans = 6 for i in range(1000000): m = [] for _ in range(6): m.append(i % 10) i //= 10 m = list(reversed(m)) if sum(m[:3]) == sum(m[3:]): ans = min(ans, comp(n, m)) print(ans)
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = input() b = list(a) c = [int(x) for x in b] d = c[:3] e = c[3:] f = [] if sum(d) == sum(e): print(0) elif sum(d) > sum(e): for i in d: f.append(i) for j in e: f.append(9 - j) f.sort() k = sum(d) - sum(e) if k > f[-1]: if k > f[-1] + f[-2]: print(3) else: print(2) else: print(1) else: for i in d: f.append(9 - i) for j in e: f.append(j) f.sort() k = sum(e) - sum(d) if k > f[-1]: if k > f[-1] + f[-2]: print(3) else: print(2) else: print(1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = list(map(int, input())) ans = 9 * 6 + 1 for a in range(10): for b in range(10): for c in range(10): for d in range(10): for e in range(10): for f in range(10): if a + b + c == d + e + f: ans = min( ans, [ a == s[0], b == s[1], c == s[2], d == s[3], e == s[4], f == s[5], ].count(False), ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
ticket = input() q1, q2 = [(int(i), j) for j, i in enumerate(ticket[:3])], [ (int(i), j) for j, i in enumerate(ticket[3:]) ] p1, p2 = [i for i, j in q1], [i for i, j in q2] if sum(p1) > sum(p2): p1, p2 = p2, p1 q1, q2 = q2, q1 if sum(p1) == sum(p2): print(0) exit() for i in range(20): if 9 - min(p1) > max(p2): pos = min(q1)[1] p1[pos] = 9 q1[pos] = 9, pos else: pos = max(q2)[1] p2[pos] = 0 q2[pos] = 0, pos if sum(p1) >= sum(p2): print(i + 1) exit()
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = list(input()) for i in range(6): a[i] = int(a[i]) a1 = a[0:3] a2 = a[3:6] a1.sort() a2.sort() ans = [] s1 = sum(a1) s2 = sum(a2) if s1 == s2: ans.append(0) ans.append(0) elif s1 > s2: if a1[1] + a1[2] <= s2 or a1[0] + a1[2] <= s2 or a1[0] + a1[1] <= s2: ans.append(1) elif a1[0] <= s2 or a1[1] <= s2 or a1[2] <= s2: ans.append(2) else: ans.append(3) if s1 <= 9 + a2[0] + a2[1] or s1 <= 9 + a2[1] + a2[2] or s1 <= 9 + a2[0] + a2[2]: ans.append(1) elif s1 <= 18 + a2[0] or s1 <= 18 + a2[1] or s1 <= 18 + a2[2]: ans.append(2) else: ans.append(3) if a1[0] + a1[1] <= s2: ans.append(1) elif a1[0] + a1[1] <= 9 + a2[1] + a2[2]: ans.append(2) else: ans.append(3) else: if a2[1] + a2[2] <= s1 or a2[0] + a2[2] <= s1 or a2[0] + a2[1] <= s1: ans.append(1) elif a2[0] <= s1 or a2[1] <= s1 or a2[2] <= s1: ans.append(2) else: ans.append(3) if s2 <= 9 + a1[0] + a1[1] or s2 <= 9 + a1[1] + a1[2] or s2 <= 9 + a1[0] + a1[2]: ans.append(1) elif s2 <= 18 + a1[0] or s2 <= 18 + a1[1] or s2 <= 18 + a1[2]: ans.append(2) else: ans.append(3) if a2[0] + a2[1] <= s1: ans.append(1) elif a2[0] + a2[1] <= 9 + a1[1] + a1[2]: ans.append(2) else: ans.append(3) print(min(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = [int(x) for x in input()] ans = 6 k = 0 for d0 in range(10): for d1 in range(10): for d2 in range(10): for d3 in range(10): for d4 in range(10): d5 = d0 + d1 + d2 - d3 - d4 if 0 <= d5 <= 9: d = [d0, d1, d2, d3, d4, d5] de = 0 for i in range(6): if s[i] != d[i]: de += 1 k += 1 ans = min(de, ans) print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR IF NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = list(map(int, input())) left_sum = sum(s[:3]) right_sum = sum(s[3:]) if left_sum >= right_sum: abc_min = s[3:] abc_max = s[:3] elif left_sum < right_sum: abc_min = s[:3] abc_max = s[3:] dif = max(left_sum, right_sum) - min(left_sum, right_sum) ans = 0 for k in range(3): if max(abc_max) > 9 - min(abc_min): dif -= max(abc_max) del abc_max[abc_max.index(max(abc_max))] else: dif -= 9 - min(abc_min) del abc_min[abc_min.index(min(abc_min))] ans += 1 if dif <= 0: break if left_sum == right_sum: print(0) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() a, b = sorted(list(map(int, s[:3]))), sorted(list(map(int, s[3:]))) sma, smb = sum(a), sum(b) if sum(a) < sum(b): mn, mx = a, b[::-1] else: mn, mx = b, a[::-1] k1, k2 = 0, 0 while sum(mn) < sum(mx): if 9 - mn[k1] >= mx[k2]: mn[k1] = 9 k1 += 1 else: mx[k2] = 0 k2 += 1 print(k1 + k2)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
n = input() a = [] b = [] for i in range(3): a.append(int(n[i])) for i in range(3, 6): b.append(int(n[i])) c = sum(a) d = sum(b) e = [] f = abs(c - d) count = 0 if c == d: print(0) elif c > d: e += a for i in range(len(b)): e.append(9 - b[i]) e.sort() e.reverse() while f > 0: f -= e[count] count += 1 print(count) else: e += b for i in range(len(a)): e.append(9 - a[i]) e.sort() e.reverse() while f > 0: f -= e[count] count += 1 print(count)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
digits = [int(x) for x in input()] difference = sum(digits[0:3]) - sum(digits[3:]) helper = [] if difference < 0: helper = [(9 - x) for x in digits[:3]] + digits[3:] else: helper = digits[:3] + [(9 - x) for x in digits[3:]] helper = sorted(helper)[::-1] n = 0 sum_ = 0 for x in helper: if sum_ >= abs(difference): break sum_ += x n += 1 print(n)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = list(str(input())) a = [int(i) for i in a] one = a[:3] two = a[3:] o = sum(one) t = sum(two) one.sort() two.sort() s = 0 c = [] if o == t: s = 0 elif o > t: b = o - t c.append(one[0]) c.append(one[1]) c.append(one[2]) c.append(9 - two[0]) c.append(9 - two[1]) c.append(9 - two[2]) c.sort() c.reverse() e = 0 for i in range(0, 6): e += c[i] if e >= b: s = i + 1 break else: b = t - o c.append(two[0]) c.append(two[1]) c.append(two[2]) c.append(9 - one[0]) c.append(9 - one[1]) c.append(9 - one[2]) c.sort() c.reverse() e = 0 for i in range(0, 6): e += c[i] if e >= b: s = i + 1 break print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() ans = 10 for x in range(10): for y in range(10): for z in range(10): for a in range(10): for b in range(10): for c in range(10): if x + y + z == a + b + c: cnt = 0 cnt += x != ord(s[0]) - ord("0") cnt += y != ord(s[1]) - ord("0") cnt += z != ord(s[2]) - ord("0") cnt += a != ord(s[3]) - ord("0") cnt += b != ord(s[4]) - ord("0") cnt += c != ord(s[5]) - ord("0") ans = min(ans, cnt) print(str(ans))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = input() ls = int(a[0]) + int(a[1]) + int(a[2]) rs = int(a[3]) + int(a[4]) + int(a[5]) dif = ls - rs if dif == 0: print(0) quit() p = [0] * 6 for i in range(6): if (i < 3) ^ (dif > 0): p[i] = 9 - int(a[i]) else: p[i] = int(a[i]) p.sort(reverse=True) ps = 0 for i in range(6): ps += p[i] if ps >= abs(dif): print(i + 1) break
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = list(map(int, input())) a_first = a[:3] a_last = a[3:] sum_a_first = sum(a_first) sum_a_last = sum(a_last) if sum_a_first != sum_a_last: a_first.sort() a_last.sort() if sum_a_first > sum_a_last: a_first, a_last = a_last, a_first sum_a_first, sum_a_last = sum_a_last, sum_a_first count_change_a_first = 1 sum_temp = sum_a_first for i in range(3): temp = a_first[i] is_break = False while temp < 9: temp += 1 sum_temp += 1 if sum_temp == sum_a_last: is_break = True break if is_break: break count_change_a_first += 1 count_change_a_last = 1 sum_temp = sum_a_last for i in range(3): temp = a_last[3 + ~i] is_break = False while temp > 0: temp -= 1 sum_temp -= 1 if sum_temp == sum_a_first: is_break = True break if is_break: break count_change_a_last += 1 sum_a_first_temp = sum_a_first sum_a_last_temp = sum_a_last first_a_first = a_first[0] last_a_last = a_last[2] index = 0 is_both = False while first_a_first < 9 or last_a_last > 0: if index % 2 == 0: if first_a_first < 9: first_a_first += 1 sum_a_first_temp += 1 else: last_a_last -= 1 sum_a_last_temp -= 1 elif last_a_last > 0: last_a_last -= 1 sum_a_last_temp -= 1 else: first_a_first += 1 sum_a_first_temp += 1 if sum_a_first_temp == sum_a_last_temp: is_both = True break index += 1 if is_both: print(min(count_change_a_first, count_change_a_last, 2)) else: print(min(count_change_a_first, count_change_a_last)) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
l = list(str(input())) l = [int(i) for i in l] l1 = l[0:3] l1 = sorted(l1) l2 = l[3:] l2 = sorted(l2) if sum(l1) > sum(l2): l1, l2 = l2, l1 if sum(l1) == sum(l2): ans = 0 else: l1[0] = 9 if sum(l1) >= sum(l2): ans = 1 else: l1[1] = 9 if sum(l1) >= sum(l2): ans = 2 else: ans = 3 l1 = l[0:3] l1 = sorted(l1) l2 = l[3:] l2 = sorted(l2) if sum(l1) > sum(l2): l1, l2 = l2, l1 if sum(l1) == sum(l2): ans = min(ans, 0) else: l2[2] = 0 if sum(l1) >= sum(l2): ans = min(ans, 1) else: l2[1] = 0 if sum(l1) >= sum(l2): ans = min(ans, 2) else: ans = min(ans, 3) l1 = l[0:3] l1 = sorted(l1) l2 = l[3:] l2 = sorted(l2) if sum(l1) > sum(l2): l1, l2 = l2, l1 if sum(l1) == sum(l2): ans = min(ans, 0) else: l2[2] = 0 if sum(l1) >= sum(l2): ans = min(ans, 1) else: l1[0] = 9 if sum(l1) >= sum(l2): ans = min(ans, 2) else: ans = min(ans, 3) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
a = input() def ass(a): ans = 0 p = list(map(int, a[:3])) q = list(map(int, a[3:])) s = sum(p) - sum(q) if s == 0: ans = 0 return ans elif s > 0: p = list(map(lambda x: 9 - x, p)) else: s = -s q = list(map(lambda x: 9 - x, q)) a = p + q a.sort() ans = 0 for i in a: if s <= 0: break s -= 9 - i ans += 1 return ans def bss(a): l = list(a) a = list(map(int, l)) l1 = a[0:3] l2 = a[3:6] if sum(l1) > sum(l2): b = l2 l2 = l1 l1 = b l1.sort() l2.sort() d = sum(l2) - sum(l1) if d == 0: return 0 else: i = 0 j = 2 c = 0 while d > 0: if l1 == []: d -= l2[j] l2.remove(l2[j]) j -= 1 c += 1 elif l2 == []: d -= 9 - l1[0] l1.remove(l[0]) c += 1 elif 9 - l1[0] >= l2[j]: d -= 9 - l1[i] l1.remove(l1[i]) c += 1 else: d -= l2[j] l2.remove(l2[j]) j -= 1 c += 1 return c print(ass(a))
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR LIST VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def main(): digits = input() li = list(digits) digits = list(map(int, li)) first = digits[:3] second = digits[3:] first.sort() second.sort() sumf = sum(first) sums = sum(second) if sumf == sums: print("0") return elif sumf < sums: small = 1 else: small = 2 if small == 1: diff = sums - sumf cnt1 = 0 for i in range(0, 3): diff = diff + first[i] if diff <= 9: cnt1 = cnt1 + 1 break else: diff = diff - 9 cnt1 = cnt1 + 1 cnt2 = 0 diff = sums - sumf for i in range(2, -1, -1): diff = diff - second[i] if diff <= 0: cnt2 = cnt2 + 1 break else: cnt2 = cnt2 + 1 result = min(cnt1, cnt2) else: cnt1 = 0 diff = sumf - sums for i in range(0, 3): diff = diff + second[i] if diff <= 9: cnt1 = cnt1 + 1 break else: diff = diff - 9 cnt1 = cnt1 + 1 cnt2 = 0 diff = sumf - sums for i in range(2, -1, -1): diff = diff - first[i] if diff <= 0: cnt2 = cnt2 + 1 break else: cnt2 = cnt2 + 1 result = min(cnt1, cnt2) temp = 9 if small == 1: for i in range(0, 3): for j in range(0, 3): diff = sums - sumf diff = diff + first[i] - 9 diff = diff - second[j] if diff <= 0: temp = 2 break else: for i in range(0, 3): for j in range(0, 3): diff = sumf - sums diff = diff + second[i] - 9 diff = diff - first[j] if diff <= 0: temp = 2 break result = min(result, temp) print(result) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
from sys import exit def strsum(s): return sum([int(c) for c in s]) def pos_diffs(s): return [(9 - int(i)) for i in s] def neg_diffs(s): return [int(i) for i in s] def reverse_nums(s): return "".join([str(9 - int(c)) for c in s]) n = input() first = n[:3] last = n[3:] if strsum(first) == strsum(last): print("0") exit(0) if strsum(first) < strsum(last): tbi = first goal = last else: tbi = last goal = first difference = strsum(goal) - strsum(tbi) current = 0 change_count = 0 diffs = sorted(pos_diffs(tbi) + neg_diffs(goal)) while current < difference: current += diffs.pop(-1) change_count += 1 print(change_count)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() ans = 6 for i in range(0, 10): for j in range(0, 10): for k in range(0, 10): for f in range(0, 10): for f1 in range(0, 10): for f2 in range(0, 10): if i + j + k == f + f1 + f2: cnt = 0 if i != ord(s[0]) - ord("0"): cnt = cnt + 1 if j != ord(s[1]) - ord("0"): cnt = cnt + 1 if k != ord(s[2]) - ord("0"): cnt = cnt + 1 if f != ord(s[3]) - ord("0"): cnt = cnt + 1 if f1 != ord(s[4]) - ord("0"): cnt = cnt + 1 if f2 != ord(s[5]) - ord("0"): cnt = cnt + 1 ans = min(ans, cnt) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def build(b): s = 0 b = b[::-1] for i in range(6): s += b[i] * 10**i return s t = [-1] * 1000000 n = int(input()) t[n] = 0 v = [n] while v != []: a = v.pop(0) x = [] while a != 0: x = [a % 10] + x a //= 10 x = [0] * (6 - len(x)) + x if sum(x[:3]) == sum(x[3:]): print(t[build(x)]) break for i in range(6): for j in range(10): s = build(x[:i] + [j] + x[i + 1 :]) if t[s] == -1: t[s] = t[build(x)] + 1 v += [s]
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR LIST VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR LIST VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
l = list(input()) a = [] b = [] for i in range(3): a.append(int(l[i])) for i in range(3, 6): b.append(int(l[i])) a.sort() b.sort() s1 = sum(a) s2 = sum(b) if s1 == s2: print(0) elif s1 < s2: diff = s2 - s1 a.sort() b.sort(reverse=True) c = [] t = 0 for i in range(3): c.append(9 - a[i]) c.append(b[i]) c.sort(reverse=True) for i in range(6): t = t + c[i] if t >= diff: break print(i + 1) else: diff = s1 - s2 t = 0 a.sort(reverse=True) b.sort() c = [] for i in range(3): c.append(a[i]) c.append(9 - b[i]) c.sort(reverse=True) for i in range(6): t = t + c[i] if t >= diff: break print(i + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def change_num(left_arr, right_arr, count): min_n, max_n = 10, -1 if sum(left_arr) > sum(right_arr): max_arr = left_arr min_arr = right_arr else: max_arr = right_arr min_arr = left_arr diff = sum(max_arr) - sum(min_arr) for i in range(3): if min_n > min_arr[i]: min_n = min_arr[i] min_i = i if max_n < max_arr[i]: max_n = max_arr[i] max_i = i if diff <= 9 - min_n: count += 1 return count elif diff <= max_n: count += 1 return count elif max_n >= 9 - min_n: max_arr[max_i] = 0 else: min_arr[min_i] = 9 count += 1 return change_num(min_arr, max_arr, count) msg = input() left = msg[:3] right = msg[3:] left_arr = [] right_arr = [] for char in left: left_arr.append(int(char)) for char in right: right_arr.append(int(char)) if sum(left_arr) == sum(right_arr): print(0) else: print(change_num(left_arr, right_arr, 0))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
x = int(input()) def s(a): r = 0 while a > 0: r += a % 10 a //= 10 return r def d(a, b): r = 0 for i in range(6): if a % 10 != b % 10: r += 1 a //= 10 b //= 10 return r c = 6 for i in range(1000000): if s(i % 1000) == s(i // 1000): c = min(c, d(x, i)) print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() v1 = v2 = 0 for i in s[:3]: v1 += int(i) for i in s[3:]: v2 += int(i) ans = 0 s1 = list(s[:3]) for i in range(3): s1[i] = int(s1[i]) s2 = list(s[3:]) for i in range(3): s2[i] = int(s2[i]) if v1 < v2: while True: ans += 1 if 9 - min(s1) > max(s2): v1 = v1 + 9 - min(s1) s1.remove(min(s1)) s1.append(9) if v1 >= v2: break else: v2 = v2 - max(s2) s2.remove(max(s2)) s2.append(0) if v2 <= v1: break print(ans) elif v1 > v2: while True: ans += 1 if 9 - min(s2) < max(s1): v1 = v1 - max(s1) s1.remove(max(s1)) s1.append(0) if v1 <= v2: break else: v2 = v2 + 9 - min(s2) s2.remove(min(s2)) s2.append(9) if v2 >= v1: break print(ans) else: print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR WHILE NUMBER VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR WHILE NUMBER VAR NUMBER IF BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
def solve(): s = input() x = [int(z) for z in s] y, z = x[:3], x[3:] if sum(y) < sum(z): y, z = z, y a = sum(y) - sum(z) b = [y[-1], y[-2], y[-3], 9 - z[0], 9 - z[1], 9 - z[2]] b.sort() c = 0 cnt = 0 while c < a: c += b.pop() cnt += 1 return cnt print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
from sys import stdin, stdout def rint(): return map(int, stdin.readline().split()) s = input() little = [] big = [] for i in range(3): little.append(int(s[i])) for i in range(3): big.append(int(s[i + 3])) suml = sum(little) sumb = sum(big) if suml > sumb: little, big = big, little suml, sumb = sumb, suml li = [] bi = [] for i in range(3): li.append((9 - little[i], "l")) bi.append((big[i], "b")) a = li + bi a.sort(reverse=True) for i in range(6): if suml >= sumb: print(i) exit() if a[i][1] == "l": suml += a[i][0] else: sumb -= a[i][0] print(6)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER STRING VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky. The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits. -----Input----- You are given a string consisting of 6 characters (all characters are digits from 0 to 9) β€” this string denotes Luba's ticket. The ticket can start with the digit 0. -----Output----- Print one number β€” the minimum possible number of digits Luba needs to replace to make the ticket lucky. -----Examples----- Input 000000 Output 0 Input 123456 Output 2 Input 111000 Output 1 -----Note----- In the first example the ticket is already lucky, so the answer is 0. In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required. In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
s = input() mas = [] for i in range(6): mas += [int(s[i])] a, b = mas[0:3], mas[3:6] if sum(a) < sum(b): a, b = b, a delta = sum(a) - sum(b) a = sorted(a, reverse=True) b.sort() ans, sum, l1, l2 = 0, 0, 0, 0 while sum < delta: if a[l1] > 9 - b[l2]: sum += a[l1] l1 += 1 else: sum += 9 - b[l2] l2 += 1 ans += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR LIST FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
for _ in range(int(input())): y, x = map(int, input().split()) c1, c2, c3, c4, c5, c6 = map(int, input().split()) cost = 0 if y == 0: if x >= 0: cost = min(x * (c3 + c1), x * c2) else: cost = min(abs(x) * (c4 + c6), abs(x) * c5) elif y > 0: if x > 0: cost1 = c6 * y + c2 * x if y >= x: cost2 = c1 * x + c6 * (y - x) cost3 = c1 * y + c5 * (y - x) if y < x: cost2 = c1 * y + c2 * (x - y) cost3 = c1 * x + c3 * (x - y) cost = min(cost1, cost2, cost3) elif x == 0: cost1 = c6 * y cost2 = (c1 + c5) * y cost = min(cost1, cost2) else: x = abs(x) cost1 = c6 * y + c5 * x cost2 = c4 * x + c6 * (x + y) cost3 = c1 * y + c5 * (x + y) cost = min(cost1, cost2, cost3) else: y = abs(y) if x < 0: x = abs(x) cost1 = c5 * x + c3 * y if y >= x: cost2 = c4 * x + c3 * (y - x) cost3 = c4 * y + c2 * (y - x) else: cost2 = c4 * y + c5 * (x - y) cost3 = c4 * x + c6 * (x - y) cost = min(cost1, cost2, cost3) elif x == 0: cost1 = c3 * y cost2 = (c4 + c2) * y cost = min(cost1, cost2) else: cost1 = c2 * x + c3 * y cost2 = c4 * y + c2 * (x + y) cost3 = c1 * x + c3 * (x + y) cost = min(cost1, cost2, cost3) print(cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
import sys input = sys.stdin.readline def solve_fast(x, y, c): ans1 = abs(x) * c[6 if x > 0 else 3] + abs(y) * c[2 if y > 0 else 5] ans2 = abs(x) * c[1 if x > 0 else 4] + abs(y - x) * c[2 if y - x > 0 else 5] ans3 = abs(y) * c[1 if y > 0 else 4] + abs(y - x) * c[3 if y - x > 0 else 6] return min(ans1, ans2, ans3) T = int(input()) for t in range(T): x, y = map(int, input().split()) price = [0] + list(map(int, input().split())) best_cost = solve_fast(x, y, price) print(best_cost)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
def ans2(x, y, cx, cy, cxy): ret = 0 xy = min(x, y) x -= xy y -= xy ret += cxy * xy ret += x * cx ret += y * cy return ret def rotate(x, y): return y, y - x def ans(x, y, costs): for _ in range(100): costs[0] = min(costs[0], costs[1] + costs[5]) costs[1] = min(costs[1], costs[2] + costs[0]) costs[2] = min(costs[2], costs[3] + costs[1]) costs[3] = min(costs[3], costs[4] + costs[2]) costs[4] = min(costs[4], costs[5] + costs[3]) costs[5] = min(costs[5], costs[0] + costs[4]) while not (x >= 0 and y >= 0): x, y = rotate(x, y) costs = costs[1:] + [costs[0]] return ans2(x, y, costs[5], costs[1], costs[0]) for _ in range(int(input())): x, y = input().split() x = int(x) y = int(y) costs = input().split() costs = list(map(int, costs)) print(ans(x, y, costs))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
p = lambda: list(map(int, input().split())) T = int(input()) for _ in range(T): Y, X = p() c = p() for i in range(6): c[i] = min(c[i], c[(i + 1) % 6] + c[(i + 5) % 6]) M = min(abs(X), abs(Y)) if X > 0: if Y > 0: C = M * c[0] X -= M Y -= M C += c[1] * X C += c[5] * Y print(C) else: C = c[1] * X C += c[2] * -Y print(C) elif Y > 0: C = c[5] * Y C += c[4] * -X print(C) else: C = M * c[3] X += M Y += M C += c[4] * -X C += c[2] * -Y print(C)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() inf = 10**20 for _ in range(II()): x, y = MI() cc = LI() def move(x, y, d): cost = 0 if d > 0: cost += cc[0] * d elif d < 0: cost += -cc[3] * d x -= d y -= d return x, y, cost def to0(x, y): cost = 0 if x > 0: cost += cc[5] * x elif x < 0: cost += cc[2] * -x if y > 0: cost += cc[1] * y elif y < 0: cost += cc[4] * -y return cost ans = inf x1, y1, cur = move(x, y, x) cur += to0(x1, y1) ans = min(ans, cur) x1, y1, cur = move(x, y, y) cur += to0(x1, y1) ans = min(ans, cur) cur = to0(x, y) ans = min(ans, cur) print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
for _ in range(int(input())): y, x = map(int, input().split()) uu, w, r, dd, q, e = map(int, input().split()) uuo, wo, ro, ddo, qo, eo = [None] * 6 while (uuo, wo, ro, ddo, qo, eo) != (uu, w, r, dd, q, e): uuo, wo, ro, ddo, qo, eo = uu, w, r, dd, q, e uu = min(uuo, w + e) dd = min(ddo, q + r) r = min(ro, dd + w) e = min(eo, uu + q) q = min(qo, dd + e) w = min(wo, uu + r) if x >= 0 and y >= 0: ans = min(x, y) * uu if y > x: ans += e * (y - x) else: ans += w * (x - y) elif x <= 0 and y <= 0: x = abs(x) y = abs(y) ans = min(x, y) * dd if y > x: ans += r * (y - x) else: ans += q * (x - y) elif x >= 0: ans = x * w - y * r else: ans = -x * q + y * e print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP LIST NONE NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
import sys as _sys def main(): t = int(input()) for i_t in range(t): x, y = _read_ints() cs = tuple(_read_ints()) result = find_min_path_cost((x, y), cs) print(result) def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) def find_min_path_cost(pos, directions_costs): c1, c2, c3, c4, c5, c6 = directions_costs c_0p, c_pp, c_p0, c_0m, c_mm, c_m0 = c2, c1, c6, c5, c4, c3 get_cs = lambda: (c_0p, c_pp, c_p0, c_0m, c_mm, c_m0) while True: old_cs = get_cs() c_0p = min(c_0p, c_pp + c_m0) c_p0 = min(c_p0, c_pp + c_0m) c_pp = min(c_pp, c_0p + c_p0) c_0m = min(c_0m, c_mm + c_p0) c_m0 = min(c_m0, c_mm + c_0p) c_mm = min(c_mm, c_0m + c_m0) if get_cs() == old_cs: break x, y = pos result = 0 if x > 0 and y > 0: operation_pp_n = min(x, y) result += c_pp * operation_pp_n x -= operation_pp_n y -= operation_pp_n if x < 0 and y < 0: operation_mm_n = min(abs(x), abs(y)) result += c_mm * operation_mm_n x += operation_mm_n y += operation_mm_n if x > 0: result += abs(x) * c_p0 elif x < 0: result += abs(x) * c_m0 if y > 0: result += abs(y) * c_0p elif y < 0: result += abs(y) * c_0m return result main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
inn = lambda: int(input()) inm = lambda: map(int, input().split()) ins = lambda: str(input()) ina = lambda: list(map(int, input().split())) def solve(): x, y = inm() c = ina() ans = [0, 0, 0] ans[0] = (x * c[5] if x >= 0 else -x * c[2]) + (y * c[1] if y >= 0 else -y * c[4]) ans[1] = ((x - y) * c[5] if x >= y else (y - x) * c[2]) + ( y * c[0] if y >= 0 else -y * c[3] ) ans[2] = ((y - x) * c[1] if y >= x else (x - y) * c[4]) + ( x * c[0] if x >= 0 else -x * c[3] ) print(min(ans)) def main(): t = 1 t = int(input()) for _ in range(t): solve() main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
t = int(input()) for _ in range(t): x, y = map(int, input().split()) c1, c2, c3, c4, c5, c6 = map(int, input().split()) c1 = min(c1, c2 + c6) c2 = min(c2, c1 + c3) c3 = min(c3, c2 + c4) c4 = min(c4, c3 + c5) c5 = min(c5, c4 + c6) c6 = min(c6, c1 + c5) ans = 0 if x * y <= 0: ans += c6 * x if x >= 0 else c3 * -x ans += c2 * y if y >= 0 else c5 * -y else: if x < 0 or y < 0: cc = max(x, y) x -= cc y -= cc ans += c4 * -cc else: cc = min(x, y) x -= cc y -= cc ans += c1 * cc ans += c6 * x if x >= 0 else c3 * -x ans += c2 * y if y >= 0 else c5 * -y print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
for _ in range(int(input())): x, y = map(int, input().split()) c = list(map(int, input().split())) c11 = min(c[0], c[1] + c[5]) c_1_1 = min(c[3], c[2] + c[4]) c01 = min(c[1], c[0] + c[2]) c10 = min(c[5], c[0] + c[4]) c0_1 = min(c[4], c[3] + c[5]) c_10 = min(c[2], c[3] + c[1]) if x >= 0 and y >= 0: m = min(x, y) ans = c11 * m x -= m y -= m ans += c10 * x ans += c01 * y elif x >= 0 and y < 0: ans = c10 * x + c0_1 * abs(y) elif x < 0 and y >= 0: ans = c_10 * abs(x) + c01 * y else: x = abs(x) y = abs(y) m = min(x, y) ans = c_1_1 * m ans += c_10 * (x - m) ans += c0_1 * (y - m) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
def solve(): y, x = map(int, input().split()) c = list(map(int, input().split())) if x == 0 and y == 0: return 0 d1 = 0 if x > 0: d1 += x * c[1] else: d1 += abs(x) * c[4] if y > 0: d1 += y * c[5] else: d1 += abs(y) * c[2] d2 = 0 if x > 0: d2 += x * c[0] else: d2 += abs(x) * c[3] if x > y: d2 += (x - y) * c[2] else: d2 += (y - x) * c[5] d3 = 0 if y > 0: d3 += y * c[0] else: d3 += abs(y) * c[3] if y > x: d3 += (y - x) * c[4] else: d3 += (x - y) * c[1] return min(d1, d2, d3) t = int(input()) i = 0 while i < t: print(solve()) i += 1
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
t = int(input()) for _ in range(t): x, y = map(int, input().split()) c1, c2, c3, c4, c5, c6 = map(int, input().split()) if x == y == 0: print(0) continue ans = 10**20 if x >= 0 and y >= 0 and y >= x: ans = min(ans, c2 * (y - x) + c1 * x) if y >= 0 and x <= y: ans = min(ans, c1 * y + c3 * (y - x)) if x >= 0 and y <= x: ans = min(ans, c1 * x + c5 * (x - y)) if x >= 0 and y <= x and y >= 0: ans = min(ans, c6 * (x - y) + c1 * y) if x <= 0 and y >= 0: ans = min(ans, c2 * y + c3 * abs(x)) if x <= 0 and y >= x: ans = min(ans, c2 * (y - x) + c4 * abs(x)) if x >= 0 and y >= 0: ans = min(ans, c2 * y + c6 * x) if y <= 0 and x <= y: ans = min(ans, c3 * (y - x) + c4 * abs(y)) if x <= 0 and y <= 0: ans = min(ans, c3 * abs(x) + c5 * abs(y)) if x <= 0 and y <= 0 and y <= x: ans = min(ans, c4 * abs(x) + c5 * (x - y)) if y <= 0 and x >= y: ans = min(ans, c4 * abs(y) + c6 * (x - y)) if x >= 0 and y <= 0: ans = min(ans, c5 * abs(y) + c6 * x) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
T = int(input()) for t in range(T): X, Y = [int(_) for _ in input().split()] C = [int(_) for _ in input().split()] for _ in range(10): for i in range(len(C)): prev = i - 1 if prev < 0: prev += 6 nxt = i + 1 if nxt >= 6: nxt -= 6 C[i] = min(C[i], C[prev] + C[nxt]) for i in range(len(C) - 1, -1, -1): prev = i - 1 if prev < 0: prev += 6 nxt = i + 1 if nxt >= 6: nxt -= 6 C[i] = min(C[i], C[prev] + C[nxt]) c1, c2, c3, c4, c5, c6 = C[0], C[1], C[2], C[3], C[4], C[5] if X == 0: if Y > 0: print(c2 * Y) else: print(c5 * abs(Y)) elif X < 0: if Y >= 0: print(c3 * abs(X) + c2 * Y) elif Y <= X: print(c4 * abs(X) + c5 * abs(X - Y)) else: print(c3 * abs(X - Y) + c4 * abs(Y)) elif X > 0: if Y >= X: print(c1 * X + (Y - X) * c2) elif Y <= 0: print(c6 * X + abs(Y) * c5) else: print(c1 * Y + c6 * (X - Y))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
t = int(input()) for _ in range(t): x, y = list(map(int, input().split())) c1, c2, c3, c4, c5, c6 = list(map(int, input().split())) a1 = min(c1, c6 + c2) a2 = min(c2, c1 + c3) a3 = min(c3, c2 + c4) a4 = min(c4, c3 + c5) a5 = min(c5, c4 + c6) a6 = min(c6, c5 + c1) if x > 0 and y > 0: if x > y: ans = a1 * y + a6 * (x - y) else: ans = a1 * x + a2 * (y - x) elif x >= 0 and y <= 0: ans = a5 * -y + a6 * x elif x <= 0 and y >= 0: ans = a2 * y + a3 * -x else: x *= -1 y *= -1 if x > y: ans = a4 * y + a3 * (x - y) else: ans = a4 * x + a5 * (y - x) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
t = int(input()) while t: t -= 1 x, y = list(map(int, input().strip().split())) c1, c2, c3, c4, c5, c6 = list(map(int, input().strip().split())) t1, t2, t3, t4, t5, t6 = c1, c2, c3, c4, c5, c6 c1 = min(c1, t2 + t6) c2 = min(c2, t1 + t3) c3 = min(c3, t2 + t4) c4 = min(c4, t3 + t5) c5 = min(c5, t4 + t6) c6 = min(c6, t5 + t1) ans = 0 if x >= 0 and y >= 0: tmp = min(x, y) ans += tmp * c1 x -= tmp y -= tmp elif x < 0 and y < 0: tmp = min(-x, -y) ans += tmp * c4 x += tmp y += tmp if x < 0: ans += -x * c3 elif x > 0: ans += x * c6 if y < 0: ans += -y * c5 elif y > 0: ans += y * c2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
from sys import stdin input = stdin.readline def solve(): y, x = map(int, input().split()) c = list(map(int, input().split())) res = float("inf") r1 = abs(y) * c[0 if y >= 0 else 3] + abs(x - y) * c[1 if y <= x else 4] r2 = abs(y) * c[5 if y >= 0 else 2] + abs(x) * c[1 if x >= 0 else 4] r3 = abs(x) * c[0 if x >= 0 else 3] + abs(x - y) * c[5 if x <= y else 2] print(min(r1, r2, r3)) t = int(input()) for _ in range(t): solve()
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
for i in range(int(input())): a = list(map(int, input().strip().split(" "))) b = list(map(int, input().strip().split(" "))) b.append(b[0]) b.insert(0, b[-2]) for j in range(1, len(b) - 1): b[j] = min(b[j - 1] + b[j + 1], b[j]) s = 0 if a[1] > 0 and a[0] > 0: s += min(a[1], a[0]) * b[1] c = min(a[1], a[0]) a[1] -= c a[0] -= c elif a[1] < 0 and a[0] < 0: s -= max(a[1], a[0]) * b[4] c = max(a[1], a[0]) a[1] -= c a[0] -= c if a[0] > 0: s += a[0] * b[6] if a[0] < 0: s += -a[0] * b[3] if a[1] > 0: s += a[1] * b[2] if a[1] < 0: s += -a[1] * b[5] print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
def main(): for _ in range(int(input())): x, y = map(int, input().split()) c = list(map(int, input().split())) r1 = abs(x) * c[5 if x >= 0 else 2] + abs(y) * c[1 if y >= 0 else 4] r2 = abs(x) * c[0 if x >= 0 else 3] + abs(y - x) * c[1 if y >= x else 4] r3 = abs(y) * c[0 if y >= 0 else 3] + abs(y - x) * c[5 if x >= y else 2] print(min([r1, r2, r3])) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR
Lindsey Buckingham told Stevie Nicks "Go your own way". Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world. Consider a hexagonal tiling of the plane as on the picture below. [Image] Nicks wishes to go from the cell marked $(0, 0)$ to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from $(0, 0)$ to $(1, 1)$ will take the exact same cost as going from $(-2, -1)$ to $(-1, 0)$. The costs are given in the input in the order $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ as in the picture below. [Image] Print the smallest cost of a path from the origin which has coordinates $(0, 0)$ to the given cell. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^{4}$). Description of the test cases follows. The first line of each test case contains two integers $x$ and $y$ ($-10^{9} \le x, y \le 10^{9}$) representing the coordinates of the target hexagon. The second line of each test case contains six integers $c_1$, $c_2$, $c_3$, $c_4$, $c_5$, $c_6$ ($1 \le c_1, c_2, c_3, c_4, c_5, c_6 \le 10^{9}$) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value). -----Output----- For each testcase output the smallest cost of a path from the origin to the given cell. -----Example----- Input 2 -3 1 1 3 5 7 9 11 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Output 18 1000000000000000000 -----Note----- The picture below shows the solution for the first sample. The cost $18$ is reached by taking $c_3$ 3 times and $c_2$ once, amounting to $5+5+5+3=18$. [Image]
def getDistances(x, y, dx, dy): res = [] if dx == 0: if x == 0: res += [0] else: cnt = x // -dx if cnt >= 0: res += [cnt] if dy == 0: if y == 0: res += [0] else: cnt = y // -dy if cnt >= 0: res += [cnt] if dx == dy: if x == y: res += [0] else: a = x - y b = dx - dy cnt = a // -b if cnt >= 0: res += [cnt] return res tt = int(input()) for _ in range(tt): x, y = [int(x) for x in input().strip().split()] c1, c2, c3, c4, c5, c6 = [int(x) for x in input().strip().split()] x = -x y = -y c = [ (c1, (1, 1)), (c2, (0, 1)), (c3, (-1, 0)), (c4, (-1, -1)), (c5, (0, -1)), (c6, (1, 0)), ] res = 1e47 for a in c: dx = a[1][0] dy = a[1][1] ds = getDistances(x, y, dx, dy) for d in ds: xx = x + d * dx yy = y + d * dy add = 1e47 if xx == 0 and yy >= 0: add = c5 * yy elif xx == 0 and yy < 0: add = c2 * -yy elif yy == 0 and xx >= 0: add = c3 * xx elif yy == 0 and xx < 0: add = c6 * -xx elif xx == yy and xx >= 0: add = c4 * xx elif xx == yy and xx < 0: add = c1 * -xx else: print(a, d, xx, yy) raise Exception() cur = d * a[0] + add res = min(res, cur) print(res)
FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER IF VAR NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR LIST VAR IF VAR NUMBER IF VAR NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR LIST VAR IF VAR VAR IF VAR VAR VAR LIST NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR LIST VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR