description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def solve(): t = int(sys.stdin.readline()) for _ in range(t): n = int(sys.stdin.readline()) blast = 0 minb = 0 s = 0 a0 = 0 for i in range(n): ai, bi = map(int, sys.stdin.readline().split()) bbigger = False if i != 0: if i > 1: if ai >= blast: minb = min(minb, blast) else: bbigger = True minb = min(minb, ai) elif ai >= blast: minb = blast else: bbigger = True minb = ai if not bbigger: s += ai - blast else: a0 = ai blast = bi s += max(0, a0 - blast) minb = min(minb, min(a0, blast)) print(s + minb) solve()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) a, b = [], [] for i in range(n): x, y = map(int, input().split()) a.append(x) b.append(y) c = [(0) for _ in range(n)] c_sum = 0 for i in range(n): if i == 0: x = max(0, a[0] - b[n - 1]) else: x = max(0, a[i] - b[i - 1]) c[i] = x c_sum += x ans = 10**20 for i in range(n): ans = min(ans, c_sum - c[i] + a[i]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys readline = sys.stdin.buffer.readline T = int(readline()) Ans = [None] * T inf = 1 << 60 for qu in range(T): N = int(readline()) AB = [tuple(map(int, readline().split())) for _ in range(N)] A, B = map(list, zip(*AB)) CA = [0] * N CA[0] = max(0, A[0] - B[N - 1]) for i in range(N): CA[i] = max(0, A[i] - B[i - 1]) Sc = sum(CA) ans = inf for i in range(N): ans = min(ans, Sc - CA[i] + A[i]) Ans[qu] = ans print("\n".join(map(str, Ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) x, y = map(int, input().split()) a, b = x, y suma = 0 mini = float("inf") for __ in range(n - 1): x, y = map(int, input().split()) dmg = max(x - b, 0) suma += dmg mini = min(x - dmg, mini) b = y dmg = max(a - b, 0) suma += dmg mini = min(a - dmg, mini) sys.stdout.write(str(suma + mini) + "\n")
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) ans = [] for _ in range(t): n = int(input()) a = [] b = [] for _ in range(n): a_, b_ = map(int, input().split()) a.append(a_) b.append(b_) cost = a[0] for i in range(n - 1): next_i = i + 1 if b[i] < a[next_i]: cost += a[next_i] - b[i] pre_cost = cost for i in range(1, n): next_i = i + 1 if next_i == n: next_i = 0 pre_i = i - 1 if pre_i == -1: pre_i = n - 1 ppre_i = i - 2 if ppre_i < 0: ppre_i %= n cur_cost = ( pre_cost - a[pre_i] - max(a[i] - b[pre_i], 0) + a[i] + max(a[pre_i] - b[ppre_i], 0) ) pre_cost = cur_cost cost = min(cost, cur_cost) ans.append(str(cost)) print("\n".join(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) ans = [] for _ in range(t): n = int(input()) a = [0] * n b = [0] * n total = 0 minA = -1 for i in range(n): a1, b1 = map(int, input().split()) a[i] = a1 b[i] = b1 if not i == 0: x = max(0, a[i] - b[i - 1]) total += x if minA == -1: minA = a[i] - x else: minA = min(minA, a[i] - x) total += max(0, a[0] - b[n - 1]) minA = min(minA, a[0] - max(0, a[0] - b[n - 1])) ans.append(total + minA) print("\n".join(map(str, ans)))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def data(): return sys.stdin.buffer.readline().strip() def mdata(): return map(int, data().split()) for t in range(int(data())): n = int(data()) a, b = mdata() a1, b1 = a, b k = s = a1 for i in range(n - 1): x, y = mdata() if x > b: s = s + x - b k = min(b, k) else: k = min(k, x) b = y k = min(k, min(a1, b)) s = s - min(a1, b) + k sys.stdout.write(str(s) + "\n")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for k in range(t): arr = [] ls = [] n = int(input()) for j in range(n): h, d = map(int, input().split()) arr.append(h) ls.append(d) for i in range(n - 1): ls[i] = min(ls[i], arr[i + 1]) ls[-1] = min(ls[-1], arr[0]) print(sum(arr) - sum(ls) + min(ls))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys from itertools import islice stdin = sys.stdin T = int(stdin.readline()) def kill_mon(arr): last_explosion = arr[-1][1] bullets = 0 mini = 100000000000000000000 for hp, exp in arr: if hp > last_explosion: bullets += hp - last_explosion candidate = last_explosion if last_explosion < hp else hp if candidate < mini: mini = candidate last_explosion = exp return bullets + mini for x in range(T): n = int(stdin.readline()) monsters = [[int(y) for y in x.split()] for x in islice(stdin, n)] print(kill_mon(monsters))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n = int(input()) a, b = [0] * n, [0] * n for i in range(n): a[i], b[i] = map(int, input().split()) mini, minb = -1, 1e18 for i in range(n): e = a[i] - max(0, a[i] - b[i - 1]) if e < minb: mini = i minb = e res = a[mini] for i in range(1, n): res += max(0, a[(mini + i) % n] - b[(mini + i - 1) % n]) print(res)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import gettrace, stdin def input(): return stdin.buffer.readline() def main(): def solve(): n = int(input()) aa = [] bb = [] mins = int(100000000000000.0) sum = 0 for i in range(n): a, b = map(int, input().split()) aa.append(a) bb.append(b) for i in range(n): sum += max(0, aa[i] - bb[(i - 1) % n]) for i in range(n): mins = min(aa[i], bb[(i - 1) % n], mins) print(sum + mins) q = int(input()) for _ in range(q): solve() main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) count = 0 a = [0] * n b = [0] * n minimum = 10**12 + 1 for l in range(n): a[l], b[l] = map(int, input().split()) for i in range(n): if a[i] - b[i - 1] > 0: count += a[i] - b[i - 1] minimum = min(minimum, b[i - 1]) else: minimum = min(minimum, a[i]) count += minimum print(count)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def gift(): for _ in range(t): n = int(input()) inamo = [] for i in range(n): mosnteri = list(map(int, input().split())) inamo.append(mosnteri) dingding = [max(inamo[0][0] - inamo[n - 1][1], 0)] biu = dingding[0] for i in range(n - 1): curr = inamo[i][1] nexmon = inamo[i + 1][0] temp = max(nexmon - curr, 0) biu += temp dingding.append(temp) jojo = float("inf") for i in range(n): jojo = min(jojo, inamo[i][0] + biu - dingding[i]) yield jojo t = int(input()) ans = gift() print(*ans, sep="\n")
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) a = [] b = [] for __ in range(n): aa, bb = [int(x) for x in input().split()] a.append(aa) b.append(bb) if n == 1: print(a[0]) continue total = 0 for i in range(n): if i == 0: bVal = b[n - 1] else: bVal = b[i - 1] total += max(a[i] - bVal, 0) ans = total - max(a[1] - b[0], 0) + a[1] for i in range(n): aVal = a[i] if i == 0: bVal = b[n - 1] else: bVal = b[i - 1] ans2 = total - max((aVal - bVal, 0)) + aVal ans = min(ans, ans2) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin, stdout t = int(stdin.readline()) ans = [] for s in range(t): n = int(stdin.readline()) least, bs = map(int, stdin.readline().split()) start = least total = 0 for i in range(n - 1): a, b = map(int, stdin.readline().split()) least = min(least, a, bs) total += max(0, a - bs) bs = b total += max(0, start - bs) ans.append(str(total + min(least, start, bs)) + "\n") stdout.writelines(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline a = int(input()) for x in range(a): d = int(input()) j = [] h = 0 mi = 0 p = 0 for y in range(d): b, c = map(int, input().split()) j.append([b, c]) if y == 0: h += b elif p - b >= 0: pass else: h += b - p p = c mi = h for y in range(d - 1): h -= j[y][0] if p - j[y][0] >= 0: pass else: h += j[y][0] - p if j[y][1] - j[y + 1][0] >= 0: h += j[y + 1][0] else: h -= j[y + 1][0] - j[y][1] h += j[y + 1][0] if mi > h: mi = h p = j[y][1] print(mi)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def main(): res = "" input = sys.stdin.readline print = sys.stdout.write t = int(input()) for _ in range(t): sub_res = 0 n = int(input()) hp_f, boom_f = map(int, input().split()) boom_last = boom_f min_hp = hp_f for __ in range(n - 1): hp, boom = map(int, input().split()) d = hp - boom_last if d > 0: sub_res += d hp -= d if hp < min_hp: min_hp = hp boom_last = boom hp, boom = hp_f, boom_f d = hp - boom_last if d > 0: sub_res += d if hp - d < min_hp: min_hp = hp - d sub_res += min_hp res += str(sub_res) + "\n" print(res) main()
IMPORT FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def main(): import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) l = [] for i in range(n): a, b = map(int, input().split()) l.append([a, b]) ans = 0 for i in range(n - 1): if l[i][1] - l[i + 1][0] < 0: ans += l[i + 1][0] - l[i][1] l[i + 1][0] = l[i + 1][0] - (l[i + 1][0] - l[i][1]) if l[-1][1] - l[0][0] < 0: ans += l[0][0] - l[-1][1] l[0][0] = l[0][0] - (l[0][0] - l[-1][1]) min = 99999999999999 for i in range(n): if l[i][0] < min: min = l[i][0] print(ans + min) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) a = [] b = [] for i in range(0, n): ai, bi = list(map(int, input().split())) a.append(ai) b.append(bi) extra = [] for i in range(0, n): extra.append(max(0, a[i] - b[(i - 1) % n])) T = sum(extra) c = [] for i in range(0, n): c.append(T - extra[i] + a[i]) print(min(c))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline MI = 10**12 + 1 t = int(input()) for _ in range(t): n = int(input()) nums = [tuple(map(int, input().split())) for _ in range(n)] ans = 0 mi = MI for i, num in enumerate(nums): _, before_b = nums[i - 1] a, _ = num damage = max(0, a - before_b) ans += damage a -= damage mi = min(a, mi) print(ans + mi)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) a, b, d, s_d = [0] * n, [0] * n, [0] * n, 0 for i in range(n): a[i], b[i] = map(int, input().split()) d[i] = a[i] if i == 0 else max(0, a[i] - b[i - 1]) s_d += d[i] ans, s_m = s_d, s_d - a[0] + max(0, a[0] - b[-1]) for i in range(1, n): ans = min(ans, s_m - d[i] + a[i]) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) ans = 0 c = [] mn = 10**15 for i in range(n): a, b = map(int, input().split()) c.append((a, b)) for i in range(n): k = max(0, c[i][0] - c[i - 1][1]) ans += k mn = min(mn, c[i][0] - k) print(ans + mn)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def solution(): n = int(input()) a = [] m = 1e18 b = [] ans = 0 for i in range(n): x, y = map(int, input().split()) a.append(x) b.append(y) for i in range(n): z = (i + 1) % n val = min(a[z], b[i]) ans += a[z] - val m = min(m, val) ans += m print(ans) for _ in range(int(input())): solution()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) h, d = [0] * n, [0] * n for i in range(n): x, y = map(int, input().split()) h[i], d[i] = x, y for i in range(n - 1): d[i] = min(d[i], h[i + 1]) d[-1] = min(d[-1], h[0]) print(sum(h) - sum(d) + min(d))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys read = sys.stdin.buffer.read input = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines t = int(input()) for i in range(t): n = int(input()) a = [] b = [] for j in range(n): c = list(map(int, input().split())) a.append(c[0]) b.append(c[1]) for j in range(n): if b[j - 1] >= a[j]: b[j - 1] = a[j] y = min(b) s = sum(b) - y print(sum(a) - s)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys I = sys.stdin.buffer.readline for _ in range(int(I())): n = int(I()) a = [] b = [] c = [0] sumi = 0 ans = int(1e17) for i in range(n): ai, bi = map(int, I().split()) a.append(ai) b.append(bi) if i != 0: c.append(max(0, a[i] - b[(i - 1) % n])) sumi += c[i] ans = min(a[i] - c[i], ans) c[0] = max(0, a[0] - b[n - 1]) sumi += c[0] ans = min(a[0] - c[0], ans) print(ans + sumi)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin def input(): return stdin.buffer.readline() for _ in range(int(input())): n = int(input()) a, b = list(), list() for __ in range(n): ai, bi = map(int, input().split()) a.append(ai) b.append(bi) gain = [0] * n gain[0] = min(a[0], b[n - 1]) imin, gmin = 0, gain[0] for i in range(1, n): gain[i] = min(a[i], b[i - 1]) if gain[i] < gmin: imin = i gmin = gain[i] out = a[imin] a[0] = max(0, a[0] - b[n - 1]) for i in range(n): if i == imin: continue if i != 0: a[i] = max(0, a[i] - b[i - 1]) out += a[i] print(out)
FUNC_DEF RETURN FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) health, damage = [], [] a, b, _min = 0, 0, 10**15 for i in range(n): h, d = map(int, input().split()) a += h health.append(h) damage.append(d) for i in range(n): damage[i] = min(health[(i + 1) % n], damage[i]) b += damage[i] _min = min(_min, damage[i]) print(a - b + _min)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys read = sys.stdin.buffer.read input = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines t = int(input()) for case in range(t): n = int(input()) a = [0] * n b = [0] * n c = [0] * n min_c = 10**13 sum_a = 0 sum_c = 0 for i in range(n): a[i], b[i] = map(int, input().split()) sum_a += a[i] if i > 0: c[i] = min(b[i - 1], a[i]) sum_c += c[i] if c[i] < min_c: min_c = c[i] c[0] = min(b[-1], a[0]) sum_c += c[0] if c[0] < min_c: min_c = c[0] ans = sum_a - sum_c + min_c print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def swaparr(arr, a, b): temp = arr[a] arr[a] = arr[b] arr[b] = temp def gcd(a, b): if a == 0: return b return gcd(b % a, a) def primefs(n): primes = {} while n % 2 == 0: primes[2] = primes.get(2, 0) + 1 n = n // 2 for i in range(3, int(n**0.5) + 2, 2): while n % i == 0: primes[i] = primes.get(i, 0) + 1 n = n // i if n > 2: primes[n] = primes.get(n, 0) + 1 return primes def swap(a, b): temp = a a = b b = temp return a, b def find(x, link): while x != link[x]: x = link[x] return x def union(x, y, size, link): x = find(x, link) y = find(y, link) if size[x] < size[y]: x, y = swap(x, y) if x != y: size[x] += size[y] link[y] = x def sieve(n): prime = [(True) for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 return prime MAXN = int(1000000.0 + 5) def spf_sieve(): spf[1] = 1 for i in range(2, MAXN): spf[i] = i for i in range(4, MAXN, 2): spf[i] = 2 for i in range(3, ceil(MAXN**0.5), 2): if spf[i] == i: for j in range(i * i, MAXN, i): if spf[j] == j: spf[j] = i def factoriazation(x): ret = {} while x != 1: ret[spf[x]] = ret.get(spf[x], 0) + 1 x = x // spf[x] return ret def int_array(): return list(map(int, input().strip().split())) def str_array(): return input().strip().split() MOD = int(1000000000.0) + 7 CMOD = 998244353 INF = float("inf") NINF = -float("inf") for _ in range(int(input())): n = int(input()) a = [] b = [] for __ in range(n): x, y = int_array() a.append(x) b.append(y) that = [None] * n that[0] = max(0, a[0] - b[-1]) for i in range(1, n): that[i] = max(0, a[i] - b[i - 1]) var = sum(that[:n]) ans = INF for i in range(n): var -= that[i] cost = var + a[i] var += that[i] ans = min(ans, cost) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF WHILE VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin input = stdin.buffer.readline N = int(input()) for Num in range(N): num1 = int(input()) list1 = [] bull = 0 prev = 0 for _ in range(num1): p, q = map(int, input().split()) list1.append([p, q]) if len(list1) != 1: if p > prev: bull = bull + p - prev p = prev list1[_][0] = p prev = q if list1[0][0] > prev: bull = bull + list1[0][0] - prev list1[0][0] = prev bull += min(list1)[0] print(bull)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline T = int(input()) for iTest in range(T): w = int(input()) a = [0] * w b = [0] * w for i in range(w): a[i], b[i] = map(int, input().split()) b[w - 1] = min(a[0], b[w - 1]) for i in range(w - 1): b[i] = min(b[i], a[i + 1]) print(sum(a) - sum(b) + min(b))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.readline INF = 1001002003004005006 def main(): t = int(input()) for _ in range(t): n = int(input()) ans, mi, blast = 0, INF, 0 for i in range(n): a, b = map(int, input().split()) if i: if a > blast: ans += a - blast mi = min(mi, blast) else: mi = min(mi, a) else: a0 = a blast = b ans += max(0, a0 - blast) mi = min(mi, min(a0, blast)) print(ans + mi) return main()
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a, b = map(int, stdin.readline().split()) ttt = a mn = a if n == 1: print(a) else: ans = 0 for i in range(n - 1): x, y = map(int, stdin.readline().split()) if x > b: ans += x - b x = b mn = min(mn, x) a, b = x, y if ttt > b: ans += ttt - b ttt = b mn = min(mn, ttt) ans += mn print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin, stdout input = stdin.buffer.readline t = int(input()) for _ in range(t): n = int(input()) a = [] b = [] for __ in range(n): x, y = list(map(int, input().split())) a.append(x) b.append(y) sm = max(0, a[0] - b[-1]) a[0] -= max(0, a[0] - b[-1]) for i in range(1, n): sm += max(0, a[i] - b[i - 1]) a[i] -= max(0, a[i] - b[i - 1]) stdout.write(str(sm + min(a)) + "\n")
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def kot(a, b): if a > b: return a - b else: return 0 def sol(): n = int(input()) mon = [[0, 0]] * n for x in range(0, n): mon[x] = list(map(int, input().split())) a = mon[0][0] for x in range(1, n): if mon[x][0] - mon[x - 1][1] > 0: a += mon[x][0] - mon[x - 1][1] else: a += 0 ans_a = ( a - (mon[0][0] - mon[1][0]) - kot(mon[1][0], mon[0][1]) + kot(mon[0][0], mon[n - 1][1]) ) res = min(ans_a, a) for x in range(2, n): ans_a -= ( mon[x - 1][0] - mon[x][0] + kot(mon[x][0], mon[x - 1][1]) - kot(mon[x - 1][0], mon[x - 2][1]) ) res = min(res, ans_a) return res case = int(input()) for x in range(0, case): print(sol())
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin, stdout input = stdin.buffer.readline t = int(input()) while t > 0: n = int(input()) a = [] b = [] for i in range(n): p, c = [int(x) for x in input().split()] a.append([p, c]) if n > 1: if a[0][0] - a[n - 1][1] > 0: b.append(a[0][0] - a[n - 1][1]) else: b.append(0) for i in range(1, n): if a[i][0] - a[i - 1][1] > 0: b.append(a[i][0] - a[i - 1][1]) else: b.append(0) total = sum(b) fin = [] for i in range(n): fin.append(total + (a[i][0] - b[i])) ans = min(fin) print(str(ans) + "\n") else: print(str(a[0][0]) + "\n") t -= 1
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER STRING VAR NUMBER
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline tests = int(input()) for ii in range(tests): n = int(input()) a = [] b = [] for i in range(1, n + 1): x, y = map(int, input().split()) a.append(x) b.append(y) sum = 0 a.append(a[0]) b.append(b[0]) for i in range(1, n + 1): sum += max(a[i] - b[i - 1], 0) ans = sum + a[1] for i in range(1, n + 1): val = max(a[i] - b[i - 1], 0) ans = min(ans, sum + a[i] - val) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) c = [] for i in range(n): a, b = list(map(int, input().split())) c.append([a, b]) sum = 0 for i in range(n): x = c[i][0] y = max(0, c[i][0] - c[i - 1][1]) sum += y if i == 0: diff = y - x else: diff = max(diff, y - x) print(sum - diff)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def solve(): n = int(input()) if n == 0: return 0 if n == 1: a, b = map(int, input().split()) return a a_first, b_first = map(int, input().split()) a_prev, b_prev = a_first, b_first remainder = float("inf") base_shots = 0 for _ in range(n - 1): a_curr, b_curr = map(int, input().split()) delta = a_curr - b_prev if delta > 0: base_shots += delta remainder = min(b_prev, remainder) else: remainder = min(a_curr, remainder) a_prev, b_prev = a_curr, b_curr a_curr, b_curr = a_first, b_first delta = a_curr - b_prev if delta > 0: base_shots += delta remainder = min(b_prev, remainder) else: remainder = min(a_curr, remainder) return base_shots + remainder task_number = int(input()) solutions = [str(solve()) for _ in range(task_number)] print("\n".join(solutions))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys I = sys.stdin.buffer.readline for _ in [0] * int(I()): a, b = zip(*(map(int, I().split()) for _ in [0] * int(I()))) b = [min(x, y) for x, y in zip(b, a[1:] + a[:1])] print(sum(a) - sum(b) + min(b))
IMPORT ASSIGN VAR VAR FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) def solve(): N = getInt() sum_A = 0 sum_B = 0 min_B = 10**15 for n in range(N): a, b = getInts() if n > 0: y = min(a, prev_b) sum_B += y min_B = min(min_B, y) else: first_a = a sum_A += a prev_b = b y = min(first_a, prev_b) sum_B += y min_B = min(min_B, y) return sum_A - sum_B + min_B T = getInt() for t in range(T): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def LI(): return tuple(map(int, sys.stdin.readline().split())) def I(): return int(sys.stdin.readline()) def JA(a, sep): return sep.join(map(str, a)) def main(): t = I() rr = [] for _ in range(t): n = I() ab = [LI() for _ in range(n)] r = 0 t = 10**12 for i in range(n): a = ab[i][0] k = max(a - ab[i - 1][1], 0) r += k if t > a - k: t = a - k rr.append(r + t) return JA(rr, "\n") print(main())
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys I = sys.stdin.buffer.readline input = lambda: stdin.readline() for _ in range(int(I())): n = int(I()) ans, mn = 0, 100000000000000 ai, bi = map(int, I().split()) for i in range(n - 1): na, nb = map(int, I().split()) if na > bi: ans += na - bi mn = min(mn, bi) else: mn = min(mn, na) bi = nb if ai > bi: ans += ai - bi mn = min(mn, bi) else: mn = min(mn, ai) print(ans + mn)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for qw in range(t): n = int(input()) cc = 0 pepe = 0 iff = 0 els = 0 counts = 0 for i in range(n): a, b = map(int, input().split()) if i != 0: count = bb - a if count < 0: if cc == 0: crow = bb cc = 1 elif crow > bb: crow = bb iff = 1 counts += a - bb else: if pepe == 0: hoe = a pepe = 1 elif hoe > a: hoe = a els = 1 if i == 0: swag = a ver = b bb = b aa = a count = b - swag if count < 0: if cc == 0: crow = b cc = 1 elif crow > bb: crow = b iff = 1 counts += swag - b else: if pepe == 0: hoe = swag pepe = 1 elif hoe > swag: hoe = swag els = 1 if iff != 0 and els != 0: if crow < hoe: ans = counts + crow else: ans = hoe + counts else: if iff == 0: ans = hoe if els == 0: ww = crow ans = ww + counts print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) l1 = [] l2 = [] total = 0 min_index = 10**12 + 1 min_prev = 10**12 + 1 a1, b1 = map(int, input().split()) l1.append(a1) l2.append(b1) for i in range(n): if i == 0: continue a, b = map(int, input().split()) l1.append(a) l2.append(b) total += max(0, l1[i] - l2[i - 1]) if min_index - max(0, min_index - min_prev) > l1[i] - max(0, l1[i] - l2[i - 1]): min_index = l1[i] min_prev = l2[i - 1] total += max(0, a1 - b) if min_index - max(0, min_index - min_prev) > l1[0] - max(0, l1[0] - l2[-1]): min_index = l1[0] min_prev = l2[-1] total += min_index - max(0, min_index - min_prev) sys.stdout.write(str(total) + "\n")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) while t: t -= 1 n = int(input()) x = [] y = [] for i in range(n): a, b = map(int, input().split()) x.append(a) y.append(b) count = 0 for i in range(n): if x[i] - y[i - 1] > 0: count += x[i] - y[i - 1] x[i] = y[i - 1] ans = min(x) print(count + ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin input = stdin.buffer.readline for _ in range(int(input())): n = int(input()) a = [[int(i) for i in input().split()] for j in range(n)] s = max(0, a[0][0] - a[-1][1]) mn = min( a[0][0] - s, min(a[i][0] - max(0, a[i][0] - a[i - 1][1]) for i in range(1, n)) ) print(s + sum(max(0, a[i][0] - a[i - 1][1]) for i in range(1, n)) + mn)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin, stdout sprint = stdout.write sinput = stdin.buffer.readline def func(): n = int(sinput()) health = [] exp = [] for i in range(n): [h, e] = [int(x) for x in sinput().split()] health.append(h) exp.append(e) bullet = 0 for i in range(n): te = max(0, health[i] - exp[i - 1]) bullet += te health[i] -= te sprint("%d\n" % (bullet + min(health))) t = int(sinput()) while t != 0: t -= 1 func()
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def solve(n, arr): total = 0 for i in range(n - 1): total += max(arr[i + 1][0] - arr[i][1], 0) total += max(arr[0][0] - arr[n - 1][1], 0) minv = float("inf") for i in range(1, n): t = arr[i][0] - max(arr[i][0] - arr[i - 1][1], 0) + total minv = min(minv, t) minv = min(minv, arr[0][0] - max(arr[0][0] - arr[n - 1][1], 0) + total) return minv t = int(input()) for _ in range(t): n = int(input()) arr = [[int(el) for el in input().split()] for i in range(n)] ans = solve(n, arr) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def main(): t = int(input()) ans = [0] * t for j in range(t): n = int(input()) l = [list(map(int, input().split())) for _ in range(n)] s = 0 m = 100000000000000.0 fb = l[-1][-1] for a, b in l: t = max(0, a - fb) s += t if a - t < m: m = a - t fb = b ans[j] = s + m print("\n".join(map(str, ans))) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import gettrace, stdin def input(): return stdin.buffer.readline() def main(): def solve(): n = int(input()) li = [] for i in range(n): temp = [int(x) for x in input().split()] li.append(temp) if i == 0: ans = li[0][0] else: ans += max(0, li[i][0] - li[i - 1][1]) ans_final = 9999999999999999999999999999999999999 for i in range(1, n): ans_temp = ( ans - li[(i - 1) % n][0] + max(0, li[(i - 1) % n][0] - li[(i + n - 2) % n][1]) + li[i][0] - max(li[i][0] - li[(i - 1) % n][1], 0) ) ans_final = min(ans, ans_temp, ans_final) ans = ans_temp print(ans_final) q = int(input()) for _ in range(q): solve() main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin case = int(stdin.buffer.readline()) for iiiiiiiii in range(case): n = int(stdin.buffer.readline()) a = [0] * n b = [0] * n c = [0] * n c_all = 0 ans = float("inf") for i in range(n): s = stdin.buffer.readline().split() a[i] = int(s[0]) b[i] = int(s[1]) for i in range(n): damage = a[i] - b[i - 1] if i > 0 else a[i] - b[-1] if damage < 0: damage = 0 c[i] = damage c_all += damage for i in range(n): bullet = c_all + a[i] - c[i] if bullet < ans: ans = bullet print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for i in range(t): n = int(input()) a = [] b = [] for j in range(n): a_, b_ = [int(s) for s in input().split()] a.append(a_) b.append(b_) min_ = 1000000000000 if b[-1] < a[0]: ans = a[0] - b[-1] a[0] = b[-1] else: ans = 0 if a[0] < min_: min_ = a[0] for j in range(len(a) - 1): if b[j] < a[j + 1]: ans += a[j + 1] - b[j] a[j + 1] = b[j] if a[j + 1] < min_: min_ = a[j + 1] print(ans + min_)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for f in range(t): n = int(input()) a = [0] * n b = [0] * n s1 = 0 s2 = 0 s3 = 100000000000000000 c = [0] * n b[-1] = s3 for i in range(n): a[i], b[i] = map(int, input().split()) s1 += a[i] c[i] = min(a[i], b[i - 1]) s3 = min(s3, c[i]) s2 += c[i] c[0] = min(a[0], b[-1]) s3 = min(s3, c[0]) s2 -= a[0] s2 += c[0] print(s3 + s1 - s2)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input, print = sys.stdin.buffer.readline, sys.stdout.write for _ in range(int(input())): n = int(input()) a1, b1 = map(int, input().split()) a, b = [a1], [0] s = 0 for _ in range(n - 1): a2, b2 = map(int, input().split()) a.append(a2) b.append(max(a2 - b1, 0)) a1, b1 = a2, b2 s += b[-1] b[0] = max(a[0] - b1, 0) s += b[0] print(str(min([(s + a[i] - b[i]) for i in range(n)])) + "\n")
IMPORT ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys from itertools import accumulate def input(): return sys.stdin.buffer.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 out = [] for _ in range(INT()): N = INT() AB = [] for i in range(N): a, b = MAP() AB.append((a, b)) need = [0] * N for i in range(N - 1, -1, -1): a, b = AB[i] na, nb = AB[(i + 1) % N] if b < na: need[(i + 1) % N] = na - b sm = sum(need) ans = INF for i, (a, b) in enumerate(AB): ans = min(ans, a + sm - need[i]) out.append(str(ans)) print("\n".join(out))
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline T = int(input()) for _ in range(T): n = int(input()) la, lb = [], [] for _ in range(n): a, b = map(int, input().split()) la.append(a) lb.append(b) sm, mn = 0, 0 for i in range(n): add = max(0, la[i] - lb[i - 1]) sm += add if not mn or la[i] - add < mn: mn = la[i] - add print(sm + mn)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n, best, best_i = int(input()), float("inf"), -1 vals = [[int(i) for i in input().split()] for j in range(n)] vals.append(vals[0]) for i in range(n): if vals[i + 1][0] - max(0, vals[i + 1][0] - vals[i][1]) < best: best = vals[i + 1][0] - max(0, vals[i + 1][0] - vals[i][1]) best_i = i + 1 new_vals, ans = [vals[(best_i + i) % n] for i in range(n)], 0 for i in range(n - 1): ans += max(new_vals[i][0], 0) new_vals[i + 1][0] -= new_vals[i][1] ans += max(new_vals[-1][0], 0) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for k in range(t): n = int(input()) a = [[int(el) for el in input().split()] for i in range(n)] s = 0 for i in range(n): s += max(0, a[i][0] - a[i - 1][1]) m = 10**20 for i in range(n): m = min(m, s + a[i][0] - max(0, a[i][0] - a[i - 1][1])) print(m)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) ans, x, y = 0, 10**13, (0, 0) a0, b0 = map(int, input().split()) for i in range(1, n): a, b = map(int, input().split()) k = max(0, a - b0) ans += k if a - k < x: x = a - k y = a, k b0 = b k = max(0, a0 - b0) ans += k if a0 - k < x: x = a0 - k y = a0, k ans += y[0] - y[1] print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys def main(): import sys input = sys.stdin.buffer.readline mod = 998244353 def GCD(x, y): while y: x, y = y, x % y return x def lcm(a, b): return a * b // GCD(a, b) def merge(a, start, mid, end, d): p = start q = mid + 1 arr = [0] * (end - start + 1) k = 0 coun = 0 for i in range(start, end + 1): if p > mid: arr[k] = a[q] q += 1 k += 1 elif q > end: arr[k] = a[p] d[a[p]] += coun k += 1 p += 1 elif a[p] < a[q]: arr[k] = a[p] d[a[p]] += coun k += 1 p += 1 else: arr[k] = a[q] k += 1 q += 1 coun += 1 for i in range(k): a[start] = arr[i] start += 1 def mergesort(a, start, end, d): if start < end: mid = (start + end) // 2 mergesort(a, start, mid, d) mergesort(a, mid + 1, end, d) merge(a, start, mid, end, d) def inversion(l): d = {} for i in l: d[i] = 0 mergesort(l, 0, len(l) - 1, d) return d def binary_search_lower(A, low, high, k): if high - low != 1: mid = (low + high) // 2 x = A[mid] if x >= k: return binary_search_lower(A, low, mid, k) else: return binary_search_lower(A, mid, high, k) if A[low] >= k: return low else: return high def binary_expo(x, n): if n == 0: return 1 elif n % 2 == 0: return binary_expo(x * x, n // 2) else: return x * binary_expo(x * x, (n - 1) // 2) def modu_expo(x, n, m): if n == 0: return 1 elif n % 2 == 0: return modu_expo(x * x % m, n // 2, m) else: return x * modu_expo(x * x % m, (n - 1) // 2, m) % m def eEuclid(c, m): if m == 0: global d global x global y d = c x = 1 y = 0 else: eEuclid(m, c % m) temp = x x = y y = temp - c // m * y def mod_Inv(c, m): eEuclid(c, m) return (x % m + m) % m def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p for _ in range(int(input())): n = int(input()) l = [] for i in range(n): a, b = map(int, input().split()) l.append([a, b]) ans = 0 for i in range(n - 1): if l[i][1] - l[i + 1][0] < 0: ans += l[i + 1][0] - l[i][1] l[i + 1][0] = l[i + 1][0] - (l[i + 1][0] - l[i][1]) if l[-1][1] - l[0][0] < 0: ans += l[0][0] - l[-1][1] l[0][0] = l[0][0] - (l[0][0] - l[-1][1]) min = 99999999999999 for i in range(n): if l[i][0] < min: min = l[i][0] print(ans + min) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def calc(i, sub): i = n - 1 while i >= 0: if i == n - 1: dp[i] = max(0, l[i][0] - l[i - 1][1]) i -= 1 continue if i == 0: dp[i] = l[i][0] + dp[i + 1] break if l[i - 1][1] < l[i][0]: dp[i] = l[i][0] - l[i - 1][1] + dp[i + 1] elif l[i - 1][1] >= l[i][0]: dp[i] = 0 + dp[i + 1] i -= 1 for nt in range(int(input())): n = int(input()) l = [] dp = [] for i in range(n): a, b = map(int, input().split()) l.append((a, b)) dp.append(0) calc(0, 0) last = max(0, l[0][0] - l[-1][1]) ans = dp[0] for i in range(1, n - 1): a = l[i][0] + dp[i + 1] + last + dp[1] - dp[i] ans = min(ans, a) ans = min(ans, l[-1][0] + last + dp[1] - dp[-1]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline T = int(input()) for t in range(T): n = int(input()) hp = [] bomb = [] bomb.append(0) for i in range(n): x, y = map(int, input().split()) hp.append(x) bomb.append(y) bomb[0] = bomb.pop(-1) for i in range(n): bomb[i] = min(bomb[i], hp[i]) print(sum(hp) - sum(bomb) + min(bomb))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline t = int(input()) for qw in range(t): n = int(input()) l1 = [] l = [] hoe = 0 cc = 0 pepe = 0 iff = 0 els = 0 l2 = [] counts = 0 for i in range(n): a, b = map(int, input().split()) l.append(a) if i != 0: count = bb - a if count < 0: l1.append(bb) iff = 1 counts += a - bb else: l2.append(a) els = 1 if i == 0: swag = a ver = b bb = b aa = a count = b - swag if count < 0: l1.append(b) iff = 1 counts += swag - b else: l2.append(swag) els = 1 if iff != 0 and els != 0: ww = min(l1) www = min(l2) if ww < www: ans = counts + ww else: ans = www + counts else: if iff == 0: www = min(l2) ans = www if els == 0: ww = min(l1) ans = ww + counts print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline T = int(input()) for _ in range(T): n = int(input()) A = [] for i in range(n): a, b = map(int, input().split()) A.append([a, b]) if i == 0: fb = A[0][0] else: fb = fb + max(A[i][0] - A[i - 1][1], 0) fa = fb for i in range(1, n): fb = ( fb - A[i - 1][0] - max(A[i][0] - A[i - 1][1], 0) + max(A[i - 1][0] - A[i - 2][1], 0) + A[i][0] ) if fb < fa: fa = fb print(fa)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def main(): t = int(input()) for _ in range(t): n = int(input()) A = [0] * n B = [0] * n for i in range(n): a, b = map(int, input().split()) A[i] = a B[i] = b R = [0] * n for i in range(n): if i != 0: if A[i] - B[i - 1] < 0: R[i] = 0 else: R[i] = A[i] - B[i - 1] elif A[i] - B[n - 1] < 0: R[i] = 0 else: R[i] = A[i] - B[n - 1] s = sum(R) ans = 10**19 for i in range(n): if ans >= s - R[i] + A[i]: ans = s - R[i] + A[i] print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
input = __import__("sys").stdin.buffer.readline t = int(input()) output = [] for tc in range(t): n = int(input()) a = [(0) for i in range(n)] b = [(0) for i in range(n)] for i in range(n): a[i], b[i] = map(int, input().split()) a += a b += b p, xp = 0, min(b[0], a[1]) for i in range(1, n): xp2 = min(b[i], a[i + 1]) if xp2 < xp: p, xp = i, xp2 sol = 0 p += 1 for k in range(p, p + n): i = k j = k + 1 sol += a[i] if j < n + n: a[j] -= min(a[j], b[i]) output.append(str(sol)) print("\n".join(output))
ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys I = sys.stdin.buffer.readline input = lambda: stdin.readline() t = int(I()) while t > 0: n = int(I()) add = 1000000000006 ans = 0 a0, b0 = map(int, I().split()) for i in range(n - 1): a, b = map(int, I().split()) ans += max(0, a - b0) add = min(add, min(a, b0)) b0 = b ans += max(0, a0 - b0) add = min(add, min(a0, b0)) print(ans + add) t -= 1
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline def prog(): for _ in range(int(input())): n = int(input()) circle = [list(map(int, input().split())) for i in range(n)] shot = 0 for i in range(n): curr = circle[i] j = (i - 1) % n previous = circle[j][1] if curr[0] > previous: shot += curr[0] - previous curr[0] = previous shot += min(circle, key=lambda x: x[0])[0] sys.stdout.write(str(shot) + "\n") prog()
IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) A = [0] * n B = [0] * n for i in range(n): a, b = map(int, input().split()) A[i] = a B[i] = b v = 10**13 ans = 0 for i in range(n): e = max(0, A[i] - B[i - 1]) v = min(v, A[i] - e) ans += e ans += v sys.stdout.write(str(ans) + "\n")
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
You are playing another computer game, and now you have to slay $n$ monsters. These monsters are standing in a circle, numbered clockwise from $1$ to $n$. Initially, the $i$-th monster has $a_i$ health. You may shoot the monsters to kill them. Each shot requires exactly one bullet and decreases the health of the targeted monster by $1$ (deals $1$ damage to it). Furthermore, when the health of some monster $i$ becomes $0$ or less than $0$, it dies and explodes, dealing $b_i$ damage to the next monster (monster $i + 1$, if $i < n$, or monster $1$, if $i = n$). If the next monster is already dead, then nothing happens. If the explosion kills the next monster, it explodes too, damaging the monster after it and possibly triggering another explosion, and so on. You have to calculate the minimum number of bullets you have to fire to kill all $n$ monsters in the circle. -----Input----- The first line contains one integer $T$ ($1 \le T \le 150000$) β€” the number of test cases. Then the test cases follow, each test case begins with a line containing one integer $n$ ($2 \le n \le 300000$) β€” the number of monsters. Then $n$ lines follow, each containing two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 10^{12}$) β€” the parameters of the $i$-th monster in the circle. It is guaranteed that the total number of monsters in all test cases does not exceed $300000$. -----Output----- For each test case, print one integer β€” the minimum number of bullets you have to fire to kill all of the monsters. -----Examples----- Input 1 3 7 15 2 14 5 3 Output 6 -----Note----- None
from sys import stdin, stdout def main(): t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) big = 1000000000001 answer = 0 a_init, b = map(int, stdin.readline().split()) for _ in range(n - 1): a, b_new = map(int, stdin.readline().split()) raz = min(a, b) if raz < big: big = raz answer += max(a - b, 0) b = b_new a = a_init raz = min(a, b) if raz < big: big = raz answer += max(a - b, 0) stdout.write("%d\n" % (answer + big)) stdout.flush() main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
n = input() a = n[:] p = len(n) - 1 count1, count2 = 0, 0 i = p flag1, flag2 = 0, 0 while i > -1: if n[i] == "5" and flag1 == 0: count1 = p - i n = n[:i] + n[i + 1 :] + "5" flag1 = 1 if a[i] == "0" and flag2 == 0: count2 = p - i a = a[:i] + a[i + 1 :] + "0" flag2 = 1 i = i - 1 if flag1 == 1 or flag2 == 1: i = p - 1 if flag1 == 1: flag1 = -1 if flag2 == 1: flag2 = -1 while i > -1: if (int(n[i]) - 2) % 5 == 0 and flag1 == -1: count1 = count1 + p - i - 1 flag1 = 2 if int(a[i]) % 5 == 0 and flag2 == -1: count2 = count2 + p - i - 1 flag2 = 2 i = i - 1 if flag1 == 2 and flag2 == 2: if n[0] == "0": i = 0 while n[i] == "0" and i < p - 1: count1 = count1 + 1 i = i + 1 if i == p - 1: count1 = count2 print(min(count1, count2)) elif flag1 == 2 and flag2 != 2: print(count1) elif flag1 != 2 and flag2 == 2: print(count2) else: print(-1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def func(l, x, y): ans = 0 ll = l.copy() i = ll.index(x) ans = ans + i ll = [ll[i]] + ll[:i] + ll[i + 1 :] i = ll.index(y, 1) ans = ans + (i - 1) ll = ll[:1] + [ll[i]] + ll[1:i] + ll[i + 1 :] i = len(ll) - 1 while ll[i] == 0: i = i - 1 ans = ans + len(l) - 1 - i ll = ll[:i] + ll[i + 1 :] + [ll[i]] if (10 * ll[1] + ll[0]) % 25 == 0: return ans else: return -1 l = [int(i) for i in list(input())] l = l[::-1] ans = 1000000 if l.count(0) >= 2: x = func(l, 0, 0) if x != -1: ans = min(x, ans) if l.count(0) >= 1 and l.count(5) >= 1: x = func(l, 0, 5) if x != -1: ans = min(x, ans) if l.count(5) >= 1 and l.count(2) >= 1: x = func(l, 5, 2) if x != -1: ans = min(x, ans) if l.count(5) >= 1 and l.count(7) >= 1: x = func(l, 5, 7) if x != -1: ans = min(x, ans) if ans == 1000000: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER LIST VAR VAR IF BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def finder(n, ind): i = ind - 1 while i >= 0: if n[i] == "0": break else: i = i - 1 return i def zeros(n): count = 0 pol = 1 while pol < l: if n[pol] == "0": count = count + 1 else: break pol = pol + 1 return count n = input() ans = [] l = len(n) if "2" in n and "5" in n: c2 = l - n.rfind("2") - 2 c5 = l - n.rfind("5") - 1 if c2 < c5: k = c2 + c5 + 1 else: k = c2 + c5 if n[1] == "0" and (n[0] == "2" or n[0] == "5"): ans.append(k + zeros(n)) else: ans.append(k) if "5" in n and "0" in n: c5 = l - n.rfind("5") - 2 c0 = l - n.rfind("0") - 1 if c5 < c0: k = c5 + c0 + 1 else: k = c5 + c0 ans.append(k) if "7" in n and "5" in n: c7 = l - n.rfind("7") - 2 c5 = l - n.rfind("5") - 1 if c7 < c5: k = c7 + c5 + 1 else: k = c7 + c5 if n[1] == "0" and (n[0] == "7" or n[0] == "5"): ans.append(k + zeros(n)) else: ans.append(k) if n.count("0") >= 2: c01 = l - n.rfind("0") - 2 c02 = l - finder(n, n.rfind("0")) - 1 ans.append(c01 + c02) if int(n) % 25 == 0: print(0) elif len(ans) == 0: print(-1) else: print(min(ans))
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
INF = 1e18 def replace_zero(s, n): for i in range(1, n + 1): if s[i] != "0": return i - 1 return None def find_number(c, s, end=1): n = len(s) - end cnt = 0 for i in range(n, -1, -1): if s[i] == c: if i == 0 and s[i + 1] == "0": cnt = replace_zero(s, n) if cnt is None: return None, s return n - i + cnt, s[:i] + s[i + 1 :] + s[i] return None, s def make_integer(c1, c2, s): if c1 + c2 == s[len(s) - 2 :]: return 0 cnt1, s = find_number(c2, s) if cnt1 is None: return INF cnt2, s = find_number(c1, s, 2) if cnt2 is None: return INF return cnt1 + cnt2 def main(): n = input() ans = min( make_integer("0", "0", n), make_integer("2", "5", n), make_integer("5", "0", n), make_integer("7", "5", n), ) if ans == INF: ans = -1 print(ans) main()
ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING RETURN BIN_OP VAR NUMBER RETURN NONE FUNC_DEF NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN NONE VAR RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NONE VAR FUNC_DEF IF BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NONE RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE RETURN VAR RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING STRING VAR FUNC_CALL VAR STRING STRING VAR FUNC_CALL VAR STRING STRING VAR FUNC_CALL VAR STRING STRING VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
INF = 1000 def compute_pair(s, pair): n = len(s) try: l2 = s.rindex(pair[1]) l1 = s.rindex(pair[0]) if pair[0] == "0": l1 = s[:l2].rindex(pair[1]) except: return INF steps = abs(n - 2 - l1) + abs(n - 1 - l2) if l1 > l2 and l1 != n - 1: steps += 1 if l1 == n - 1: steps -= 1 l1, l2 = min(l1, l2), max(l1, l2) new_s = s[:l1] + s[l1 + 1 : l2] + s[l2 + 1 :] if new_s.startswith("0"): for i, q in enumerate(new_s): if q != "0": steps += i break return steps def main(): s = input() best = INF if int(s) < 10: print(-1) return for pair in ["00", "25", "50", "75"]: l = compute_pair(s, pair) if l < best: best = l if best == INF: best = -1 print(best) main()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
n = list(input()) ends = ["00", "25", "50", "75"] res = -1 def can_be_result(indeces): if -1 in indeces: return False t = [x for i, x in enumerate(n) if i not in indeces] if len(t) == 0: return True if len([x for x in t if x != "0"]) == 0: return False return True for end in ends: indeces = [(-1) for _ in end] for i in reversed(range(len(n))): for j in reversed(range(len(end))): if indeces[j] == -1 and n[i] == end[j]: indeces[j] = i break if not can_be_result(indeces): continue temp = [x for x in n] preres = 0 while len([(0) for i, x in enumerate(reversed(end)) if temp[-(i + 1)] == x]) != len( end ): for i in reversed(range(len(indeces))): if indeces[i] != len(n) - len(end) + i: left = indeces[i] right = left + 1 if left == 0: while temp[right] == "0": left += 1 right += 1 temp[left], temp[right] = temp[right], temp[left] indeces = [ (x + 1 if x == left else x - 1 if x == right else x) for i, x in enumerate(indeces) ] preres += 1 break if res == -1 or preres < res: res = preres print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR NUMBER FUNC_DEF IF NUMBER VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER RETURN NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
maxint = 2147483647 class mylist(list): def index(self, obj, *args): try: if len(args) == 0: return super(mylist, self).index(obj) if len(args) == 1: return super(mylist, self).index(obj, args[0]) return super(mylist, self).index(obj, args[0], args[1]) except ValueError: return -1 def cost(target, index1, index2): if index1 == -1 or index2 == -1: return maxint if target == "00": return index1 + index2 - 1 else: return index1 + index2 - 1 if index2 < index1 else index1 + index2 def calcCosts(rn): i0 = rn.index("0") i00 = rn.index("0", i0 + 1) i2 = rn.index("2") i5 = rn.index("5") i7 = rn.index("7") costs = [ ("00", cost("00", i0, i00)), ("50", cost("50", i5, i0)), ("25", cost("25", i2, i5)), ("75", cost("75", i7, i5)), ] costs.sort(key=lambda tup: tup[1]) return costs def swapCritical(rn, i): if i == len(rn) - 1: for index in range(i - 1, -1, -1): if rn[index] != "0": break else: index = i - 1 rn[index], rn[index + 1] = rn[index + 1], rn[index] return rn def swap(rn, typ): if typ == "00": i0 = rn.index("0") i00 = rn.index("0", i0 + 1) if i0 != 0: rn[i0 - 1], rn[i0] = rn[i0], rn[i0 - 1] return rn, 1 if i00 == 1: return rn, 0 rn[i00 - 1], rn[i00] = rn[i00], rn[i00 - 1] return rn, 1 if typ == "50": i0 = rn.index("0") i5 = rn.index("5") if i0 != 0: if i0 < i5 or i5 == 0: rn[i0 - 1], rn[i0] = rn[i0], rn[i0 - 1] return rn, 1 rn[i5 - 1], rn[i5] = rn[i5], rn[i5 - 1] return rn, 1 if i5 == 1: return rn, 0 if i5 != len(rn) - 1: rn[i5 - 1], rn[i5] = rn[i5], rn[i5 - 1] return rn, 1 rn = swapCritical(rn, i5) return rn, 1 if typ == "25": i2 = rn.index("2") i5 = rn.index("5") if i5 != 0: if i5 < i2 or i2 == 0: swapCritical(rn, i5) return rn, 1 rn[i2 - 1], rn[i2] = rn[i2], rn[i2 - 1] return rn, 1 if i2 == 1: return rn, 0 if i2 != len(rn) - 1: rn[i2 - 1], rn[i2] = rn[i2], rn[i2 - 1] return rn, 1 rn = swapCritical(rn, i2) return rn, 1 if typ == "75": i7 = rn.index("7") i5 = rn.index("5") if i5 != 0: if i5 < i7 or i7 == 0: swapCritical(rn, i5) return rn, 1 rn[i7 - 1], rn[i7] = rn[i7], rn[i7 - 1] return rn, 1 if i7 == 1: return rn, 0 if i7 != len(rn) - 1: rn[i7 - 1], rn[i7] = rn[i7], rn[i7 - 1] return rn, 1 rn = swapCritical(rn, i7) return rn, 1 def realCost(rn, maximum): costs = calcCosts(rn) bestCost = maximum for cost in costs: if cost[1] >= bestCost: return bestCost newrn = mylist(rn) newrn, tempcost = swap(newrn, cost[0]) if tempcost == 0: return 0 thiscost = tempcost + realCost(newrn, bestCost - 1) if thiscost < bestCost: bestCost = thiscost n = input() rn = n[::-1] rn = mylist(rn) rcost = realCost(rn, maxint) print(rcost if rcost < maxint else -1)
ASSIGN VAR NUMBER CLASS_DEF VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR RETURN NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR STRING RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST STRING FUNC_CALL VAR STRING VAR VAR STRING FUNC_CALL VAR STRING VAR VAR STRING FUNC_CALL VAR STRING VAR VAR STRING FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER IF VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def swap(N, a, b): A = N[a] B = N[b] N[a] = B N[b] = A def getToEnd(N, a, b): bInd = -1 for i in range(len(N) - 1, -1, -1): if N[i] == b: bInd = i break total = 0 if bInd == -1: return q for i in range(bInd, len(N) - 1): total += 1 swap(N, i, i + 1) aInd = -1 for i in range(len(N) - 2, -1, -1): if N[i] == a: aInd = i break if aInd == -1: return q for i in range(aInd, len(N) - 2): total += 1 swap(N, i, i + 1) if N[0] == "0": worked = False for i in range(len(N) - 2): if N[i] == "0": total += 1 else: worked = True break if not worked: return q return total q = 999999 N = list(input()) a = getToEnd(list(N), "0", "0") b = getToEnd(list(N), "2", "5") c = getToEnd(list(N), "5", "0") d = getToEnd(list(N), "7", "5") if a == q and b == q and c == q and d == q: print(-1) else: print(min(a, b, c, d))
FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING IF VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
s = input() n = len(s) def i(c, o=0): try: return s[::-1].index(str(c), o) except: return -1 oo = 0 while oo < n - 1 and s[oo + 1] == "0": oo += 1 o5 = i(5) o7 = i(7) o2 = i(2) o01 = i(0) o02 = i(0, o01 + 1) if o01 >= 0 else -1 r = 100 if o01 >= 0 and o02 >= 0: r = min(r, o01 + o02 - 1) if o01 >= 0 and o5 >= 0: r = min(r, o01 + o5 - int(o01 < o5)) if o5 >= 0 and o2 >= 0: r = min(r, o5 + o2 - int(o5 < o2) + int(o5 == n - 1 and oo)) if o5 >= 0 and o7 >= 0: r = min(r, o5 + o7 - int(o5 < o7) + int(o5 == n - 1 and oo)) print(r if r < 100 else -1)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def solve1(): n = m try: x = n.index("0") n = n[x] + n[:x] + n[x + 1 :] x += len(n) - len(n.rstrip("0")) y = n.index("5") return x + y - 1 except: return float("inf") def solve2(): n = m try: x = n.index("0") y = n.find("0", x + 1) if y == -1: raise Exception return x + y - 1 except: return float("inf") def solve3(): n = m try: x = n.index("5") n = n[x] + n[:x] + n[x + 1 :] x += len(n) - len(n.rstrip("0")) y = n.index("2") return x + y - 1 except: return float("inf") def solve4(): n = m try: x = n.index("5") n = n[x] + n[:x] + n[x + 1 :] x += len(n) - len(n.rstrip("0")) y = n.index("7") return x + y - 1 except: return float("inf") m = input()[::-1] x = min(solve1(), solve2(), solve3(), solve4()) if x == float("inf"): print(-1) else: print(x)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING RETURN BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
import sys n = input() num = ["25", "50", "75", "00"] minn = sys.maxsize step = 0 length = len(n) for x in num: if x[0] == x[1] and n.count("0") > 1: first = n.rfind(x[0]) second = n[:first].rfind(x[1]) step = length - 1 - second + length - 2 - first minn = min(step, minn) elif x[0] != x[1] and x[0] in n and x[1] in n: i = 1 first = n.rfind(x[0]) second = n.rfind(x[1]) step = length - 1 - second + length - 2 - first if second == 0: while n[i] == "0": step += 1 i += 1 if second < first: step += 1 minn = min(minn, step) if minn == sys.maxsize: print("-1") else: print(minn)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
n = input()[::-1] c0 = n.count("0") c2 = n.count("2") c5 = n.count("5") c7 = n.count("7") s = 1000000000.0 if c0 > 1: i = n.find("0") j = n.find("0", i + 1) s = min(s, i + j - 1) if c2 and c5: i = n.find("2") j = n.find("5") r = j + i - 1 + (j > i) i, j = sorted([i, j]) m = n[:i] + n[i + 1 : j] + n[j + 1 :] if not m: s = min(s, r) elif int(m) == 0: pass else: d = -1 while m[d] == "0": d -= 1 s = min(s, r - d - 1) if c5 and c0: i = n.find("5") j = n.find("0") s = min(s, j + i - 1 + (j > i)) if c7 and c5: i = n.find("7") j = n.find("5") r = j + i - 1 + (j > i) i, j = sorted([i, j]) m = n[:i] + n[i + 1 : j] + n[j + 1 :] if not m: s = min(s, r) elif int(m) == 0: pass else: d = -1 while m[d] == "0": d -= 1 s = min(s, r - d - 1) print(-1 if s == 1000000000.0 else s)
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
n = input() ans = 10000 n1 = list(n) n2 = list(n) l = len(n2) curr = 0 for i in range(l - 1, -1, -1): if n1[i] == "0": curr += l - 1 - i for j in range(i, l - 1): n1[j], n1[j + 1] = n1[j + 1], n1[j] for j in range(l - 2, -1, -1): if n1[j] == "5" or n1[j] == "0": curr += l - 2 - j ans = min(ans, curr) break else: continue break curr = 0 for i in range(l - 1, -1, -1): if n2[i] == "5": curr += l - 1 - i if i == 0 and n2[1] == "0": for j in range(1, l): if n2[j] != "0": curr += j - 1 n2[1], n2[j] = n2[j], n2[1] break else: break for j in range(i, l - 1): n2[j], n2[j + 1] = n2[j + 1], n2[j] for j in range(l - 2, -1, -1): if n2[j] == "2" or n2[j] == "7": curr += l - 2 - j ans = min(ans, curr) break else: continue break if ans > 1000: ans = -1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
n = input().rstrip() l = len(n) c0 = 0 ii = 1 while ii < l and n[ii] == "0": c0 += 1 ii += 1 ans = 10**9 if n.count("0") >= 2: idx = [] for i in range(l)[::-1]: if n[i] == "0": idx.append(l - 1 - i) if len(idx) >= 2: break c00 = idx[0] + idx[1] - 1 ans = min(ans, c00) if n.count("0") and n.count("5"): c50 = 10**9 idx0 = [] idx5 = [] for i in range(l)[::-1]: if n[i] == "0": idx0.append(l - 1 - i) if n[i] == "5": idx5.append(l - 1 - i) for i0 in idx0: for i5 in idx5: if i5 > i0: c50 = min(c50, i0 + i5 - 1) else: c50 = min(c50, i5 + i0) ans = min(ans, c50) if n.count("7") and n.count("5"): c75 = 10**9 idx7 = [] idx5 = [] for i in range(l)[::-1]: if n[i] == "7": idx7.append(l - 1 - i) if n[i] == "5": idx5.append(l - 1 - i) for i7 in idx7: for i5 in idx5: if i7 > i5: if i7 == l - 1: c75 = min(c75, i7 + i5 - 1 + c0) else: c75 = min(c75, i7 + i5 - 1) elif i5 == l - 1: c75 = min(c75, i7 + i5 + c0) else: c75 = min(c75, i7 + i5) ans = min(ans, c75) if n.count("2") and n.count("5"): c25 = 10**9 idx2 = [] idx5 = [] for i in range(l)[::-1]: if n[i] == "2": idx2.append(l - 1 - i) if n[i] == "5": idx5.append(l - 1 - i) for i2 in idx2: for i5 in idx5: if i2 > i5: if i2 == l - 1: c25 = min(c25, i2 + i5 - 1 + c0) else: c25 = min(c25, i2 + i5 - 1) elif i5 == l - 1: c25 = min(c25, i2 + i5 + c0) else: c25 = min(c25, i2 + i5) ans = min(ans, c25) if ans == 10**9: print(-1) else: print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR FOR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR VAR FOR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
z = input() n = len(z) maxa = 10**18 y = int(z) if y % 25 == 0: print(0) quit() if n == 1: print(-1) quit() for i in range(n): for j in range(n): if i == j: continue copy = list(z) swaps = 0 for k in range(i, n - 1): copy[k], copy[k + 1] = copy[k + 1], copy[k] swaps += 1 if j > i: j -= 1 for k in range(j, n - 2): copy[k], copy[k + 1] = copy[k + 1], copy[k] swaps += 1 if (10 * (ord(copy[n - 2]) - 48) + (ord(copy[n - 1]) - 48)) % 25: continue if copy[0] == "0": done = 0 for yo in range(n - 2): if copy[yo] != "0": done = 1 swaps += yo break if done: maxa = min(maxa, swaps) else: continue maxa = min(maxa, swaps) if maxa == 10**18: print(-1) else: print(maxa)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR NUMBER VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
inf = float("inf") def find_right(s, c, j=-1): for i in range(len(s) - 1, -1, -1): if i != j and s[i] == c: return i return inf def swaps(s, c1, c2): if len(s) > 1 and s[-2:] == c1 + c2: return 0 if len(s) > 1 and s[-2:] == c2 + c1: return 1 i2 = find_right(s, c2) if i2 == inf: return inf i1 = find_right(s, c1, i2) if i1 == inf: return inf ans = 0 if i2 < i1: ans += abs(len(s) - 2 - (i1 - 1)) + abs(len(s) - 1 - i2) else: ans += abs(len(s) - 2 - i1) + abs(len(s) - 1 - i2) s0 = "".join([si for i, si in enumerate(s) if i != i1 and i != i2]) for i in range(len(s0)): if s0[i] != "0": ans += i break else: return inf return ans n = input().strip() ans = min( swaps(n, "0", "0"), swaps(n, "2", "5"), swaps(n, "5", "0"), swaps(n, "7", "5") ) print(ans if ans != inf else -1)
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
INF = float("inf") def count_min_steps(num_str): result = INF for i, c1 in enumerate(num_str): suffix = num_str[:i] + num_str[i + 1 :] for j, c2 in enumerate(suffix): swap_str = suffix[:j] + suffix[j + 1 :] + c2 + c1 if swap_str[0] != "0" and int(swap_str) % 25 == 0: result = min(result, len(num_str) + len(suffix) - i - j - 2) return result def solve(num_str): result = INF for i, c in enumerate(num_str): swap_str = num_str[i] + num_str[:i] + num_str[i + 1 :] result = min(result, count_min_steps(swap_str) + i) return result number_str = input() result = solve(number_str) print(-1 if result == INF else result)
ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER STRING BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def check(): res = 0 if cnt[0] > 1: res += 1 if cnt[0] > 0 and cnt[5] > 0: res += 2 if cnt[5] > 0 and (cnt[2] > 0 or cnt[7] > 0): res += 4 return res num = int(input()) cnt = [] str = [] for i in range(10): cnt.append(0) while num != 0: i = num % 10 str.append(i) cnt[i] += 1 num //= 10 k = check() ans = 40 if k == 0: print("-1") if k & 1 == 1: tmp = index = 0 cok = 2 for i in str: if i == 0: tmp += index cok -= 1 index += 1 if cok == 0: break ans = min(ans, tmp - 1) if k & 2 == 2: index = 0 zero = five = -1 for i in str: if i == 0 and zero == -1: zero = index elif i == 5 and five == -1: five = index index += 1 if zero < five: ans = min(ans, zero + five - 1) else: ans = min(ans, zero + five) if k & 4 == 4: index = 0 five = tos = -1 for i in str: if i == 5 and five == -1: five = index elif tos == -1 and (i == 2 or i == 7): tos = index index += 1 if five < tos: ans = min(ans, five + tos - 1) elif five == len(str) - 1: index = five - 1 while index >= 0 and (index == tos or str[index] == 0): index -= 1 else: if index == five - 1: ans = min(ans, five + tos) elif index != -1: tmp = five - index five -= 1 if index < tos: tos -= 1 tmp += five + tos ans = min(ans, tmp) elif len(str) == 2: ans = 1 else: ans = min(ans, five + tos) if k != 0: print(ans)
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
import sys k = input() Min = sys.maxsize if "5" in k and "0" in k: temp = len(k) - 2 - k.rfind("5") + (len(k) - 1) - k.rfind("0") if k.rfind("5") > k.rfind("0"): temp += 1 if temp < Min: Min = temp if "2" in k and "5" in k: temp = len(k) - 2 - k.rfind("2") + (len(k) - 1) - k.rfind("5") if k.rfind("5") == 0 and k[1] == "0" and len(k) > 3: add = 0 i = 1 while k[i] == "0": add += 1 i += 1 temp += add if k.rfind("2") > k.rfind("5"): temp += 1 if temp < Min: Min = temp if "7" in k and "5" in k: temp = len(k) - 2 - k.rfind("7") + (len(k) - 1) - k.rfind("5") if k.rfind("5") == 0 and k[1] == "0" and len(k) > 3: add = 0 i = 1 while k[i] == "0": add += 1 i += 1 temp += add if k.rfind("7") > k.rfind("5"): temp += 1 if temp < Min: Min = temp if k.count("0") > 1: css = k.rfind("0") temp = len(k) - 1 - css k = k[:css] + k[css + 1 :] temp += len(k) - 1 - k.rfind("0") if temp < Min: Min = temp if Min == sys.maxsize: print(-1) else: print(Min)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF STRING VAR STRING VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR STRING NUMBER VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def g(qw, a, i=0): return a.find(str(qw), i) + 1 or 50 def main(): a = input()[::-1] s = len(a) z, r = g(0, a), g(5, a) x = min(g(0, a, z), r) q = z + x + (x < z) x = min(g(2, a), g(7, a)) c = r + x + (x < r) if r == s: while a[r - 2] == "0": r -= 1 c += s - r q = min(q, c) print((-1, q - 3)[q < 40]) main()
FUNC_DEF NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
s = input()[::-1] m = I = 41 f = s.find("5") + 1 i = s.find("0") + 1 t = len(s) if i: j = min(s.find("0", i) + 1 or I, f or I) if j < I: m = i + j - 3 if j < i: m += 1 if f: j = min(s.find("2") + 1 or I, s.find("7") + 1 or I) if j < I: l = f + j - 3 if j < f: l += 1 if f == t: i = t - 1 while s[i - 1] == "0": l += 1 i -= 1 m = min(m, l) print((-1, m)[m < I])
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING NUMBER VAR BIN_OP FUNC_CALL VAR STRING NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
num = str(input()) endings = [[0, 0], [5, 2], [0, 5], [5, 7]] lenght = len(num) candidate = [] if lenght == 2: if [int(num[1]), int(num[0])] in endings: print(0) elif [int(num[0]), int(num[1])] in endings: print(1) else: print(-1) exit() for tup in endings: poss = [] ind = [] leftCheck = [i for i in tup] count = 0 for i in tup: for j in range(lenght - 1, -1, -1): index = lenght - j - 1 if str(i) == num[j] and j not in ind and int(num[j]) in leftCheck: if len(ind): if str(tup[1]) == num[j] and j < ind[0]: index -= 1 ind.append(j) poss.append(index) del leftCheck[leftCheck.index(i)] count += 1 if count == 2: s = sum(poss) if 0 in ind: i = 1 while num[i] == "0" and i not in ind: s += 1 i += 1 candidate.append(s) break if len(candidate): print(min(candidate)) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER IF LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
num = input()[::-1] case1 = False case2 = False case3 = False case4 = False zero1 = num.find("0") zero2 = num.find("0", num.find("0") + 1) five = num.find("5") seven = num.find("7") two = num.find("2") if zero1 != -1: if five != -1: case1 = True if five != -1: if two != -1: case2 = True if zero1 != -1: if zero2 != -1: case3 = True if five != -1: if seven != -1: case4 = True cases = [] if case1: cases.append([zero1, five]) if case2: cases.append([five, two]) if case3: cases.append([zero1, zero2]) if case4: cases.append([five, seven]) def score(pair): i = 0 line = num[::-1] while ( i < len(num) - 2 and line.replace(str(num[pair[0]]), "").replace(str(num[pair[1]]), "")[i] == "0" ): i += 1 if pair[0] < pair[1]: return pair[0] + pair[1] - 1 + i else: return pair[0] + pair[1] + i if cases: print(min([score(p) for p in cases])) else: print(-1)
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER STRING VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def go(): s = input() n = len(s) if "5" not in s and "0" not in s: return -1 k = ["00", "25", "50", "75"] m = float("inf") for x in k: if x == "00" and s.count("0") > 1: first = s.rfind("0") second = s[:first].rfind("0") d = n - 1 - first + n - 2 - second m = min(m, d) elif x != "00" and x[0] in s and x[1] in s: first = s.rfind(x[0]) second = s.rfind(x[1]) d = n - 1 - second + n - 2 - first if second == 0: i = 1 while s[i] == "0": i += 1 d += 1 if second < first: d += 1 m = min(m, d) if float("inf") == m: return -1 return m print(go())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR STRING VAR RETURN NUMBER ASSIGN VAR LIST STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR STRING FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR STRING VAR RETURN NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
s = input().strip() def find(a, b): if a not in s or b not in s: return 40 j = s.rindex(b) i = -1 for t, v in enumerate(s): if t != j and v == a: i = t if i < 0: return 40 o = 0 if not (i and j) and len(s) > 2: for t in range(len(s)): if t != i and t != j: if s[t] == "0": o += 1 else: break return (i > j) + 2 * len(s) - i - j - 3 + o t = min(find("0", "0"), find("2", "5"), find("5", "0"), find("7", "5")) print(t if t < 40 else -1)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
s = input()[::-1] l = len(s) def f(c, i=0): return s.find(c, i) + 1 or 50 i0_1 = f("0") i5 = f("5") i05 = min(f("0", i0_1), i5) m1 = i0_1 + i05 + (i05 < i0_1) - 3 i27 = min(f("2"), f("7")) m2 = i5 + i27 + (i27 < i5) - 3 if l == i5: while s[i5 - 2] == "0": i5 -= 1 m2 += l - i5 m = min(m1, m2) print((-1, m)[m < 40])
ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def go(n, c1, c2): l = len(n) pos2 = n.rfind(c2) pos1 = n.rfind(c1) if c1 == c2: pos1 = n[:pos2].rfind(c1) if pos1 == -1 or pos2 == -1: return 10**10 nxt = 0 if min(pos1, pos2) == 0: nxt = 1 if max(pos1, pos2) == 1: nxt = 2 ans = 0 if nxt != l: while nxt < l and n[nxt] == "0": ans += 1 nxt += 1 if nxt == l: return 10**10 ans += l - pos2 - 1 if pos1 > pos2: pos1 -= 1 ans += l - pos1 - 2 return ans n = input() ans = min([go(n, "2", "5"), go(n, "0", "0"), go(n, "7", "5"), go(n, "5", "0")]) if ans == 10**10: print(-1) else: print(ans)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR RETURN BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING FUNC_CALL VAR VAR STRING STRING IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an integer $n$ from $1$ to $10^{18}$ without leading zeroes. In one move you can swap any two adjacent digits in the given number in such a way that the resulting number will not contain leading zeroes. In other words, after each move the number you have cannot contain any leading zeroes. What is the minimum number of moves you have to make to obtain a number that is divisible by $25$? Print -1 if it is impossible to obtain a number that is divisible by $25$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^{18}$). It is guaranteed that the first (left) digit of the number $n$ is not a zero. -----Output----- If it is impossible to obtain a number that is divisible by $25$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. -----Examples----- Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 -----Note----- In the first example one of the possible sequences of moves is 5071 $\rightarrow$ 5701 $\rightarrow$ 7501 $\rightarrow$ 7510 $\rightarrow$ 7150.
def main(): ar = list(input()) size = len(ar) ans = 100000.0 for i in range(size): for j in range(size): if i == j: continue s = list(ar) cost = 0 for k in range(i, size - 1): tmp = s[k] s[k] = s[k + 1] s[k + 1] = tmp cost += 1 if i < j: for k in range(j - 1, size - 2): tmp = s[k] s[k] = s[k + 1] s[k + 1] = tmp cost += 1 else: for k in range(j, size - 2): tmp = s[k] s[k] = s[k + 1] s[k + 1] = tmp cost += 1 pos = -1 for k in range(size): if s[k] != "0": pos = k break for k in range(pos, 0, -1): tmp = s[k] s[k] = s[k - 1] s[k - 1] = tmp cost += 1 curr_num = int("".join(s)) if curr_num % 25 == 0: ans = min(ans, cost) if ans == 100000.0: print(-1) else: print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR