| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 | 
|---|---|---|---|---|---|---|---|
| 1038204 | Beerus13 | K개의 묶음 (IZhO14_blocks) | C++14 | 0 ms | 0 KiB | 
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b)  for(int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for(int i = (b), _a = (a); i >= _a; --i)
#define REP(i, a, b)  for(int i = (a), _b = (b); i < _b;  ++i)
#define REPD(i, b, a) for(int i = (b), _a = (a); i > _a;  --i)
#define MASK(i) (1LL << (i))
#define BIT(mask, i) (((mask) >> (i)) & 1)
#define __builtin_popcount __builtin_popcountll
#define all(val) val.begin(), val.end()
#define ll long long
#define ull unsigned long long
#define lb long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
#define cd complex<double>
const double PI = acos(-1);
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll Rand(ll l, ll r) {return l + rng() % (r - l + 1);}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<pii, null_type,less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// - insert(x),erase(x)
// - find_by_order(k): return iterator to the k-th smallest element
// - order_of_key(x): the number of elements that are strictly smaller
template<class X, class Y>
    bool minimize(X &x, const Y &y) {
        if (x > y) {
            x = y;
            return true;
        } else return false;
    }
template<class X, class Y>
    bool maximize(X &x, const Y &y) {
        if (x < y) {
            x = y;
            return true;
        } else return false;
    }
template<class T>
    T Abs(const T &x) {
        return (x < 0 ? -x : x);
    }
const int mod = 1e9 + 7;
const int inf = 1e9;
const int N = 1e5 + 5;
const int K = 105;
int n, k, a[N];
ll dp[N], dp_before[N];
void process() {
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];
    dp[0] = 0;
    FOR(i, 1, n) dp[i] = max(dp[i - 1], a[i]);
    FOR(j, 2, k) {
        swap(dp_before, dp);
        memset(dp, 0x3f, sizeof(dp));
        stack<pii> st;
        st.emplace(0, 0);
        a[0] = inf;
        FOR(i, j, n) {
            ll res = dp_before[i - 1];
            while(a[st.top().fi] <= a[i]) {
                minimize(res, st.top().se);
                st.pop();
            }
            dp[i] = min(dp[st.top().se], res + a[i]);
            st.emplace(i, res);
        }
    }
    cout << dp[n] << '\n';
}
signed main() { 
    if(fopen("test.inp","r")) {
        freopen("test.inp","r",stdin);
        freopen("test.out","w",stdout);
    }
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    
    // clock_t start = clock()
    int ntests = 1;
    // cin >> ntests;
    while(ntests--) process();
    // cerr << "Time elapsed: " << clock() - start << " ms!\n";
    return 0;
}
// https://oj.uz/problem/view/IZhO14_blocks
