https://codeforces.com/contest/1334/problem/C
2.Idea
Only start point takes a[i] shoots, and other places a[i] - b[i-1]. Check every places as start point.
3.Source
int n, t;
ll a[300005], b[300005], ac[300005];
void solve()
{
ll sum = 0;
for (int i = 0; i < n; i++) {
ac[i] = max(0ll, a[i] - b[(i + n - 1) % n]);
sum += ac[i];
}
ll ans = 1ll << 60;
for (int i = 0; i < n; i++) {
ans = min(ans, sum - ac[i] + a[i]);
}
//cout << ans << endl;
printf("%lld\n", ans);
}
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lld%lld", &a[i], &b[i]);
}
solve();
}
return 0;
}
No comments:
Post a Comment