/*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, LOG[N];
LL a[N], lst[N], dp[N][105];
LL sp[101][N][18];
LL get(int k, int l, int r)
{
int j = LOG[r - l + 1];
//cout << k << " : " << l << " : " << r << " = " << min(sp[k][r][j], sp[k][l + (1 << j) - 1][j]) << endl;
return min(sp[k][r][j], sp[k][l + (1 << j) - 1][j]);
}
vector<int> s;
int main()
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
a[i] = readint();
LOG[1] = 0;
for (int i = 2; i <= n; i++)
LOG[i] = LOG[i / 2] + 1;
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;
dp[0][0] = 0;
for (int j = 0; j <= k; j++)
for (int x = 0; x <= LOG[n]; x++)
sp[j][0][x] = mod * mod;
sp[0][0][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]);
dp[i][j] = min(dp[i][j], get(j - 1, lst[i], i - 1) + a[i]);
}
for (int j = 0; j <= k; j++)
{
sp[j][i][0] = dp[i][j];
for (int x = 1; x <= LOG[n]; x++)
{
sp[j][i][x] = sp[j][i][x - 1];
if (i - (1 << (x - 1)) >= 0)
sp[j][i][x] = min(sp[j][i][x], sp[j][i - (1 << (x - 1))][x - 1]);
}
}
}
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 |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
380 KB |
Output is correct |
12 |
Correct |
2 ms |
508 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 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 |
632 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 |
296 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 |
504 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 |
504 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 |
504 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1528 KB |
Output is correct |
2 |
Correct |
2 ms |
380 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 |
380 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 |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
1016 KB |
Output is correct |
14 |
Correct |
2 ms |
1016 KB |
Output is correct |
15 |
Correct |
4 ms |
2680 KB |
Output is correct |
16 |
Correct |
4 ms |
2680 KB |
Output is correct |
17 |
Correct |
3 ms |
1656 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
45688 KB |
Output is correct |
2 |
Correct |
53 ms |
50296 KB |
Output is correct |
3 |
Correct |
123 ms |
87908 KB |
Output is correct |
4 |
Runtime error |
526 ms |
262144 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
5 |
Halted |
0 ms |
0 KB |
- |