//Link :
//Author: Nguyen Dinh Manh
#include <bits/stdc++.h>
using namespace std;
#define timing "Time collapse : " << fixed << setprecision(3) << 1.000 * clock() / CLOCKS_PER_SEC
#define int long long
#define ar array< int , 2>
#define MASK(i) (1 << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define task ""
template<typename T1, typename T2> bool minN(T1 &a, T2 b) {if (a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxX(T1 &a, T2 b) {if (a < b) a = b; else return 0; return 1;}
const int MAX = 100011, INFL = 1e9 + 7, INFLL = 1e18 + 7;
int n, f[MAX][111], a[MAX], k;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen(task".inp","r", stdin); freopen(task".out","w",stdout);
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
memset(f, 0x3f, sizeof f);
f[1][0] = 0;
for (int i = 1; i <= n; i++)
minN(f[1][i], max(f[1][i - 1], a[i]));
for (int i = 2; i <= k; i++)
{
int minF;
stack< ar > s;
for (int j = 1; j <= n; j++)
{
minF = f[i - 1][j - 1];
while (!s.empty() && s.top()[1] <= a[j])
{
minN(minF, f[i][s.top()[0]]);
s.pop();
}
f[i][j] = min(f[i][(s.empty() ? 0 : s.top()[0])], minF + a[i]);
s.push({j, a[j]});
}
}
cout << f[k][n] ;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87116 KB |
Output is correct |
2 |
Correct |
34 ms |
87180 KB |
Output is correct |
3 |
Correct |
36 ms |
87212 KB |
Output is correct |
4 |
Correct |
37 ms |
87188 KB |
Output is correct |
5 |
Incorrect |
38 ms |
87100 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
87236 KB |
Output is correct |
2 |
Correct |
33 ms |
87120 KB |
Output is correct |
3 |
Correct |
36 ms |
87120 KB |
Output is correct |
4 |
Correct |
37 ms |
87088 KB |
Output is correct |
5 |
Incorrect |
34 ms |
87104 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87116 KB |
Output is correct |
2 |
Correct |
34 ms |
87180 KB |
Output is correct |
3 |
Correct |
36 ms |
87212 KB |
Output is correct |
4 |
Correct |
37 ms |
87188 KB |
Output is correct |
5 |
Incorrect |
38 ms |
87100 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
87116 KB |
Output is correct |
2 |
Correct |
34 ms |
87180 KB |
Output is correct |
3 |
Correct |
36 ms |
87212 KB |
Output is correct |
4 |
Correct |
37 ms |
87188 KB |
Output is correct |
5 |
Incorrect |
38 ms |
87100 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |