結果

問題 No.3683 サーバー代がもったいない!
コンテスト
ユーザー
提出日時 2026-09-05 14:18:33
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,565 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,596 ms
コンパイル使用メモリ 358,752 KB
実行使用メモリ 9,984 KB
最終ジャッジ日時 2026-09-05 14:18:57
合計ジャッジ時間 5,065 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge6_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 WA * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/segtree>
using namespace std;
using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
template <typename K, typename V>
using umap = unordered_map<K, V>;

#define rep(i, n) for (int i = 0; i < n;i++)
#define rep1(i, n) for (int i = 1; i <= n;i++)
#define rrep(i, n) for (int i = n - 1; i >= 0;i--)
#define rrep1(i, n) for (int i = n; i >= 1;i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define INF 1LL << 60
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)

struct S {
    ll mx;
    int idx;
};


S op(S a, S b) {
    S res;
    if (a.mx >= b.mx) {
        res.mx = a.mx;
        res.idx = a.idx;
    } else {
        res.mx = b.mx;
        res.idx = b.idx;
    }
    return res;
}

S e() {
    return {-INF, 0};
}

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

    int n, k;
    cin >> n >> k;

    if (k > n / 2) {
        cout << "Impossible" << endl;
        return 0;
    }
    vector<ll> a(n);

    ll m = -1;
    int idx = 0;

    vector<S> comp(n);
    rep(i, n) {
        cin >> a[i];
        if (m < a[i]) {
            m = a[i];
            idx = i;
        }
        comp[i] = {a[i], i};
    }

    segtree<S, op, e> seg(comp);

    ll ans = m;
    int cur = idx;
    seg.set(cur, {-INF, cur});
    rep(i, k - 1) {
        S res = op(seg.prod(0, max(0, cur- 1)), seg.prod(min(n, cur + 2), n));
        ans += res.mx;
        cur = res.idx;
        seg.set(res.idx, {-1, res.idx});
    }

    cout << ans << endl;

}
0