#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,k;
vector<ll> W;
vector<vector<ll>> dp(2, vector<ll>(300005, -1));
vector<vector<ll>> dpcnt(2, vector<ll>(300005, -1));
ll pen;
ll solve(bool w, int i) {
if(dp[w][i] != -1) return dp[w][i];
if(i >= n) return 0;
ll ans = numeric_limits<int>::min();
if(w == false) {
if(solve(false, i+1) > ans) {
dpcnt[w][i] = dpcnt[w][i+1];
ans = solve(false, i+1);
}
if(-pen + solve(true, i+1) > ans) {
dpcnt[w][i] = 1 + dpcnt[w][i+1];
ans = -pen + solve(true, i+1);
}
} else {
if(W[i] + solve(false, i+1) > ans) {
dpcnt[w][i] = dpcnt[w][i+1];
ans = W[i] + solve(false, i+1);
}
if(W[i] + solve(true, i+1) > ans) {
dpcnt[w][i] = dpcnt[w][i+1];
ans = W[i] + solve(true, i+1);
}
}
dp[w][i] = ans;
return ans;
}
int main() {
cin >> n >> k;
W.resize(n);
for(int i = 0; i < n; i++) {
cin >> W[i];
}
pen = 0;
ll lower = 0;
ll upper = numeric_limits<ll>::max();
while(lower < upper) {
ll mid = (lower + upper)/2;
pen = mid;
dp = vector<vector<ll>>(2, vector<ll>(300005, -1));
dpcnt = vector<vector<ll>>(2, vector<ll>(300005, -1));
ll start = solve(true, 0); ll dstart = solve(false, 0);
if(start >= dstart) {
int cnt = dpcnt[true][0];
if(cnt > k) {
lower = mid;
} else {
upper = mid;
}
} else {
int cnt = dpcnt[true][0];
if(cnt > k) {
lower = mid;
} else {
upper = mid;
}
}
}
pen = lower;
ll start = solve(true, 0); ll dstart = solve(false, 0);
ll cnt;
if(start >= dstart) cnt = dpcnt[true][0];
else cnt = dpcnt[false][0];
cout << max(start, dstart) + cnt*pen;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
830 ms |
39656 KB |
Output is correct |
2 |
Correct |
947 ms |
40064 KB |
Output is correct |
3 |
Execution timed out |
1038 ms |
40288 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1010 ms |
39800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1024 ms |
40204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
116 ms |
19332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
116 ms |
19332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
116 ms |
19332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
830 ms |
39656 KB |
Output is correct |
2 |
Correct |
947 ms |
40064 KB |
Output is correct |
3 |
Execution timed out |
1038 ms |
40288 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |