Submission #1340916

#TimeUsernameProblemLanguageResultExecution timeMemory
1340916iamhereforfunK blocks (IZhO14_blocks)C++20
53 / 100
1041 ms78548 KiB
// Starcraft 2 enjoyer //

#include <bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

#define LSOne(X) ((X) & -(X))

const int N = 1e5 + 5;
const int K = 1e2 + 5;
const int M = 2e5 + 5;
const int LG = 20;
const long long INF = 1e18 + 5;
const int C = 26;
const int B = 1000;
const int MOD = 998244353;

int n, k, a[N], dp1[K], dp2[K];
vector<pair<int, long long>> best[K];
multiset<long long> st[K];

inline void solve()
{
    cin >> n >> k;
    for (int x = 1; x <= n; x++)
    {
        cin >> a[x];
    }
    for (int x = 0; x <= k; x++)
    {
        dp1[x] = -1;
        dp2[x] = -1;
    }
    best[0].push_back({0, 0});
    st[0].insert(0);
    a[0] = 0;
    for (int x = 1; x <= n; x++)
    {
        for (int y = k; y >= 0; y--)
        {
            long long mn = INF;
            while (!best[y].empty())
            {
                // cout << y << "\n";
                if (best[y].back().first <= a[x])
                {
                    // cout << k << " " << best[y].back().first << " " << best[y].back().second << "\n";
                    mn = min(mn, best[y].back().second);
                    st[y].erase(st[y].find(best[y].back().second + best[y].back().first));
                    best[y].pop_back();
                }
                else
                {
                    break;
                }
            }
            if (!best[y].empty())
            {
                mn = min(mn, *st[y].begin() - a[x]);
            }
            if (mn == INF)
            {
                continue;
            }
            st[y].insert(mn + a[x]);
            best[y].push_back({a[x], mn});
            best[y + 1].push_back({0, mn + a[x]});
            st[y + 1].insert(mn + a[x]);
        }
    }
    cout << *st[k].begin();
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    for (int x = 1; x <= t; x++)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...