Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int int64_t
- using namespace std;
- deque <int> a, b, c;
- int get(int l, int r){
- if(r < l) return 0LL;
- if(l == 0) return c[r];
- else return c[r] - c[l - 1];
- }
- int32_t main()
- {
- int t;
- cin >> t;
- while (t--)
- {
- int n;
- cin >> n;
- a.resize(n);
- b.resize(n);
- c.resize(n);
- for (int i = 0; i < n; i++)
- cin >> a [i] >> b [i];
- for (int i = 0; i < n; i++)
- {
- if (i == n - 1)
- c [0] = max(a [0] - b [i], 0LL);
- else
- c [i + 1] = max(a [i + 1] - b [i], 0LL);
- }
- for (int i = 1; i < n; i++)
- c [i] += c [i - 1];
- int ans = LLONG_MAX;
- for (int i = 0; i < n; i++)
- {
- int cnt = a [i] + get(0, i - 1) + get(i + 1, n - 1);
- ans = min(ans, cnt);
- }
- cout << ans << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment