Submission #25850606


Source Code Expand

#include <bits/stdc++.h>
#include <cassert>
typedef long long int ll;
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;

// @@ !! LIM()

int main(/* int argc, char *argv[] */) {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << setprecision(20);

  ll N, R; cin >> N >> R;
  if (R > N / 2) R = N - R;
  vector<ll> A(N+1), B(N), P(N);
  for (ll i = 1; i < N; i++) cin >> A[i];
  for (ll i = 0; i < N; i++) B[i] = A[i] + A[i + 1];
  using sta = tuple<ll, ll, ll>;
  priority_queue<sta> pque;
  for (ll i = 0; i < N; i++) {
    P[i] = i;
    pque.emplace(B[i], i, i);
  }
  ll ans = 0, cnt = 0;
  while (true) {
    auto [m, p, q] = pque.top(); pque.pop();
    if (P[p] != q || P[q] != p) continue;
    ans += m;
    if (++cnt == R) break;
    assert(! (q == N-1 && p == 0));
    if (q == N-1) {
      assert(p - 1 >= 0);
      P[p - 1] = -1;
    }else if (p == 0) {
      assert(q + 1 < N);
      P[q + 1] = -1;
    }else {
      ll u = P[p - 1];
      ll v = P[q + 1];
      ll new_m = B[u] + B[v] - m;
      pque.emplace(new_m, u, v);
      P[u] = v;
      P[v] = u;
      B[u] = B[v] = new_m;
    }
  }
  cout << ans << endl;

  return 0;
}

Submission Info

Submission Time
Task H - Red and Blue Lamps
User yamate11
Language C++ (GCC 9.2.1)
Score 600
Code Size 1184 Byte
Status AC
Exec Time 85 ms
Memory 14172 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 28
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 8 ms 3516 KiB
random_01.txt AC 21 ms 6320 KiB
random_02.txt AC 24 ms 5872 KiB
random_03.txt AC 49 ms 9320 KiB
random_04.txt AC 19 ms 6004 KiB
random_05.txt AC 31 ms 6360 KiB
random_06.txt AC 18 ms 5856 KiB
random_07.txt AC 40 ms 13752 KiB
random_08.txt AC 67 ms 13840 KiB
random_09.txt AC 9 ms 4000 KiB
random_10.txt AC 52 ms 13236 KiB
random_11.txt AC 39 ms 14112 KiB
random_12.txt AC 55 ms 14172 KiB
random_13.txt AC 84 ms 14172 KiB
random_14.txt AC 85 ms 14064 KiB
random_15.txt AC 55 ms 14108 KiB
random_16.txt AC 40 ms 14108 KiB
random_17.txt AC 35 ms 13976 KiB
random_18.txt AC 43 ms 13860 KiB
random_19.txt AC 51 ms 14108 KiB
random_20.txt AC 46 ms 14080 KiB
random_21.txt AC 34 ms 13452 KiB
random_22.txt AC 58 ms 13252 KiB
random_23.txt AC 35 ms 13272 KiB
random_24.txt AC 46 ms 13240 KiB
sample_01.txt AC 2 ms 3552 KiB
sample_02.txt AC 2 ms 3428 KiB
sample_03.txt AC 2 ms 3480 KiB