#include<iostream>
#define ll long long
using namespace std;
int main(){
int n,k;
cin >> n >> k;
ll arr[n];
ll pre[n+1];
int negc = 0;
int a;
bool ans = false;
for(int x=0; x<n; x++){
cin >> a; arr[x] = a;
pre[x] = 0;
if(a<0) ++negc;
}
pre[n] = 0;
ll total = 0;
if(negc==0&&!ans){
for(int x=0; x<n; x++){
total += arr[x];
}
cout << total;
ans = true;
}
if(negc==1&&!ans){
if(k>=2){
for(int x=0; x<n; x++){
if(arr[x]>0){
total += arr[x];
}
}
cout << total;
}
else{
ll all = 0;
for(int x=0; x<n; x++){
all += arr[x];
}
ll before,after = 0;
bool reached = false;
for(int x=0; x<n; x++){
if(arr[x]<0){
reached = true;
continue;
}
if(reached){
after += arr[x];
}
else{
before += arr[x];
}
}
total = max(all,max(before,after));
}
ans = true;
}
if(k==1&&!ans){
pre[0] = 0;
for(int x=1; x<n+1; x++){
pre[x] = pre[x-1]+arr[x-1];
}
int l,r = 0;
while(r<n){
total = max(total,pre[r]-pre[l]);
if(l==r){
++r;
continue;
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
4812 KB |
Output is correct |
2 |
Correct |
119 ms |
7708 KB |
Output is correct |
3 |
Correct |
137 ms |
7752 KB |
Output is correct |
4 |
Correct |
160 ms |
7720 KB |
Output is correct |
5 |
Correct |
124 ms |
7700 KB |
Output is correct |
6 |
Correct |
133 ms |
7628 KB |
Output is correct |
7 |
Correct |
146 ms |
7620 KB |
Output is correct |
8 |
Correct |
127 ms |
7704 KB |
Output is correct |
9 |
Correct |
121 ms |
7620 KB |
Output is correct |
10 |
Correct |
135 ms |
7676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
4812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
129 ms |
4812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
4812 KB |
Output is correct |
2 |
Correct |
119 ms |
7708 KB |
Output is correct |
3 |
Correct |
137 ms |
7752 KB |
Output is correct |
4 |
Correct |
160 ms |
7720 KB |
Output is correct |
5 |
Correct |
124 ms |
7700 KB |
Output is correct |
6 |
Correct |
133 ms |
7628 KB |
Output is correct |
7 |
Correct |
146 ms |
7620 KB |
Output is correct |
8 |
Correct |
127 ms |
7704 KB |
Output is correct |
9 |
Correct |
121 ms |
7620 KB |
Output is correct |
10 |
Correct |
135 ms |
7676 KB |
Output is correct |
11 |
Incorrect |
57 ms |
4812 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |