# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
833264 | 2023-08-22T03:52:36 Z | josanneo22 | Feast (NOI19_feast) | C++17 | 2 ms | 340 KB |
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double #define mp make_pair #define pb push_back #define pii pair<int,int> #define fi first #define se second #define all(x) x.begin(), x.end() #define ar array const int inf=1e15; long long maxSubArraySum(vector<long long> &a, int size) { long long max_so_far = LLONG_MIN, max_ending_here = 0; int starting_index = 0, ending_index = 0, s = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; starting_index = s; ending_index = i; } if (max_ending_here < 0) { max_ending_here = 0; s = i + 1; } } return max_so_far; } void solve(){ int n,k; cin>>n>>k; vector<int> a(n+1); for(int i=1;i<=n;i++) cin>>a[i]; if(*min_element(a.begin(), a.end())>=0){ cout<< accumulate(a.begin(),a.end(),0LL)<<'\n'; return; } if(k==1){ cout<<max(0LL,maxSubArraySum(a,n+1))<<'\n'; return; } int cnt=0,pos=-1; for(int i=1;i<=n;i++){ if(a[i]<0){ cnt++; pos=i; } } if(cnt<=1){ int ans=max(0LL,accumulate(a.begin(),a.end(),0LL)); if(pos==-1){ cout<<ans<<'\n'; return;} else{ int S1=0,S2=0; for(int i=1;i<pos;i++) S1+=a[i]; for(int i=pos+1;i<=n;i++) S2+=a[i]; cout<<S1+S2<<'\n'; return; } } vector<vector<vector<int>>> dp(n+1,vector<vector<int>>(n+1,vector<int>(2,-inf))); dp[0][0][0]=0; dp[0][0][1]=0; int ans=0; for(int i=1;i<=n;i++){ dp[i][0][0]=0; for(int j=1;j<=n;j++){ dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j][1]); dp[i][j][1]=max(dp[i-1][j][1],dp[i-1][j-1][0])+a[i]; if(j<=k) ans=max(ans,max(dp[i][j][0],dp[i][j][1])); } } cout<<ans<<'\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); clock_t tStart = clock(); int local=1,multi=0,debug=1,tt=1; if(local){ freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); } if(multi) cin>>tt; for(int i=1;i<=tt;i++){ if(debug && multi && local) cout<<"样例 "<<i<<'\n'; solve(); } fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC); }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 1 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Incorrect | 2 ms | 340 KB | Output isn't correct |
2 | Halted | 0 ms | 0 KB | - |