Circle of Monsters (Codeforces 1334C)
Tags: Greedy, brute force

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.


Sample Inputs:
1
3
7 15
2 14
5 3

Sample Outputs:
6

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
import sys
input = sys.stdin.readline

def readList():
    return list(map(int, input().split()))

def readInt():
    return int(input())

def readInts():
    return map(int, input().split())

def readStr():
    return input().strip()

def solve():
    n = readInt()
    arr = [readList() for _ in range(n)]
    (S, v) = (0, pow(10, 12))
    for i in range(n):
        S += max(arr[i][0] - arr[i - 1][1], 0)
        v = min(v, arr[i - 1][1]) if arr[i - 1][1] < arr[i][0] else min(v, arr[i][0])
    return S + v
for _ in range(int(input())):
    print(solve())