Submission #242674

# Submission time Handle Problem Language Result Execution time Memory
242674 2020-06-28T23:06:29 Z AmineWeslati Feast (NOI19_feast) C++14
51 / 100
285 ms 5760 KB
//Never stop trying
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//s.order_of_key(), *s.find_by_order()

using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef string str;
typedef long long ll;
#define int ll
typedef double db;
typedef long double ld;

typedef pair<int,int> pi;
#define fi first
#define se second

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;

#define pb push_back
#define eb emplace_back
#define pf push_front

#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"

const int MOD = 1e9+7; //998244353
const ll INF = 1e18;
const int nx[4]={0,0,1,-1}, ny[4]={1,-1,0,0}; //right left down up

int t[300001];
int pref[300001];
int memo[300][301];
int N,K;

int solve(int i, int r){
   if(!r) return 0;
   if(i==N) return -INF;
   if(memo[i][r]!=-1) return memo[i][r];
   int ans=solve(i,r-1);
   for(int j=i; j<N; j++){
      for(int k=j; k<N; k++){
         ans=max(ans,solve(k+1,r-1)+pref[k+1]-pref[j]);
      }
   }
   return memo[i][r]=ans;
}

int32_t main(){
   boost;
   memset(memo,-1,sizeof(memo));
   cin>>N>>K;
   pref[0]=0;
   int cnt=0;
   for(int i=0; i<N; i++){
      cin>>t[i];
      pref[i+1]=t[i];
      if(i) pref[i+1]+=pref[i];
      if(t[i]<0) cnt++;
   }

   if(cnt==0 ||(cnt==1 && K>1)){
      int res=0;
      for(int i=0; i<N; i++) if(t[i]>0) res+=t[i];
      cout << res << endl;
   }
   else if(cnt==1){
      int s=0,ss=0,sss=0;
      int idx=0;
      while(t[idx]>=0){s+=t[idx]; idx++;}
      sss+=t[idx]; idx++;
      while(idx<N && t[idx]>=0){ss+=t[idx]; idx++;}
      cout << max(max(s,ss),s+ss+sss) << endl;
   }
   else if(K==1){
      int s=0;
      int res=0;
      for(int i=0; i<N; i++){
         s+=t[i];
         res=max(res,s);
         if(s<=0) s=0;
      }
      cout << res << endl;
   }
   else if(N<=300){
      int res=0;
      for(int i=0; i<N; i++) res=max(res,solve(i,K));
      cout << res << endl;
   }


   return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 45 ms 5624 KB Output is correct
2 Correct 45 ms 5752 KB Output is correct
3 Correct 44 ms 5752 KB Output is correct
4 Correct 47 ms 5752 KB Output is correct
5 Correct 47 ms 5624 KB Output is correct
6 Correct 44 ms 5624 KB Output is correct
7 Correct 42 ms 5624 KB Output is correct
8 Correct 45 ms 5752 KB Output is correct
9 Correct 44 ms 5624 KB Output is correct
10 Correct 47 ms 5624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 5632 KB Output is correct
2 Correct 29 ms 5752 KB Output is correct
3 Correct 30 ms 5632 KB Output is correct
4 Correct 28 ms 5632 KB Output is correct
5 Correct 46 ms 5624 KB Output is correct
6 Correct 26 ms 5624 KB Output is correct
7 Correct 28 ms 5760 KB Output is correct
8 Correct 45 ms 5752 KB Output is correct
9 Correct 43 ms 5624 KB Output is correct
10 Correct 27 ms 5760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 5752 KB Output is correct
2 Correct 49 ms 5624 KB Output is correct
3 Correct 52 ms 5752 KB Output is correct
4 Correct 49 ms 5624 KB Output is correct
5 Correct 51 ms 5628 KB Output is correct
6 Correct 51 ms 5752 KB Output is correct
7 Correct 49 ms 5752 KB Output is correct
8 Correct 51 ms 5624 KB Output is correct
9 Correct 49 ms 5752 KB Output is correct
10 Correct 50 ms 5752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1024 KB Output is correct
2 Correct 12 ms 1024 KB Output is correct
3 Correct 5 ms 1024 KB Output is correct
4 Correct 5 ms 1152 KB Output is correct
5 Correct 7 ms 1024 KB Output is correct
6 Correct 6 ms 1152 KB Output is correct
7 Correct 6 ms 1024 KB Output is correct
8 Correct 5 ms 1024 KB Output is correct
9 Correct 5 ms 1024 KB Output is correct
10 Correct 6 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1024 KB Output is correct
2 Correct 12 ms 1024 KB Output is correct
3 Correct 5 ms 1024 KB Output is correct
4 Correct 5 ms 1152 KB Output is correct
5 Correct 7 ms 1024 KB Output is correct
6 Correct 6 ms 1152 KB Output is correct
7 Correct 6 ms 1024 KB Output is correct
8 Correct 5 ms 1024 KB Output is correct
9 Correct 5 ms 1024 KB Output is correct
10 Correct 6 ms 1024 KB Output is correct
11 Correct 246 ms 1144 KB Output is correct
12 Correct 255 ms 1144 KB Output is correct
13 Correct 93 ms 1024 KB Output is correct
14 Correct 221 ms 1144 KB Output is correct
15 Correct 157 ms 1144 KB Output is correct
16 Correct 109 ms 1024 KB Output is correct
17 Correct 285 ms 1144 KB Output is correct
18 Correct 24 ms 1152 KB Output is correct
19 Correct 64 ms 1024 KB Output is correct
20 Correct 51 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 1024 KB Output is correct
2 Correct 12 ms 1024 KB Output is correct
3 Correct 5 ms 1024 KB Output is correct
4 Correct 5 ms 1152 KB Output is correct
5 Correct 7 ms 1024 KB Output is correct
6 Correct 6 ms 1152 KB Output is correct
7 Correct 6 ms 1024 KB Output is correct
8 Correct 5 ms 1024 KB Output is correct
9 Correct 5 ms 1024 KB Output is correct
10 Correct 6 ms 1024 KB Output is correct
11 Correct 246 ms 1144 KB Output is correct
12 Correct 255 ms 1144 KB Output is correct
13 Correct 93 ms 1024 KB Output is correct
14 Correct 221 ms 1144 KB Output is correct
15 Correct 157 ms 1144 KB Output is correct
16 Correct 109 ms 1024 KB Output is correct
17 Correct 285 ms 1144 KB Output is correct
18 Correct 24 ms 1152 KB Output is correct
19 Correct 64 ms 1024 KB Output is correct
20 Correct 51 ms 1024 KB Output is correct
21 Incorrect 5 ms 1152 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 45 ms 5624 KB Output is correct
2 Correct 45 ms 5752 KB Output is correct
3 Correct 44 ms 5752 KB Output is correct
4 Correct 47 ms 5752 KB Output is correct
5 Correct 47 ms 5624 KB Output is correct
6 Correct 44 ms 5624 KB Output is correct
7 Correct 42 ms 5624 KB Output is correct
8 Correct 45 ms 5752 KB Output is correct
9 Correct 44 ms 5624 KB Output is correct
10 Correct 47 ms 5624 KB Output is correct
11 Correct 28 ms 5632 KB Output is correct
12 Correct 29 ms 5752 KB Output is correct
13 Correct 30 ms 5632 KB Output is correct
14 Correct 28 ms 5632 KB Output is correct
15 Correct 46 ms 5624 KB Output is correct
16 Correct 26 ms 5624 KB Output is correct
17 Correct 28 ms 5760 KB Output is correct
18 Correct 45 ms 5752 KB Output is correct
19 Correct 43 ms 5624 KB Output is correct
20 Correct 27 ms 5760 KB Output is correct
21 Correct 49 ms 5752 KB Output is correct
22 Correct 49 ms 5624 KB Output is correct
23 Correct 52 ms 5752 KB Output is correct
24 Correct 49 ms 5624 KB Output is correct
25 Correct 51 ms 5628 KB Output is correct
26 Correct 51 ms 5752 KB Output is correct
27 Correct 49 ms 5752 KB Output is correct
28 Correct 51 ms 5624 KB Output is correct
29 Correct 49 ms 5752 KB Output is correct
30 Correct 50 ms 5752 KB Output is correct
31 Correct 5 ms 1024 KB Output is correct
32 Correct 12 ms 1024 KB Output is correct
33 Correct 5 ms 1024 KB Output is correct
34 Correct 5 ms 1152 KB Output is correct
35 Correct 7 ms 1024 KB Output is correct
36 Correct 6 ms 1152 KB Output is correct
37 Correct 6 ms 1024 KB Output is correct
38 Correct 5 ms 1024 KB Output is correct
39 Correct 5 ms 1024 KB Output is correct
40 Correct 6 ms 1024 KB Output is correct
41 Correct 246 ms 1144 KB Output is correct
42 Correct 255 ms 1144 KB Output is correct
43 Correct 93 ms 1024 KB Output is correct
44 Correct 221 ms 1144 KB Output is correct
45 Correct 157 ms 1144 KB Output is correct
46 Correct 109 ms 1024 KB Output is correct
47 Correct 285 ms 1144 KB Output is correct
48 Correct 24 ms 1152 KB Output is correct
49 Correct 64 ms 1024 KB Output is correct
50 Correct 51 ms 1024 KB Output is correct
51 Incorrect 5 ms 1152 KB Output isn't correct
52 Halted 0 ms 0 KB -