#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 siga(ll pos, ll left){
//retornar o best que posso fazer
//usei até pos-1
if(pos==n+1)return 0;
if(left==0)return 0;
ll i,ret=0,k,gains;
k=siga(pos+1,left);
ret=max(ret,k);
for(i=pos+1;i<=n;i++){//usar até i
gains=dp[i]-dp[pos-1];
k = siga(i+1,left-1)+gains;
ret=max(ret,k);
k = siga(i+1,left);
ret=max(ret,k);
}
return ret;
}
int main(){
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
ans=siga(1,k);
}
printf("%lld\n",ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
6520 KB |
Output is correct |
2 |
Correct |
180 ms |
6468 KB |
Output is correct |
3 |
Correct |
182 ms |
6472 KB |
Output is correct |
4 |
Correct |
182 ms |
6520 KB |
Output is correct |
5 |
Correct |
181 ms |
6440 KB |
Output is correct |
6 |
Correct |
180 ms |
6520 KB |
Output is correct |
7 |
Correct |
180 ms |
6404 KB |
Output is correct |
8 |
Correct |
184 ms |
6520 KB |
Output is correct |
9 |
Correct |
178 ms |
6392 KB |
Output is correct |
10 |
Correct |
185 ms |
6648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
6136 KB |
Output is correct |
2 |
Correct |
97 ms |
6264 KB |
Output is correct |
3 |
Correct |
95 ms |
6208 KB |
Output is correct |
4 |
Correct |
97 ms |
6136 KB |
Output is correct |
5 |
Correct |
178 ms |
6392 KB |
Output is correct |
6 |
Correct |
99 ms |
6008 KB |
Output is correct |
7 |
Correct |
100 ms |
6136 KB |
Output is correct |
8 |
Correct |
182 ms |
6536 KB |
Output is correct |
9 |
Correct |
176 ms |
6392 KB |
Output is correct |
10 |
Correct |
102 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
8696 KB |
Output is correct |
2 |
Correct |
196 ms |
8628 KB |
Output is correct |
3 |
Correct |
197 ms |
8824 KB |
Output is correct |
4 |
Correct |
192 ms |
8696 KB |
Output is correct |
5 |
Correct |
194 ms |
8696 KB |
Output is correct |
6 |
Correct |
197 ms |
8696 KB |
Output is correct |
7 |
Correct |
198 ms |
8696 KB |
Output is correct |
8 |
Correct |
196 ms |
8824 KB |
Output is correct |
9 |
Correct |
197 ms |
8696 KB |
Output is correct |
10 |
Correct |
195 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
2688 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
2688 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
2688 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
6520 KB |
Output is correct |
2 |
Correct |
180 ms |
6468 KB |
Output is correct |
3 |
Correct |
182 ms |
6472 KB |
Output is correct |
4 |
Correct |
182 ms |
6520 KB |
Output is correct |
5 |
Correct |
181 ms |
6440 KB |
Output is correct |
6 |
Correct |
180 ms |
6520 KB |
Output is correct |
7 |
Correct |
180 ms |
6404 KB |
Output is correct |
8 |
Correct |
184 ms |
6520 KB |
Output is correct |
9 |
Correct |
178 ms |
6392 KB |
Output is correct |
10 |
Correct |
185 ms |
6648 KB |
Output is correct |
11 |
Correct |
100 ms |
6136 KB |
Output is correct |
12 |
Correct |
97 ms |
6264 KB |
Output is correct |
13 |
Correct |
95 ms |
6208 KB |
Output is correct |
14 |
Correct |
97 ms |
6136 KB |
Output is correct |
15 |
Correct |
178 ms |
6392 KB |
Output is correct |
16 |
Correct |
99 ms |
6008 KB |
Output is correct |
17 |
Correct |
100 ms |
6136 KB |
Output is correct |
18 |
Correct |
182 ms |
6536 KB |
Output is correct |
19 |
Correct |
176 ms |
6392 KB |
Output is correct |
20 |
Correct |
102 ms |
6264 KB |
Output is correct |
21 |
Correct |
197 ms |
8696 KB |
Output is correct |
22 |
Correct |
196 ms |
8628 KB |
Output is correct |
23 |
Correct |
197 ms |
8824 KB |
Output is correct |
24 |
Correct |
192 ms |
8696 KB |
Output is correct |
25 |
Correct |
194 ms |
8696 KB |
Output is correct |
26 |
Correct |
197 ms |
8696 KB |
Output is correct |
27 |
Correct |
198 ms |
8696 KB |
Output is correct |
28 |
Correct |
196 ms |
8824 KB |
Output is correct |
29 |
Correct |
197 ms |
8696 KB |
Output is correct |
30 |
Correct |
195 ms |
8824 KB |
Output is correct |
31 |
Correct |
6 ms |
2688 KB |
Output is correct |
32 |
Execution timed out |
1090 ms |
2688 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |