제출 #832888

#제출 시각아이디문제언어결과실행 시간메모리
832888fanwenK개의 묶음 (IZhO14_blocks)C++17
100 / 100
135 ms3540 KiB
#include <bits/stdc++.h>

using namespace std;

#define MASK(x) (1LL << (x))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0, _n = n; i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define FORE(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define debug(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }

template <class A, class B> bool minimize(A &a, B b)  { if (a > b) { a = b; return true; } return false; }
template <class A, class B> bool maximize(A &a, B b)  { if (a < b) { a = b; return true; } return false; }

const int MAXN = 1e5 + 5;

int n, k, a[MAXN];

void you_make_it(void) {
    cin >> n >> k;
    FOR(i, 1, n) cin >> a[i];
    vector <long long> dp(n + 1, 1e18);
    FOR(i, 1, n) dp[i] = max((i == 1 ? a[i] : dp[i - 1]), 1LL * a[i]);
    FORE(j, 1, k) {
    	vector <long long> new_dp(dp);
    	stack <pair <int, long long>> st;
    	FOR(i, 1, n) {
    		long long _min = dp[i - 1];
    		// new_dp[i] = dp[i - 1] + a[i];
    		while(!st.empty() and a[st.top().first] <= a[i]) {
    			_min = min(_min, st.top().second);
    			st.pop();
    		}
    		if(!st.empty()) new_dp[i] = min(new_dp[st.top().first], _min + a[i]);
    		else new_dp[i] = _min + a[i];
    		st.push(make_pair(i, _min));
    	}
    	dp = new_dp;
    }
    cout << dp[n];
}

signed main() {

#ifdef LOCAL
    freopen("TASK.inp", "r", stdin);
    freopen("TASK.out", "w", stdout);
#endif
    auto start_time = chrono::steady_clock::now();

    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);

    you_make_it();

    auto end_time = chrono::steady_clock::now();

    cerr << "\nExecution time : " << chrono::duration_cast <chrono::milliseconds> (end_time - start_time).count() << "[ms]" << endl;

    return (0 ^ 0);
}

// Dream it. Wish it. Do it.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...