結果

問題 No.951 【本日限定】1枚頼むともう1枚無料!
ユーザー WarToks
提出日時 2020-01-03 09:51:39
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 2,410 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 144,108 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 16:02:10
合計ジャッジ時間 4,374 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cstring>
#include <deque>
#include <functional>
#include <math.h>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <queue>
#include <vector>

#define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define rFOR(i, a, b) for(int (i) = (b); (i) >= (a); --(i))
#define REP(i, n) FOR(i, 0, n)
#define rREP(i, n) rFOR(i, 0, (n-1))
#define SORT(A) std::sort((A).begin(), (A).end())
#define ALL(A) (A).begin(), (A).end()
// 座標圧縮 (for vector) : ソートしてから使うのが一般的 ; SORT(A) => COORDINATE_COMPRESSION(A)
#define COORDINATE_COMPRESSION(A) (A).erase(unique((A).begin(),(A).end()),(A).end())

using lli = long long int;
using pii = std::pair<int, int>;

void VintOut(std::vector<int>& A){
    const int n = A.size();
    if(n == 0){putchar('\n'); return;}
    printf("%d", A[0]);
    for(int i = 1; i < n; ++i) printf(" %d", A[i]);
    putchar('\n');
}
void VintOut(std::vector<long long int>& A){
    const int n = A.size();
    if(n == 0){ putchar('\n'); return;}
    printf("%lld", A[0]);
    for(int i = 1; i < n; ++i) printf(" %lld", A[i]);
    putchar('\n');
}

template <typename T>
inline bool chmin(T& a, T b){
    if(b < a){ a = b; return true;}
    return false;
}

template <typename T>
inline bool chmax(T& a, T b){
    if(a < b){ a = b; return true;}
    return false;
}


// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

constexpr int inf = 1e9;
std::vector<std::pair<int, int>> A;

int main(void){
    int n, k; scanf("%d%d", &n, &k);
    A.resize(n); REP(i, n) scanf("%d%d", &A[i].first, &A[i].second);
    std::sort(A.begin(), A.end(), std::greater<std::pair<int, int>>());

    std::vector<int> P(k + 1, -inf), Q(k + 1, -inf), PP(k + 1, -inf), QQ(k + 1, -inf);
    P[0] = 0;
    for(auto& itr : A){
        int p = itr.first, c = itr.second;
        for(int e = 0; e <= k; ++e){
            chmax(PP[e], Q[e] + c); // 無料の交換
            chmax(PP[e], P[e]);
            chmax(QQ[e], Q[e]);
            if(e + p <= k) chmax(QQ[e + p], P[e] + c);
        }
        std::swap(P, PP);
        std::swap(Q, QQ);
    }
    int res = 0;
    for(int x = 0; x <= k; ++x){
        if(res < P[x]) res = P[x];
        if(res < Q[x]) res = Q[x];
    }
    printf("%d\n", res);

    

    
    
    return 0;
}
0