因为看错一个正数,我把0算上了,改了无数次,GG
#include <bits/stdc++.h>
#define ll long long
using namespace std;
long long a[50010],x,min1=1e18;
set<long long>s;
int main()
{
int n;
scanf("%d",&n);
a[0]=0;
for(int i=1;i<=n;i++)
{scanf("%lld",&x);a[i]=a[i-1]+x;
if(x>0)min1=min(min1,x);
if(a[i]>0)min1=min(min1,a[i]);
}
s.insert(a[n]);
for(int i=n-1;i>=1;i--)
{
if(s.upper_bound(a[i])!=s.end())
{long long k=*s.lower_bound(a[i]);
// printf("%d\n",i);
if(k>a[i])
min1=min(min1,k-a[i]);}
s.insert(a[i]);
}
cout<<min1<<endl;
return 0;
}代码二
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;
int n;
struct no1
{
long long value;
int pos;
}node[50005];
bool cmp(no1 &n1,no1 &n2)
{
return n1.value<n2.value;
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
int i,flag;
long long sum,temp,res;
scanf("%d",&n);
node[0].pos=0;
node[0].value=0;
sum=0;
for(i=1;i<=n;i++)
{
scanf("%lld",&temp);
sum += temp;
node[i].pos=i;
node[i].value=sum;
}
sort(node,node+n+1,cmp);
flag=0;
for(i=1;i<=n;i++)
{
if(node[i].pos-node[i-1].pos>0&&node[i].value-node[i-1].value>0)
{
if(flag==0)
{
flag=1;
res=node[i].value-node[i-1].value;
}
else
{
if(node[i].value-node[i-1].value<res)
res=node[i].value-node[i-1].value;
}
}
}
printf("%lld\n",res);
//system("pause");
return 0;
}