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>
#define el '\n'
#define fi first
#define sc second
#define int ll
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
const int mod=1e9+7;
const int N=1e5+11;
int n, a[N], k, dp[102][N];
void sub1()
{
for(int i=1;i<=k;i++) for(int j=1;j<=n;j++) dp[i][j]=1e18;
int mx=0;
for(int i=1;i<=n;i++)
{
mx=max(mx, a[i]);
dp[1][i]=mx;
}
for(int i=2;i<=k;i++)
{
for(int j=1;j<=n;j++)
{
int mx=0;
for(int o=j;o>=i;o--)
{
mx=max(mx, a[o]);
dp[i][j]=min(dp[i][j], dp[i-1][o-1]+mx);
}
}
}
cout << dp[k][n];
}
void sub2()
{
for(int i=0;i<=k;i++) for(int j=0;j<=n;j++) dp[i][j]=1e18;
int mx=0;
for(int i=1;i<=n;i++)
{
mx=max(mx, a[i]);
dp[1][i]=mx;
}
for(int i=2;i<=k;i++)
{
stack<pii> st;
for(int j=i;j<=n;j++)
{
int s=dp[i-1][j-1];
while(!st.empty() && a[st.top().fi]<=a[j])
{
s=min(s, st.top().sc);
st.pop();
}
if(st.empty() || s+a[j]<a[st.top().fi]+st.top().sc) st.emplace(j, s);
dp[i][j]=a[st.top().fi]+st.top().sc;
}
}
cout << dp[k][n];
}
void sol()
{
cin >> n >> k;
for(int i=1;i<=n;i++) cin >> a[i];
if(n<=100) sub1();
else sub2();
// sub2();
}
signed main()
{
// freopen("divisor.INP", "r", stdin);
// freopen("divisor.OUT", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--)
{
sol();
}
}
# | 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... |