結果

問題 No.914 Omiyage
ユーザー erbowlerbowl
提出日時 2020-05-06 14:47:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 2,518 ms
コンパイル使用メモリ 209,284 KB
実行使用メモリ 5,416 KB
最終ジャッジ日時 2023-09-11 01:50:37
合計ジャッジ時間 3,632 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
5,360 KB
testcase_01 AC 15 ms
5,416 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
5,360 KB
testcase_04 AC 13 ms
5,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 14 ms
5,344 KB
testcase_14 AC 15 ms
5,360 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

typedef long long ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;


int main() {
    ll n,m,k;
    std::cin >> n>>m>>k;
    
    vector<vector<ll>> a(n,vector<ll>(m));
    
    ll minv = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            std::cin >> a[i][j];
        }
        minv += a[i][0];
    }

    if(minv>k){
        std::cout << -1 << std::endl;
        return 0;
    }
    
        if(n==1){
        ll now = 0;
        for (int i = 0; i < m; i++) {
            if(a[0][i]<=k)now = a[0][i];
        }
        std::cout << k-now << std::endl;
        return 0;
    }
    vector<ll> v1,v2;

    function<void(int, int)> rec = [&] (int now, int val) {
        if(now==n/2){
            v1.push_back(val);
            return;
        }
        for (int i = 0; i < m; i++) {
            rec(now+1, val + a[now][i]);
        }
    };
    function<void(int, int)> rec2 = [&] (int now, int val) {
        if(now==n){
            v2.push_back(val);
            return;
        }

        for (int i = 0; i < m; i++) {
            rec2(now+1, val + a[now][i]);
        }
    };
    
    rec(0,0);
    rec2(n/2,0);
    sort(v1.begin(),v1.end());
    sort(v2.begin(),v2.end());

    ll ans = k;
    for (auto e : v1) {
        if(e>=k)continue;
        auto itr = upper_bound(v2.begin(),v2.end(),k-e);
        if(itr == v2.begin()){
            continue;
        }else{
            --itr;
            ans = min(ans,k-e-*itr);
        }
    }
    std::cout << ans << std::endl;
}
0