#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("Ofast")
#define TASK ""
#define ll long long
#define ull unsigned ll
#define db long double
#define pLL pair<ll, ll>
#define pLI pair<ll, int>
#define pIL pair<int, ll>
#define pII pair<int, int>
#define vec vector
#define vL vec<ll>
#define vvL vec<vL>
#define vI vec<int>
#define vvI vec<vI>
#define vvvI vec<vvI>
#define vvvvI vec<vvvI>
#define vD vec<db>
#define vvD vec<vD>
#define vLL vec<pLL>
#define vLI vec<pLI>
#define vIL vec<pIL>
#define vII vec<pII>
#define vvII vec<vII>
#define vS vec<string>
#define vvS vec<vS>
#define vB vec<bool>
#define vvB vec<vB>
#define umap unordered_map
#define gphash gp_hash_table
#define mset multiset
#define pque priority_queue
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define stt(a, n) a.begin(), a.begin() + n
#define stf(a, n) a.begin() + n, a.end()
#define eb emplace_back
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ins insert
#define asg assign
#define rev reverse
#define fi first
#define se second
#define th third
#define ub upper_bound
#define lb lower_bound
#define ite iterator
#define fs(n) fixed << setprecision(n)
using namespace std;
using namespace __gnu_pbds;
const ll llINF = 1e18;
const int intINF = 1e9;
const ll MOD = 1e9 + 7;
const ll LIM = 2e5;
template< class T >
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define oset ordered_set
template< class A,
class B,
class C > struct triple {
A fi; B se; C th;
triple() {}
triple(A a,B b,C c) : fi(a), se(b), th(c) {}
};
#define tIII triple<int, int, int>
#define tLLL triple<ll, ll, ll>
#define vIII vec<tIII>
#define vvIII vec<vIII>
#define vLLL vec<tLLL>
mt19937 Rand(chrono::high_resolution_clock::now().time_since_epoch().count());
vI prime, lpf;
void primeSieve(int n) { prime.asg(1, 2); lpf.asg(n + 1, 2); lpf[0] = lpf[1] = 1;
for (int i = 3; i <= n; i += 2) { if (lpf[i] == 2) { lpf[i] = i; prime.pb(i); }
for (int j = 0; j < prime.size() && i * prime[j] <= n && prime[j] <= lpf[i]; ++ j) lpf[i * prime[j]] = prime[j];
} }
vvI dvs;
void dvsSieve(int n) { dvs.asg(n + 1, vI());
for (int i = 1; i <= n; ++ i) {
for (int j = i; j <= n; j += i)
dvs[j].pb(i);
} }
template< class T > bool maximize(T &a, T b) { if (b > a) return a = b, 1; return 0; }
template< class T > bool minimize(T &a, T b) { if (b < a) return a = b, 1; return 0; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll fastPow(ll n, ll p,
ll m = MOD) { ll r = 1; for (n %= m; p; p >>= 1) { if (p & 1) (r *= n) %= m; (n *= n) %= m; } return r; }
ll invMod(ll n,
ll m = MOD) { return fastPow(n, m - 2, m); }
ll mask(int i) { return 1LL << i; }
bool bit(ll n, int i) { return n >> i & 1LL; }
#define popcount __builtin_popcountll
#define clz __builtin_clzll
#define ctz __builtin_ctzll
//__________________________________________________________________________________________________//
void init() {}
int N;
struct SegmentTree {
vL seg;
SegmentTree() {}
SegmentTree(int n) { seg.asg(n + 1 << 2, llINF); }
void update(int i, ll v, int l = 0, int r = N, int id = 1) {
if (i < l || r < i) return;
if (l == r) { seg[id] = v; return; }
int m = l + r >> 1;
update(i, v, l, m, id << 1);
update(i, v, m + 1, r, id << 1 | 1);
seg[id] = min(seg[id << 1], seg[id << 1 | 1]);
}
ll get(int L, int R, int l = 0, int r = N, int id = 1) {
if (R < l || r < L) return llINF;
if (L <= l && r <= R) return seg[id];
int m = l + r >> 1;
ll lGet = get(L, R, l, m, id << 1);
ll rGet = get(L, R, m + 1, r, id << 1 | 1);
return min(lGet, rGet);
}
};
void solve() {
int K; cin >> N >> K;
vI A(N + 1); A[0] = intINF;
for (int i = 1; i <= N; ++ i) cin >> A[i];
vec<SegmentTree> DP(K + 1, SegmentTree(N + 1)); DP[0].update(0, 0);
vec<vIL> stk(K, vIL(1, {0, llINF})); vec<mset<ll>> sech(K);
for (int i = 0; i < K; ++ i) sech[i].ins(llINF);
for (int i = 1; i <= N; ++ i) {
for (int j = 0; j < K; ++ j) {
while (A[i] >= A[stk[j].back().fi]) {
sech[j].erase(sech[j].find(stk[j].back().se));
stk[j].popb();
}
stk[j].eb(i, DP[j].get(stk[j].back().fi, i - 1) + A[i]);
sech[j].ins(stk[j].back().se);
DP[j + 1].update(i, *sech[j].begin());
}
}
cout << DP[K].get(N, N);
}
signed main() {
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
init();
int tests = 1;
//cin >> tests;
while (tests --) solve();
return 0;
}
Compilation message
blocks.cpp: In function 'void primeSieve(int)':
blocks.cpp:87:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int j = 0; j < prime.size() && i * prime[j] <= n && prime[j] <= lpf[i]; ++ j) lpf[i * prime[j]] = prime[j];
| ~~^~~~~~~~~~~~~~
blocks.cpp: In constructor 'SegmentTree::SegmentTree(int)':
blocks.cpp:123:36: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
123 | SegmentTree(int n) { seg.asg(n + 1 << 2, llINF); }
| ~~^~~
blocks.cpp: In member function 'void SegmentTree::update(int, long long int, int, int, int)':
blocks.cpp:129:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
129 | int m = l + r >> 1;
| ~~^~~
blocks.cpp: In member function 'long long int SegmentTree::get(int, int, int, int, int)':
blocks.cpp:140:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
140 | int m = l + r >> 1;
| ~~^~~
blocks.cpp: In function 'int main()':
blocks.cpp:174:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
174 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:175:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
175 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
464 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
716 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
464 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
344 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
716 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
860 KB |
Output is correct |
58 |
Correct |
31 ms |
9052 KB |
Output is correct |
59 |
Correct |
11 ms |
5080 KB |
Output is correct |
60 |
Correct |
60 ms |
13404 KB |
Output is correct |
61 |
Correct |
446 ms |
109648 KB |
Output is correct |
62 |
Runtime error |
108 ms |
262144 KB |
Execution killed with signal 9 |
63 |
Halted |
0 ms |
0 KB |
- |