Submission #791354

#TimeUsernameProblemLanguageResultExecution timeMemory
791354trucmaiFeast (NOI19_feast)C++17
71 / 100
69 ms68684 KiB
//i_love_aisukiuwu #include <bits/stdc++.h> using namespace std; #ifdef LOCAL #include "/home/trucmai/.vim/tools.h" #define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl; #else #define debug(x...) #endif void open(){ if(fopen("i.inp","r")){ freopen("i.inp","r",stdin); freopen("o.out","w",stdout); } } #define ll long long #define all(a) (a).begin(), (a).end() #define vi vector<ll> #define pi pair<ll,ll> #define pii pair<ll,pair<ll,ll>> #define fi first #define se second #define gcd __gcd #define mset(a,v) memset(a, v, sizeof(a)) #define endl '\n' #define spc " " const int MN1 = 1e6 + 5,MN2 = 2e3 + 5,LOG = 27; const ll MOD = 1e9 + 7,INF = 1e9; ll n,k,a[MN1],dp[MN2][MN2][2]; /* dp[i][j][k(0\1)] la max sum tai vi tri i,segment j va state k k = 0: dp[i][j][0] = max(dp[i-1][j][0],dp[i-1][j][1] + a[j]) k = 1: dp[i][j][1] = max(dp[i-1][j-1][0],dp[i-1][j][1] + a[j]) */ void solve(){ cin>>n>>k; ll cnt = 0; for(ll i = 1;i <= n;++i){ cin>>a[i]; if(a[i] < 0) ++cnt; } if(cnt == 0){ ll res = 0; for(ll i = 1; i <= n;++i) res += a[i]; cout<<res<<endl; } else if(cnt == 1){ ll left = 0, right = 0, flag = 0, pos = -1,res = 0; for(int i = 1; i <= n; i++){ if(a[i] < 0){ pos = i; flag = 1; continue; } else if(flag == 0) left += a[i]; else right += a[i]; } debug(left,right); if(k >= 2) res = left + right; else if(k == 1) res = max(max(left,right), left + right + a[pos]); cout<<res<<endl; } else if(k == 1){ ll help = 0,res = 0; for(int i = 1; i <= n; i++){ help += a[i]; if(res < help) res = help; if(help < 0) help = 0; } cout<<res<<endl; } else{ mset(dp,-1); auto f=[&](ll i,ll j,bool state,auto &&f)->ll{ if(i > n || j > k) return 0; ll &res = dp[i][j][state]; if(res != -1) return res; res = 0; res = max(res,f(i+1,j,1,f) + a[i]); if(!state) res = max(res,f(i+1,j,0,f)); else if(j + 1 < k && state) res = max(res,f(i+1,j+1,0,f)); return res; }; cout<<f(1,0,0,f)<<endl; } } signed main(){ cin.tie(0) -> sync_with_stdio(0); open(); ll t = 1; //cin>>t; while(t--) solve(); #ifdef LOCAL cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; #endif }

Compilation message (stderr)

feast.cpp: In function 'void open()':
feast.cpp:14:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |     freopen("i.inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~
feast.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |     freopen("o.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...