#include<bits/stdc++.h>
#define taskname "B"
using namespace std;
const int INF = 1e9;
const int lim = 1e5 + 5;
template<class T>void minimize(T& a, T b){
if(a > b){
a = b;
}
}
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
int n, k, a[lim];
namespace sub123{
void solve(){
vector<int>dp(n + 1, INF);
for(int _ = dp[0] = 0; _ < k; _++){
vector<int>ndp(n + 1, INF);
for(int i = 1; i <= n; i++){
int cur_max = 0;
for(int j = i; j > 0; j--){
maximize(cur_max, a[j]);
minimize(ndp[i], dp[j - 1] + cur_max);
}
}
swap(dp, ndp);
}
cout << dp[n];
}
}
namespace sub4{
int dp[lim], log_v[lim], spt[lim][17];
int get_min(int l, int r){
int k = log_v[r - l + 1];
return min(spt[l][k], spt[r - (1 << k) + 1][k]);
}
void solve(){
log_v[0] = -1;
for(int i = 1; i < lim; i++){
log_v[i] = log_v[i >> 1] + 1;
}
memset(dp, 0x3f, sizeof(dp));
for(int _ = dp[0] = 0; _ < k; _++){
for(int i = 0; i <= n; i++){
spt[i][0] = dp[i];
}
for(int j = 1; j < 17; j++){
for(int i = 0; i + (1 << j) - 1 <= n; i++){
spt[i][j] = min(spt[i][j - 1], spt[i + (1 << (j - 1))][j - 1]);
}
}
dp[0] = INF;
stack<int>st;
for(int i = 1; i <= n; st.push(i++)){
while(!st.empty() && a[st.top()] <= a[i]){
st.pop();
}
int left = (st.empty() ? 0 : st.top());
dp[i] = min(dp[left], get_min(left, i - 1) + a[i]);
}
}
cout << dp[n];
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n >> k;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
if(n <= 100){
sub123::solve();
}
else{
sub4::solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
blocks.cpp: In function 'int main()':
blocks.cpp:71:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |