結果

問題 No.3050 Prefix Removal
ユーザー suisen
提出日時 2023-01-05 22:13:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 900 bytes
コンパイル時間 1,017 ms
コンパイル使用メモリ 78,824 KB
実行使用メモリ 16,756 KB
最終ジャッジ日時 2025-03-07 20:30:23
合計ジャッジ時間 7,003 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <limits>
#include <queue>
#include <vector>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::size_t n, k;
    std::cin >> n >> k;

    std::vector<long long> a(n), b(n + 1);
    for (auto &e : a) std::cin >> e;

    for (std::size_t i = 0; i < n; ++i) {
        b[i + 1] = b[i] + a[i];
    }

    long long ans = std::numeric_limits<long long>::min();

    std::priority_queue<long long> pq;
    long long pq_sum = 0; // sum of top (k - 1)

    for (std::size_t x = 1; x <= n; ++x) {
        if (x >= k) {
            ans = std::max(ans, static_cast<long long>(k) * b[x] - pq_sum);
        }
        pq.push(b[x]);
        pq_sum += b[x];
        if (pq.size() >= k) {
            long long rem_val = pq.top();
            pq.pop();
            pq_sum -= rem_val;
        }
    }
    std::cout << ans << std::endl;

    return 0;
}
0