Problem - 1334C - Codeforces
解析:
对于某个怪兽,他的耗费为两种情况,要么直接用子弹打,要么被前面的怪兽炸,显然第二种情况耗费更少。
统计出所有怪兽的 max(0,a[ i ] - b[ i - 1 ]),然后统计出总和 sum
因为必须选择一个怪兽用子弹打死,所以遍历所有的怪兽,统计出 sum - c[ i ] + a[ i ] 的最小值
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=3e5+5;
int t,n,a[N],b[N],c[N],sum[N];
signed main(){
scanf("%lld",&t);
while(t--){
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld%lld",&a[i],&b[i]);
}
b[0]=b[n];
int sum=0;
for(int i=1;i<=n;i++){
c[i]=max((long long)0,a[i]-b[i-1]);
sum+=c[i];
}
int res=0x3f3f3f3f;
for(int i=1;i<=n;i++){
res=min(res,sum-c[i]+a[i]);
}
printf("%lld\n\n",res);
}
return 0;
}