#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define MAXX 100000000010LL
#define MAXN 100010
#define MAXK 110
using namespace std;
ll seg[MAXK][MAXN], dp[MAXN][MAXK], a[MAXN];
int n, k, lv[MAXN];
ll query(int i, int node, int l, int r, int levo, int desno)
{
if (levo>r || desno<l) return MAXX;
if (levo<=l && desno>=r) return seg[i][node];
int mid=l+(r-l)/2;
return min(query(i, 2*node, l, mid, levo, desno),
query(i, 2*node+1, mid+1, r, levo, desno));
}
void upd(int i, int node, int l, int r, int pos, int val)
{
if (pos>r || pos<l) return;
if (l==r) { seg[i][node]=val; return; }
int mid=l+(r-l)/2;
upd(i, 2*node, l, mid, pos, val);
upd(i, 2*node+1, mid+1, r, pos, val);
seg[i][node]=min(seg[i][2*node], seg[i][2*node+1]);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>k;
for (int i=1; i<=n; i++) cin>>a[i];
stack<pii> st;
st.push({1000010, 0});
for (int i=1; i<=n; i++)
{
while (st.top().xx<a[i]) st.pop();
lv[i]=st.top().yy;
//cout<<lv[i]<<" ";
st.push({a[i], i});
}
//cout<<endl;
for (int i=0; i<=n; i++) for (int j=1; j<=k; j++) { dp[i][j]=MAXX; upd(j, 1, 0, n-1, i-1, MAXX); }
dp[1][1]=a[1]; upd(1, 1, 0, n-1, 0, dp[1][1]);
for (int i=2; i<=n; i++)
{
//cout<<i<<endl;
for (int j=1; j<=min(k, i); j++)
{
dp[i][j]=query(j-1, 1, 0, n-1, lv[i], i-2)+a[i];
//cout<<dp[i][j]<<" ";
dp[i][j]=min(dp[i][j], dp[lv[i]][j]);
//cout<<dp[i][j]<<endl;
}
for (int j=i+1; j<=k; j++) dp[i][j]=MAXX;
for (int j=1; j<=k; j++) upd(j, 1, 0, n-1, i-1, dp[i][j]);
}
cout<<dp[n][k];
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:8:14: warning: overflow in conversion from 'long long int' to 'int' changes value from '100000000010' to '1215752202' [-Woverflow]
8 | #define MAXX 100000000010LL
| ^~~~~~~~~~~~~~
blocks.cpp:56:95: note: in expansion of macro 'MAXX'
56 | for (int i=0; i<=n; i++) for (int j=1; j<=k; j++) { dp[i][j]=MAXX; upd(j, 1, 0, n-1, i-1, MAXX); }
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |