#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 200007
#define pb push_back
#define mp make_pair
typedef pair<ll,ll> pii;
int main(){
ll n,i,k,ans,count=0,mini,x;
cin>>n>>k;
vector<ll>a(n+1);
vector<ll>dp(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{
}
printf("%lld\n",ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
7672 KB |
Output is correct |
2 |
Correct |
182 ms |
7928 KB |
Output is correct |
3 |
Correct |
182 ms |
7872 KB |
Output is correct |
4 |
Correct |
181 ms |
7832 KB |
Output is correct |
5 |
Correct |
183 ms |
7928 KB |
Output is correct |
6 |
Correct |
179 ms |
7672 KB |
Output is correct |
7 |
Correct |
179 ms |
7800 KB |
Output is correct |
8 |
Correct |
179 ms |
7800 KB |
Output is correct |
9 |
Correct |
178 ms |
7672 KB |
Output is correct |
10 |
Correct |
180 ms |
7724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
6104 KB |
Output is correct |
2 |
Correct |
99 ms |
6136 KB |
Output is correct |
3 |
Correct |
98 ms |
6108 KB |
Output is correct |
4 |
Correct |
96 ms |
6008 KB |
Output is correct |
5 |
Correct |
180 ms |
7672 KB |
Output is correct |
6 |
Correct |
95 ms |
6008 KB |
Output is correct |
7 |
Correct |
97 ms |
6136 KB |
Output is correct |
8 |
Correct |
182 ms |
7800 KB |
Output is correct |
9 |
Correct |
178 ms |
7672 KB |
Output is correct |
10 |
Correct |
97 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
10360 KB |
Output is correct |
2 |
Correct |
195 ms |
10184 KB |
Output is correct |
3 |
Correct |
194 ms |
10232 KB |
Output is correct |
4 |
Correct |
194 ms |
10232 KB |
Output is correct |
5 |
Correct |
197 ms |
10236 KB |
Output is correct |
6 |
Correct |
199 ms |
10360 KB |
Output is correct |
7 |
Correct |
196 ms |
10492 KB |
Output is correct |
8 |
Correct |
194 ms |
10232 KB |
Output is correct |
9 |
Correct |
198 ms |
10488 KB |
Output is correct |
10 |
Correct |
198 ms |
10360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
7672 KB |
Output is correct |
2 |
Correct |
182 ms |
7928 KB |
Output is correct |
3 |
Correct |
182 ms |
7872 KB |
Output is correct |
4 |
Correct |
181 ms |
7832 KB |
Output is correct |
5 |
Correct |
183 ms |
7928 KB |
Output is correct |
6 |
Correct |
179 ms |
7672 KB |
Output is correct |
7 |
Correct |
179 ms |
7800 KB |
Output is correct |
8 |
Correct |
179 ms |
7800 KB |
Output is correct |
9 |
Correct |
178 ms |
7672 KB |
Output is correct |
10 |
Correct |
180 ms |
7724 KB |
Output is correct |
11 |
Correct |
96 ms |
6104 KB |
Output is correct |
12 |
Correct |
99 ms |
6136 KB |
Output is correct |
13 |
Correct |
98 ms |
6108 KB |
Output is correct |
14 |
Correct |
96 ms |
6008 KB |
Output is correct |
15 |
Correct |
180 ms |
7672 KB |
Output is correct |
16 |
Correct |
95 ms |
6008 KB |
Output is correct |
17 |
Correct |
97 ms |
6136 KB |
Output is correct |
18 |
Correct |
182 ms |
7800 KB |
Output is correct |
19 |
Correct |
178 ms |
7672 KB |
Output is correct |
20 |
Correct |
97 ms |
6136 KB |
Output is correct |
21 |
Correct |
194 ms |
10360 KB |
Output is correct |
22 |
Correct |
195 ms |
10184 KB |
Output is correct |
23 |
Correct |
194 ms |
10232 KB |
Output is correct |
24 |
Correct |
194 ms |
10232 KB |
Output is correct |
25 |
Correct |
197 ms |
10236 KB |
Output is correct |
26 |
Correct |
199 ms |
10360 KB |
Output is correct |
27 |
Correct |
196 ms |
10492 KB |
Output is correct |
28 |
Correct |
194 ms |
10232 KB |
Output is correct |
29 |
Correct |
198 ms |
10488 KB |
Output is correct |
30 |
Correct |
198 ms |
10360 KB |
Output is correct |
31 |
Correct |
5 ms |
256 KB |
Output is correct |
32 |
Incorrect |
5 ms |
256 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |