#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define fi first
#define se second
#define pb emplace_back
#define pii pair<int, int>
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x >> (i)) & 1)
#define sz(s) (int)((s).size())
#define all(a) a.begin(), a.end()
#define uni(a) (sort(all(a)), a.resize(unique(all(a))-a.begin()))
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define F0R(i, b) for (int i = 0, _b = (b); i < _b; ++i)
#define FORd(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define F0Rd(i, b) for (int i = (b)-1; i >= 0; i--)
using namespace std;
template<typename T1,typename T2> bool ckmax(T1 &x,const T2 &y){if(x<y){x=y; return 1;} return 0;}
template<typename T1,typename T2> bool ckmin(T1 &x,const T2 &y){if(y<x){x=y; return 1;} return 0;}
const int MOD = (int)1e9 + 7;
const int mod = 998244353;
const int N = 2e5 + 1, M = 101;
const long long INF = (long long)(4e18 + 7);
/*
/\_/\
(= ._.)
/ >? \>$
*/
int n, s, l[N];
long long a[N], dp[N][M];
void sol(void){
cin >> n >> s;
FOR(i, 1, n) cin >> a[i];
stack<int> st;
FOR(i, 1, n){
while(sz(st) && a[st.top()] <= a[i]) st.pop();
l[i] = sz(st) ? st.top() : 0;
st.push(i);
}
memset(dp, 0x3f, sizeof(dp)); dp[1][1] = a[1];
FOR(i, 2, n) dp[i][1] = max(dp[i-1][1], 1LL * a[i]);
FOR(k, 2, s){
deque<int> dq;
FOR(i, 1, n){
dp[i][k] = dp[l[i]][k];
while(sz(dq) && dq.front() < l[i]) dq.pop_front();
if(sz(dq)) dp[i][k] = min(dp[i][k], dp[dq.front()][k-1] + a[i]);
while(sz(dq) && dp[dq.back()][k-1] >= dp[i][k-1]) dq.pop_back();
dq.push_back(i);
}
}
// FOR(j, 1, s) FOR(i, 1, n) cout << dp[i][j] << " \n"[i == n];
cout << dp[n][s] << '\n';
}
signed main(void){
#define TASK "nhap"
if(fopen(TASK".inp", "r")){
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--) sol();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << " ms\n";
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:63:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:64:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
160848 KB |
Output is correct |
2 |
Correct |
34 ms |
160592 KB |
Output is correct |
3 |
Correct |
33 ms |
160604 KB |
Output is correct |
4 |
Correct |
36 ms |
160604 KB |
Output is correct |
5 |
Correct |
32 ms |
160592 KB |
Output is correct |
6 |
Correct |
33 ms |
160596 KB |
Output is correct |
7 |
Correct |
34 ms |
160600 KB |
Output is correct |
8 |
Correct |
36 ms |
160500 KB |
Output is correct |
9 |
Correct |
35 ms |
160596 KB |
Output is correct |
10 |
Correct |
34 ms |
160484 KB |
Output is correct |
11 |
Incorrect |
35 ms |
160492 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
160544 KB |
Output is correct |
2 |
Correct |
36 ms |
160628 KB |
Output is correct |
3 |
Correct |
34 ms |
160592 KB |
Output is correct |
4 |
Correct |
34 ms |
160596 KB |
Output is correct |
5 |
Correct |
35 ms |
160596 KB |
Output is correct |
6 |
Correct |
35 ms |
160556 KB |
Output is correct |
7 |
Correct |
35 ms |
160592 KB |
Output is correct |
8 |
Correct |
34 ms |
160592 KB |
Output is correct |
9 |
Correct |
36 ms |
160592 KB |
Output is correct |
10 |
Correct |
33 ms |
160600 KB |
Output is correct |
11 |
Incorrect |
35 ms |
160532 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
160848 KB |
Output is correct |
2 |
Correct |
34 ms |
160592 KB |
Output is correct |
3 |
Correct |
33 ms |
160604 KB |
Output is correct |
4 |
Correct |
36 ms |
160604 KB |
Output is correct |
5 |
Correct |
32 ms |
160592 KB |
Output is correct |
6 |
Correct |
33 ms |
160596 KB |
Output is correct |
7 |
Correct |
34 ms |
160600 KB |
Output is correct |
8 |
Correct |
36 ms |
160500 KB |
Output is correct |
9 |
Correct |
35 ms |
160596 KB |
Output is correct |
10 |
Correct |
34 ms |
160484 KB |
Output is correct |
11 |
Incorrect |
35 ms |
160492 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
160848 KB |
Output is correct |
2 |
Correct |
34 ms |
160592 KB |
Output is correct |
3 |
Correct |
33 ms |
160604 KB |
Output is correct |
4 |
Correct |
36 ms |
160604 KB |
Output is correct |
5 |
Correct |
32 ms |
160592 KB |
Output is correct |
6 |
Correct |
33 ms |
160596 KB |
Output is correct |
7 |
Correct |
34 ms |
160600 KB |
Output is correct |
8 |
Correct |
36 ms |
160500 KB |
Output is correct |
9 |
Correct |
35 ms |
160596 KB |
Output is correct |
10 |
Correct |
34 ms |
160484 KB |
Output is correct |
11 |
Incorrect |
35 ms |
160492 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |