-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1334C.cpp
More file actions
executable file
·40 lines (34 loc) · 846 Bytes
/
1334C.cpp
File metadata and controls
executable file
·40 lines (34 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Problem Code: 1334C
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
using namespace std;
long long circle_of_monsters(int n, vector<long long>& a, vector<long long>& b) {
long long shots, mn, damage;
shots = 0;
mn = numeric_limits<long long>::max();
for (int i = 0; i < n; i++) {
int next = (i + 1) % n;
damage = min(a[next], b[i]); // a - max(0, a - b) => min(a, b)
mn = min(damage, mn);
shots += a[next] - damage; // max(0, a - b) => a - min(a, b)
}
shots += mn; // [a - min(a, b)] + min(a, b) => a
return shots;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<long long> a(n), b(n);
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
cout << circle_of_monsters(n, a, b) << '\n';
}
return 0;
}