結果

問題 No.3050 Prefix Removal
ユーザー emthrm
提出日時 2025-03-07 21:28:13
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 3,052 bytes
コンパイル時間 4,045 ms
コンパイル使用メモリ 287,104 KB
実行使用メモリ 23,764 KB
最終ジャッジ日時 2025-03-07 21:28:27
合計ジャッジ時間 12,344 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 998244353;
// constexpr int MOD = 1000000007;
constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1};
constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U>
inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U>
inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

// https://ei1333.github.io/library/structure/others/priority-sum-structure.hpp
template <typename T, typename Compare = less<T>,
          typename RCompare = greater<T> >
struct PrioritySumStructure {
  size_t k;
  T sum;

  priority_queue<T, vector<T>, Compare> in, d_in;
  priority_queue<T, vector<T>, RCompare> out, d_out;

  PrioritySumStructure(int k) : k(k), sum(0) {}

  void modify() {
    while (in.size() - d_in.size() < k && !out.empty()) {
      auto p = out.top();
      out.pop();
      if (!d_out.empty() && p == d_out.top()) {
        d_out.pop();
      } else {
        sum += p;
        in.emplace(p);
      }
    }
    while (in.size() - d_in.size() > k) {
      auto p = in.top();
      in.pop();
      if (!d_in.empty() && p == d_in.top()) {
        d_in.pop();
      } else {
        sum -= p;
        out.emplace(p);
      }
    }
    while (!d_in.empty() && in.top() == d_in.top()) {
      in.pop();
      d_in.pop();
    }
  }

  T query() const { return sum; }

  void insert(T x) {
    in.emplace(x);
    sum += x;
    modify();
  }

  void erase(T x) {
    assert(size());
    if (!in.empty() && in.top() == x) {
      sum -= x;
      in.pop();
    } else if (!in.empty() && RCompare()(in.top(), x)) {
      sum -= x;
      d_in.emplace(x);
    } else {
      d_out.emplace(x);
    }
    modify();
  }

  void set_k(size_t kk) {
    k = kk;
    modify();
  }

  size_t get_k() const { return k; }

  size_t size() const {
    return in.size() + out.size() - d_in.size() - d_out.size();
  }
};

template <typename T>
using MaximumSum = PrioritySumStructure<T, greater<T>, less<T> >;

template <typename T>
using MinimumSum = PrioritySumStructure<T, less<T>, greater<T> >;

int main() {
  int n, k; cin >> n >> k;
  vector<int> a(n);
  for (int& a_i : a) cin >> a_i;
  vector<ll> b(n);
  ranges::copy(a, b.begin());
  for (int i = n - 2; i >= 0; --i) {
    b[i] += b[i + 1];
  }
  MaximumSum<ll> topk(k - 1);
  FOR(i, 1, n) topk.insert(b[i]);
  ll ans = topk.query() + b.front(), minus = 0;
  for (int i = n - 1; i >= 1 && topk.size() > k - 1; --i) {
    topk.erase(b[i]);
    minus += a[i];
    chmax(ans, topk.query() + b.front() - minus * k);
  }
  cout << ans << '\n';
  return 0;
}
0