ProgramAlgTrain/20240919/CSP常考算法模板/前缀和.cpp

38 lines
466 B
C++
Raw Normal View History

2024-09-19 02:22:41 +00:00
#include <bits/stdc++.h>
using namespace std;
int n, a[1001], prefix[1001];
int main()
{
freopen("bcount.in","r",stdin);
freopen("bcount.out","w",stdout);
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
//todo
prefix[i]=prefix[i-1]+a[i];
}
int ans = INT_MIN;
for(int i=1;i<=n;i++) //begin location
{
for(int j=i;j<=n;j++) //end location
{
ans=max(ans,prefix[j]-prefix[i-1]);
}
}
cout<<ans<<endl;
return 0;
}
//5
//1 -2 3 1 -4