Submission #854795

# Submission time Handle Problem Language Result Execution time Memory
854795 2023-09-29T02:31:01 Z NeroZein K blocks (IZhO14_blocks) C++17
0 / 100
1 ms 460 KB
#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 time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 460 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 344 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Incorrect 0 ms 348 KB Output isn't correct
6 Halted 0 ms 0 KB -