結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー momoyuu
提出日時 2024-10-06 19:48:16
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,003 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 96,620 KB
実行使用メモリ 43,440 KB
最終ジャッジ日時 2024-10-06 19:48:19
合計ジャッジ時間 2,879 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 20 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
using ll = long long;

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


    ll n;
    cin>>n;
    string k;
    cin>>k;
    vector<int> c(10,0);
    for(int i = 1;i<=9;i++) cin>>c[i];

    string ans = "";
    auto dfs = [&](auto dfs,int ni) -> bool {
        if(ni==k.size()) return true;
        int p = k[ni] - '0';
        if(c[p]!=0&&ni+1!=k.size()){
            c[p]--;
            ans += k[ni];
            bool ok = dfs(dfs,ni+1);
            if(ok) return true;
            c[p]++;
            ans.pop_back();
        }
        for(int i = p+1;i<=9;i++){
            if(c[i]==0) continue;
            ans += '0' + i;
            c[i]--;
            for(int j = 1;j<=9;j++){
                for(int l = 0;l<c[j];l++) ans += '0' + j;
            }
            return true;
        }
        return false;
    };

    dfs(dfs,0);

    if(ans.empty()) cout<<-1<<endl;
    else cout<<ans<<endl;
}   

0