#include <bits/stdc++.h>
using namespace std;
#define loop(i,n) for (ll i = 0; i < n; i++)
#define ll long long
#define INF 1e9+5
#define MAXN 300007
#define pb push_back
#define mp make_pair
typedef pair<ll,ll> pii;
ll n,k;
vector<ll>dp(MAXN);//de 1 a n
ll arr[2005][2005][2];
ll siga(ll pos, ll left,bool maybe){
//retornar o best que posso fazer
//usei até pos-1
if(pos==n+1)return 0;
if(left==0)return 0;
ll x;
if(maybe)x=1;
else x=0;
if(arr[pos][left][x]!=-1){
return arr[pos][left][x];
}
ll i,ret=0,k,gains;
for(i=pos;i<=n;i++){//usar até i
gains=dp[i]-dp[pos-1];
k = siga(i+1,left-1,true)+gains;
ret=max(ret,k);
if(maybe){
k = siga(i+1,left,false);
ret=max(ret,k);
}
}
arr[pos][left][x]=ret;
return ret;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll i,ans,count=0,mini,x;
cin>>n>>k;
vector<ll>a(n+1);
for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]<0){count++; mini=i;}
dp[i]=dp[i-1]+a[i];
}
if(count==0){
ans=dp[n];
}else if(count==1){
if(k>=2)ans=dp[n]-a[mini];
else{
x=dp[n]-dp[mini];
ans=max(dp[n], max(dp[mini-1], x));
}
}else if(k==1){//clássico maximum sum subarray
ans=0;
vector<ll>bruh(n+1);
for(i=1;i<=n;i++){
bruh[i]=max(bruh[i-1],(ll int)0)+a[i];
ans=max(ans,bruh[i]);
}
}else{//bruteforce básico
ll j;
for(i=0;i<=n+1;i++){
for(j=0;j<=k+1;j++){
arr[i][j][0]=(ll)-1;
arr[i][j][1]=(ll)-1;
}
}
ans=siga(1,k,true);
}
printf("%lld\n",ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4992 KB |
Output is correct |
2 |
Correct |
45 ms |
4992 KB |
Output is correct |
3 |
Correct |
47 ms |
5112 KB |
Output is correct |
4 |
Correct |
47 ms |
4992 KB |
Output is correct |
5 |
Correct |
45 ms |
5068 KB |
Output is correct |
6 |
Correct |
45 ms |
5112 KB |
Output is correct |
7 |
Correct |
47 ms |
4864 KB |
Output is correct |
8 |
Correct |
48 ms |
5116 KB |
Output is correct |
9 |
Correct |
47 ms |
5024 KB |
Output is correct |
10 |
Correct |
44 ms |
5060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
4992 KB |
Output is correct |
2 |
Correct |
29 ms |
4992 KB |
Output is correct |
3 |
Correct |
27 ms |
4992 KB |
Output is correct |
4 |
Correct |
34 ms |
4992 KB |
Output is correct |
5 |
Correct |
45 ms |
4992 KB |
Output is correct |
6 |
Correct |
30 ms |
4992 KB |
Output is correct |
7 |
Correct |
27 ms |
4992 KB |
Output is correct |
8 |
Correct |
48 ms |
5112 KB |
Output is correct |
9 |
Correct |
44 ms |
5020 KB |
Output is correct |
10 |
Correct |
26 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
7288 KB |
Output is correct |
2 |
Correct |
53 ms |
7288 KB |
Output is correct |
3 |
Correct |
53 ms |
7288 KB |
Output is correct |
4 |
Correct |
53 ms |
7288 KB |
Output is correct |
5 |
Correct |
53 ms |
7288 KB |
Output is correct |
6 |
Correct |
53 ms |
7288 KB |
Output is correct |
7 |
Correct |
53 ms |
7416 KB |
Output is correct |
8 |
Correct |
54 ms |
7288 KB |
Output is correct |
9 |
Correct |
53 ms |
7416 KB |
Output is correct |
10 |
Correct |
55 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
7 ms |
3072 KB |
Output is correct |
3 |
Correct |
7 ms |
3200 KB |
Output is correct |
4 |
Correct |
7 ms |
3072 KB |
Output is correct |
5 |
Correct |
7 ms |
3072 KB |
Output is correct |
6 |
Correct |
7 ms |
3072 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
7 ms |
3072 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
7 ms |
3072 KB |
Output is correct |
3 |
Correct |
7 ms |
3200 KB |
Output is correct |
4 |
Correct |
7 ms |
3072 KB |
Output is correct |
5 |
Correct |
7 ms |
3072 KB |
Output is correct |
6 |
Correct |
7 ms |
3072 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
7 ms |
3072 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
3072 KB |
Output is correct |
11 |
Correct |
28 ms |
4152 KB |
Output is correct |
12 |
Correct |
32 ms |
3968 KB |
Output is correct |
13 |
Correct |
16 ms |
3840 KB |
Output is correct |
14 |
Correct |
27 ms |
3968 KB |
Output is correct |
15 |
Correct |
23 ms |
3968 KB |
Output is correct |
16 |
Correct |
16 ms |
3840 KB |
Output is correct |
17 |
Correct |
33 ms |
4096 KB |
Output is correct |
18 |
Correct |
8 ms |
3840 KB |
Output is correct |
19 |
Correct |
13 ms |
3840 KB |
Output is correct |
20 |
Correct |
10 ms |
3968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
7 ms |
3072 KB |
Output is correct |
3 |
Correct |
7 ms |
3200 KB |
Output is correct |
4 |
Correct |
7 ms |
3072 KB |
Output is correct |
5 |
Correct |
7 ms |
3072 KB |
Output is correct |
6 |
Correct |
7 ms |
3072 KB |
Output is correct |
7 |
Correct |
6 ms |
3072 KB |
Output is correct |
8 |
Correct |
7 ms |
3072 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
3072 KB |
Output is correct |
11 |
Correct |
28 ms |
4152 KB |
Output is correct |
12 |
Correct |
32 ms |
3968 KB |
Output is correct |
13 |
Correct |
16 ms |
3840 KB |
Output is correct |
14 |
Correct |
27 ms |
3968 KB |
Output is correct |
15 |
Correct |
23 ms |
3968 KB |
Output is correct |
16 |
Correct |
16 ms |
3840 KB |
Output is correct |
17 |
Correct |
33 ms |
4096 KB |
Output is correct |
18 |
Correct |
8 ms |
3840 KB |
Output is correct |
19 |
Correct |
13 ms |
3840 KB |
Output is correct |
20 |
Correct |
10 ms |
3968 KB |
Output is correct |
21 |
Execution timed out |
1089 ms |
16768 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
4992 KB |
Output is correct |
2 |
Correct |
45 ms |
4992 KB |
Output is correct |
3 |
Correct |
47 ms |
5112 KB |
Output is correct |
4 |
Correct |
47 ms |
4992 KB |
Output is correct |
5 |
Correct |
45 ms |
5068 KB |
Output is correct |
6 |
Correct |
45 ms |
5112 KB |
Output is correct |
7 |
Correct |
47 ms |
4864 KB |
Output is correct |
8 |
Correct |
48 ms |
5116 KB |
Output is correct |
9 |
Correct |
47 ms |
5024 KB |
Output is correct |
10 |
Correct |
44 ms |
5060 KB |
Output is correct |
11 |
Correct |
27 ms |
4992 KB |
Output is correct |
12 |
Correct |
29 ms |
4992 KB |
Output is correct |
13 |
Correct |
27 ms |
4992 KB |
Output is correct |
14 |
Correct |
34 ms |
4992 KB |
Output is correct |
15 |
Correct |
45 ms |
4992 KB |
Output is correct |
16 |
Correct |
30 ms |
4992 KB |
Output is correct |
17 |
Correct |
27 ms |
4992 KB |
Output is correct |
18 |
Correct |
48 ms |
5112 KB |
Output is correct |
19 |
Correct |
44 ms |
5020 KB |
Output is correct |
20 |
Correct |
26 ms |
4992 KB |
Output is correct |
21 |
Correct |
54 ms |
7288 KB |
Output is correct |
22 |
Correct |
53 ms |
7288 KB |
Output is correct |
23 |
Correct |
53 ms |
7288 KB |
Output is correct |
24 |
Correct |
53 ms |
7288 KB |
Output is correct |
25 |
Correct |
53 ms |
7288 KB |
Output is correct |
26 |
Correct |
53 ms |
7288 KB |
Output is correct |
27 |
Correct |
53 ms |
7416 KB |
Output is correct |
28 |
Correct |
54 ms |
7288 KB |
Output is correct |
29 |
Correct |
53 ms |
7416 KB |
Output is correct |
30 |
Correct |
55 ms |
7416 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Correct |
7 ms |
3072 KB |
Output is correct |
33 |
Correct |
7 ms |
3200 KB |
Output is correct |
34 |
Correct |
7 ms |
3072 KB |
Output is correct |
35 |
Correct |
7 ms |
3072 KB |
Output is correct |
36 |
Correct |
7 ms |
3072 KB |
Output is correct |
37 |
Correct |
6 ms |
3072 KB |
Output is correct |
38 |
Correct |
7 ms |
3072 KB |
Output is correct |
39 |
Correct |
6 ms |
2688 KB |
Output is correct |
40 |
Correct |
6 ms |
3072 KB |
Output is correct |
41 |
Correct |
28 ms |
4152 KB |
Output is correct |
42 |
Correct |
32 ms |
3968 KB |
Output is correct |
43 |
Correct |
16 ms |
3840 KB |
Output is correct |
44 |
Correct |
27 ms |
3968 KB |
Output is correct |
45 |
Correct |
23 ms |
3968 KB |
Output is correct |
46 |
Correct |
16 ms |
3840 KB |
Output is correct |
47 |
Correct |
33 ms |
4096 KB |
Output is correct |
48 |
Correct |
8 ms |
3840 KB |
Output is correct |
49 |
Correct |
13 ms |
3840 KB |
Output is correct |
50 |
Correct |
10 ms |
3968 KB |
Output is correct |
51 |
Execution timed out |
1089 ms |
16768 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |