/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author Miguel Mini
*/
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <stack>
#define sz(x) (int)x.size()
#define trav(v, x) for (auto v : x)
#define re(x, y, z) for (int x=y; x<z; ++x)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define set_to(x, v) fill(all(x), v)
#define eb emplace_back
#define lso(x) ((x)&-(x))
#define mset(m ,v) memset(m, v, sizeof(m))
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
const int mod = 1e9 + 7;
const ll inf = 1.e18;
const int maxn = 1e5 + 10;
int a[maxn];
ll dp[maxn][2];
class IZHO_14_blocks {
public:
void solveOne(istream& in, ostream& out) {
int n, k;
in >> n >> k;
re(i, 0, n) {
in >> a[i];
}
for (int i = 0; i < n; ++i) {
dp[i][1] = i == 0 ? a[i] : max(0ll + a[i], dp[i-1][1]);
}
for (int i = 2; i <= k; ++i) {
stack<ll> s1({inf});
stack<ll> s2({inf});
ll temp = inf;
for (int j = 0; j < n; ++j) {
while (!s1.empty() && s1.top() <= a[j]) {
temp = min(temp, s2.top() - s1.top());
s1.pop();
s2.pop();
}
s1.push(a[j]);
s2.push(min(s2.top(), a[j] + temp));
temp = dp[j][(i&1)^1];
dp[j][i&1] = s2.top();
}
}
out << dp[n-1][k&1] << endl;
}
void solve(istream& in, ostream& out) {
int testNumber = 1;
//in >> testNumber;
re(tc, 1, testNumber+1) {
//out << "Case #" << tc << ": ";
solveOne(in, out);
}
}
};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
IZHO_14_blocks solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
512 KB |
Output is correct |
2 |
Correct |
10 ms |
1536 KB |
Output is correct |
3 |
Correct |
14 ms |
1536 KB |
Output is correct |
4 |
Correct |
56 ms |
1536 KB |
Output is correct |
5 |
Correct |
160 ms |
3064 KB |
Output is correct |
6 |
Correct |
20 ms |
2944 KB |
Output is correct |
7 |
Correct |
78 ms |
3072 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
4 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
404 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
23 ms |
640 KB |
Output is correct |
15 |
Correct |
7 ms |
640 KB |
Output is correct |
16 |
Correct |
10 ms |
640 KB |
Output is correct |
17 |
Correct |
11 ms |
1536 KB |
Output is correct |
18 |
Correct |
16 ms |
1536 KB |
Output is correct |
19 |
Correct |
76 ms |
1536 KB |
Output is correct |
20 |
Correct |
207 ms |
3064 KB |
Output is correct |
21 |
Correct |
21 ms |
2944 KB |
Output is correct |
22 |
Correct |
101 ms |
2944 KB |
Output is correct |
23 |
Correct |
18 ms |
2944 KB |
Output is correct |
24 |
Correct |
34 ms |
2944 KB |
Output is correct |
25 |
Correct |
212 ms |
2944 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
384 KB |
Output is correct |
28 |
Correct |
20 ms |
768 KB |
Output is correct |
29 |
Correct |
6 ms |
640 KB |
Output is correct |
30 |
Correct |
9 ms |
640 KB |
Output is correct |
31 |
Correct |
10 ms |
1536 KB |
Output is correct |
32 |
Correct |
15 ms |
1792 KB |
Output is correct |
33 |
Correct |
66 ms |
1832 KB |
Output is correct |
34 |
Correct |
187 ms |
3612 KB |
Output is correct |
35 |
Correct |
21 ms |
3636 KB |
Output is correct |
36 |
Correct |
94 ms |
3608 KB |
Output is correct |
37 |
Correct |
6 ms |
640 KB |
Output is correct |
38 |
Correct |
20 ms |
640 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
4 ms |
384 KB |
Output is correct |
42 |
Correct |
4 ms |
384 KB |
Output is correct |