Submission #558779

#TimeUsernameProblemLanguageResultExecution timeMemory
558779KiriLL1caK blocks (IZhO14_blocks)C++17
0 / 100
1 ms324 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fr first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pw(x) (1ll << x)
#define sz(x) (int)((x).size())
#define pb push_back
#define endl '\n'
#define vec vector

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;

template <typename T> inline bool umin (T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; }
template <typename T> inline bool umax (T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }

template <typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

inline void solve () {
      int n, k; cin >> n >> k;
      vector <int> a (n + 1);
      for (int i = 1; i <= n; ++i) cin >> a[i];
      vector <vector <int>> dp (k + 1, vector <int> (n + 1));
      for (int i = 1; i <= n; ++i) dp[1][i] = max(dp[1][i - 1], a[i]);
      for (int j = 2; j <= k; ++j) {
            stack <pii> v;
            for (int i = j; i <= n; ++i) {
                  int mn = dp[j - 1][i - 1];
                  while (sz(v) && v.top().fr <= a[i]) {
                        umin(mn, v.top().sc); v.pop();
                  }
                  if (!sz(v) || v.top().fr + v.top().sc > mn + a[i]) v.push({a[i], mn});
                  dp[j][i] = mn + a[i];
            }
      }
      cout << dp[k][n] << endl;
}

signed main() {
      ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
      int t = 1; //cin >> t;
      while (t--) 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...