题解 | #丢手绢#
丢手绢
https://ac.nowcoder.com/acm/problem/207040
#include
using namespace std;
int main() { int n, sum = 0, t = 0; cin >> n;
int a[n];
for(int i = 0; i < n; i++) cin >> a[i], sum += a[i];
int l = 0, r = 0, ret = 0;
for(l; l < n; l++)
{
while(t < sum / 2)
{
t += a[r % n];
r ++;
}
if(t == sum / 2){cout << t; return 0;}
if(t > sum / 2) ret = max(ret, min(t, sum - t));
t -= a[l];
}
cout << ret;
return 0;
}
当对r限制到小于n时会导致错误。