/*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]);
dp[i][j] = min(dp[i][j], dp[lst[i]][j - 1] + a[i]);
if (lst[i] != i - 1)
dp[i][j] = min(dp[i][j], dp[lst[i] + 1][j - 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 |
376 KB |
Output is correct |
11 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
15608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |