제출 #854795

#제출 시각아이디문제언어결과실행 시간메모리
854795NeroZeinK개의 묶음 (IZhO14_blocks)C++17
0 / 100
1 ms460 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<int> a(n), dp(n); for (int i = 0, mx = 0; i < n; ++i) { cin >> a[i]; mx = max(mx, a[i]); dp[i] = mx; } stack<pair<int, long long>> stk; for (int j = 1; j < k; ++j) { stk.push({a[j - 1], dp[j - 1]}); for (int i = j; i < n; ++i) { long long mn = LLONG_MAX; while (!stk.empty() && stk.top().first <= a[i]) { mn = min(mn, stk.top().second); stk.pop(); } if (mn != LLONG_MAX && (stk.empty() || stk.top().first + stk.top().second > a[i] + mn)) { stk.push({a[i], mn}); } int val = stk.top().first + stk.top().second; if (stk.empty() || stk.top().first + stk.top().second > a[i] + dp[i]) { stk.push({a[i], dp[i]}); } dp[i] = val; } } cout << dp[n - 1] << '\n'; 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...