結果

問題 No.31 悪のミックスジュース
ユーザー mayoko_mayoko_
提出日時 2015-11-06 13:02:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 1,989 bytes
コンパイル時間 700 ms
コンパイル使用メモリ 83,128 KB
実行使用メモリ 13,348 KB
最終ジャッジ日時 2023-10-12 17:18:49
合計ジャッジ時間 1,637 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 6 ms
12,924 KB
testcase_02 AC 6 ms
13,028 KB
testcase_03 AC 8 ms
13,308 KB
testcase_04 AC 8 ms
13,324 KB
testcase_05 AC 8 ms
13,304 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 7 ms
13,304 KB
testcase_08 AC 7 ms
13,284 KB
testcase_09 AC 7 ms
13,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
5,612 KB
testcase_12 AC 5 ms
10,408 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 7 ms
13,268 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 2 ms
5,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>
#include<stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

const int MAXN = 111;
const ll INF = 1ll<<60;
ll C[MAXN], S[MAXN];
pair<ll, int> P[MAXN];

ll dp[MAXN][MAXN*MAXN];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    ll V;
    cin >> N >> V;
    for (int i = 0; i < N; i++) {
        cin >> C[i];
    }
    S[0] = C[0];
    P[0] = make_pair(S[0], 1);
    for (int i = 1; i < N; i++) {
        S[i] = S[i-1] + C[i];
        P[i] = make_pair(S[i], i+1);
    }
    ll ans = S[N-1];
    V -= N;
    if (V <= 0) {
        cout << ans << endl;
        return 0;
    }
    sort(P, P+N, [](const pair<ll, int>& lhs, const pair<ll, int>& rhs) -> bool {return lhs.first*rhs.second < rhs.first*lhs.second;});
    // V の残りが N^2 になるまでは引いていっても良い
    ll ok = V-N*N;
    if (ok > 0) {
        ll num = ok/P[0].second;
        if (ok%P[0].second) num++;
        ans += P[0].first*num;
        V -= P[0].second*num;
    }
    assert(V <= N*N);
    // 後は dp
    for (int i = 0; i < N; i++) for (int j = 0; j <= N*N; j++) dp[i][j] = INF;
    dp[0][0] = 0;
    for (int i = 0; i < N; i++) {
        if (i > 0) {
            for (int j = 0; j <= N*N; j++) {
                dp[i][j] = min(dp[i][j], dp[i-1][j]);
            }
        }
        for (int j = 0; j <= N*N; j++) {
            if (j-(i+1) >= 0) dp[i][j] = min(dp[i][j], dp[i][j-i-1]+S[i]);
        }
    }
    cout << ans + dp[N-1][V] << endl;
    return 0;
}
0