#include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[50002]; struct node{ ll sum,idx; }nn[50002]; const ll inf=1ll<<61; bool cmp(const node &a,const node &b){ return a.sum<b.sum||(a.sum==b.sum&&a.idx<b.idx); } int main(){ ll n,ans=inf; cin>>n; for(int i=1;i<=n;i++) cin>>a[i],nn[i].sum=nn[i-1].sum+a[i],nn[i].idx=i; sort(nn,nn+n+1,cmp); for(int i=1;i<=n;i++){ if(nn[i].sum>0){ ans=min(ans,nn[i].sum); } if(nn[i].sum>nn[i-1].sum&&nn[i-1].idx<nn[i].idx){ ans=min(ans,nn[i].sum-nn[i-1].sum); } } cout<<ans<<"\n"; return 0; }
时间: 2024-10-10 22:24:24