Submission #1288520

#TimeUsernameProblemLanguageResultExecution timeMemory
1288520LIATricks of the Trade (CEOI23_trade)C++17
25 / 100
1155 ms2162688 KiB
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define tp tuple<ll, ll, ll>
const ll inf = 1e18;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  ll n, k, ans = -1e18;
  cin >> n >> k;
  vll b(n), s(n);
  for (ll i = 0; i < n; ++i)
    cin >> b[i];
  for (ll i = 0; i < n; ++i)
    cin >> s[i];

  vector<vll> dp(n + 1, vll(k + 1, -inf));
  for (ll i = 0; i <= n; ++i)
    dp[i][0] = 0;

  vll pref(n + 1, 0);
  for (ll i = 1; i <= n; ++i)
    pref[i] = pref[i - 1] + b[i - 1];

  vector<vector<int>> par(n + 1, vector<int>(k + 1, -1));

  for (ll j = 1; j <= k; ++j) {
    ll best = (j == 1 ? 0 : inf);
    int best_pos = (j == 1 ? 0 : -1);
    for (ll i = 1; i <= n; ++i) {
      if (best != inf) {
        dp[i][j] = s[i - 1] - pref[i] - best;
        par[i][j] = best_pos;
      }
      ll cand = -(dp[i][j - 1] + pref[i]);
      if (cand < best) {
        best = cand;
        best_pos = i;
      }
    }
  }
  int at = 1;
  for (ll i = 1; i <= n; ++i)
    if (dp[i][k] > ans) {
      ans = dp[i][k];
      at = i;
    }

  set<ll> in;

  ll cur = ans;
  ll took = 0;
  ll ii = at, jj = k;
  while (took != k) {
    for (ll i = 1; i <= n; ++i) {
      if (i == ii) {
        in.insert(i);
        ++took;
        ii = par[ii][jj];
        --jj;
        break;
      }
    }
  }

  cout << ans << endl;
  for (ll i = 1; i <= n; ++i)
    cout << (in.count(i) ? '1' : '0');
  cout << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...