/*DavitMarg*/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <stack>
#include <cassert>
#include <iterator>
#include <fstream>
#define mod 1000000007ll
#define LL long long
#define LD long double
#define MP make_pair
#define PB push_back
#define all(v) v.begin(), v.end()
using namespace std;
char readchar()
{
char c = getchar();
while (c <= 33)
{
c = getchar();
}
return c;
}
int readint()
{
char c = getchar();
while (c <= 33)
{
c = getchar();
}
bool sign = false;
if (c == '-')
{
sign = true;
c = getchar();
}
int t = 0;
while (c >= '0' && c <= '9')
{
t = (t << 3) + (t << 1) + c - '0';
c = getchar();
}
return (sign ? -t : t);
}
const int N = 100005;
int n, k;
LL a[N], lst[N], dp[N][105];
LL t[105][4 * N];
void build(int k, int v, int l, int r)
{
t[k][v] = mod * mod;
if (l != r)
{
int m = (l + r) / 2;
build(k, v * 2, l, m);
build(k, v * 2 + 1, m + 1, r);
}
}
void upd(int k, int v, int l, int r, int pos, LL val)
{
if (l == r)
{
t[k][v] = min(val, t[k][v]);
return;
}
int m = (l + r) / 2;
if (pos <= m)
upd(k, v * 2, l, m, pos, val);
else
upd(k, v * 2 + 1, m + 1, r, pos, val);
t[k][v] = min(t[k][v * 2], t[k][v * 2 + 1]);
}
LL get(int k, int v, int l, int r, int i, int j)
{
if (i > j)
return mod * mod;
if (l == i && r == j)
return t[k][v];
int m = (l + r) / 2;
return min(
get(k, v * 2, l, m, i, min(j, m)),
get(k, v * 2 + 1, m + 1, r, max(i, m + 1), j));
}
vector<int> s;
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
a[i] = readint();
a[0] = mod;
s.PB(0);
for (int i = 1; i <= n; i++)
{
while (a[s.back()] <= a[i])
s.pop_back();
lst[i] = s.back();
s.PB(i);
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= k; j++)
dp[i][j] = mod * mod;
for (int j = 0; j <= k; j++)
build(j, 1, 0, n);
dp[0][0] = 0;
upd(0, 1, 0, n, 0, 0);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++)
{
dp[i][j] = min(dp[i][j], dp[lst[i]][j]);
// for (int x = i; x > lst[i]; x--)
// dp[i][j] = min(dp[i][j], dp[x - 1][j - 1] + a[i]);
dp[i][j] = min(dp[i][j], get(j - 1, 1, 0, n, lst[i], i - 1) + a[i]);
upd(j, 1, 0, n, i, dp[i][j]);
}
cout << dp[n][k] << endl;
return 0;
}
/*
2 4
9 10
5 4 5 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
2 ms |
524 KB |
Output is correct |
15 |
Correct |
2 ms |
508 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
504 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
888 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
632 KB |
Output is correct |
15 |
Correct |
4 ms |
1400 KB |
Output is correct |
16 |
Correct |
4 ms |
1400 KB |
Output is correct |
17 |
Correct |
3 ms |
1016 KB |
Output is correct |
18 |
Correct |
4 ms |
1400 KB |
Output is correct |
19 |
Correct |
4 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
15608 KB |
Output is correct |
2 |
Correct |
58 ms |
39676 KB |
Output is correct |
3 |
Correct |
145 ms |
45816 KB |
Output is correct |
4 |
Execution timed out |
1014 ms |
117112 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |