Submission #1361999

#TimeUsernameProblemLanguageResultExecution timeMemory
1361999Born_To_LaughK blocks (IZhO14_blocks)C++17
0 / 100
0 ms344 KiB
// Born_To_Laugh - Hughie Do
#include <bits/stdc++.h>
#define alle(AC) AC.begin(), AC.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
[[maybe_unused]] const ll MOD = 998244353, INF = 3e18 + 7;
const int maxn = 1e5 + 10;
int n, k;
ll a[maxn], prefm[maxn];
ll dp[maxn][110];
void solve(){
    cin >> n >> k;
    for(int i=0; i<=n; ++i)
        for(int j=0; j<=k; ++j) dp[i][j] = INF;

    dp[0][0] = 0;
    for(int i=1; i<=n; ++i){
        cin >> a[i];
        prefm[i] = max(prefm[i - 1], a[i]);
        dp[i][1] = prefm[i];
    }

    for(int j=2; j<=k; ++j){
        multiset<ll> num;
        vector<int> st;
        for(int i=j; i<=n; ++i){
            while(!st.empty() && a[st.back()] <= a[i]){
                int id = st.back();
                st.pop_back();
                int pid = (st.empty() ? j - 1 : st.back());
                num.erase(num.lower_bound(dp[pid][j - 1] + a[id]));
            }
            int pid = (st.empty() ? j - 1 : st.back());
            int id = i;
            st.push_back(id);
            num.insert(dp[pid][j - 1] + a[id]);
            dp[i][j] = *num.begin();
        }
    }
    cout << dp[n][k] << '\n';
}
signed main(){
    // freopen("inp.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...