This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define uwu "blocks"
typedef long long ll;
#define pb push_back
#define endl '\n'
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
const int N = 1e5 + 3;
const int INF = 1e9 + 7;
int n, k, a[N];
int dp[N][104];
void mini(int &cur, int val){
if (cur > val) cur = val;
}
void solve(void){
memset(dp, 0x3f, sizeof(dp)); dp[0][1] = 0;
FOR(i, 1, n) dp[i][1] = max(dp[i - 1][1], a[i]);
FOR(j, 2, k){
stack <pair<int, int>> st;
FOR(i, j, n){
int val = dp[i - 1][j - 1];
while(!st.empty() && a[st.top().first] <= a[i]){
mini(val, st.top().second);
st.pop();
}
int L = st.empty() ? 0 : st.top().first;
dp[i][j] = min(dp[L][j], val + a[i]);
st.push(make_pair(i, val));
}
}
cout << dp[n][k];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(uwu".inp","r")){
freopen(uwu".inp","r",stdin);
freopen(uwu".out","w",stdout);
}
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
solve();
cerr << "\nTime used: " << clock() << "ms\n";
return 0;
}
Compilation message (stderr)
blocks.cpp: In function 'int main()':
blocks.cpp:44:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen(uwu".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
blocks.cpp:45:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen(uwu".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |