結果

問題 No.2686 商品券の使い道
ユーザー otoshigo
提出日時 2024-03-20 23:25:55
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 187 ms / 3,000 ms
コード長 1,131 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 75,968 KB
最終ジャッジ日時 2025-02-20 10:08:30
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N;
    ll M,Q;
    cin>>N>>M>>Q;
    vector<ll>A(N),B(N);
    for(int i=0;i<N;i++)cin>>A[i]>>B[i];
    vector<ll>V(1<<N),W(1<<N);
    for(int bit=0;bit<1<<N;bit++){
        ll C=0,D=0;
        for(int i=0;i<N;i++){
            if(bit>>i&1){
                C+=A[i];
                D+=B[i];
            }
        }
        if(C<=M)V[bit]=D;
        if(C<=Q)W[bit]=D;
    }
    for(int bit=0;bit<1<<N;bit++){
        for(int i=0;i<N;i++){
            if(bit>>i&1){
                chmax(V[bit],V[bit^(1<<i)]);
                chmax(W[bit],W[bit^(1<<i)]);
            }
        }
    }
    ll ans=0;
    for(int bit=0;bit<1<<N;bit++){
        chmax(ans,V[bit]+W[((1<<N)-1)^bit]);
    }
    cout<<ans<<"\n";
}
0