#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define REP(i,a,b) for(int i=(a); i>=(b); --i)
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(i) (1LL << (i))
#define sz(v) (int)(v).size()
#define ALL(v) (v).begin(),(v).end()
#define printArr(a, n) for (int i=1; i<=n; ++i) cout << a[i] << " "; el;
#define el cout << "\n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int base = 311;
const ll INF = 1e18;
/// REXDINO FROM LTV HSGS ///
int n, k, a[N];
stack<pii> st;
ll dp[100005][105];
void solve(){
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
memset(dp, 0x3f, sizeof dp);
dp[0][0] = 0;
FOR(j, 1, k) {
stack<pll> st;
FOR(i, 1, n){
ll mn = dp[i - 1][j - 1];
while(!st.empty() && a[st.top().fi] <= a[i]){
mn = min(mn, st.top().se);
st.pop();
}
dp[i][j] = mn + a[i];
if (!st.empty()) dp[i][j] = min(dp[i][j], dp[st.top().fi][j]);
st.push({i, mn});
}
}
cout << dp[n][k];
}
signed main(){
#define NAME "blocks"
if (fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
blocks.cpp: In function 'int main()':
blocks.cpp:55:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:56:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | freopen(NAME".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... |