結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー srjywrdnprkt
提出日時 2023-06-21 22:29:38
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 43 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 1,862 ms
コンパイル使用メモリ 197,684 KB
最終ジャッジ日時 2025-02-14 23:55:01
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main(){

    string K, S="";
    ll N, M, ok=-1, s;
    cin >> N >> K;
    vector<ll> c(10), d;
    for (int i=1; i<=9; i++) cin >> c[i];
    d = c;
    M = K.size();

    if (M < N){
        for (int i=1; i<=9; i++) S += string(c[i], '0'+i);
        cout << S << endl;
        return 0;
    }
    else if (M > N){
        cout << -1 << endl;
        return 0;
    }

    for (int i=0; i<N; i++){
        s = K[i]-'0';
        for (int j=s+1; j<=9; j++) if (c[j]) ok=i;
        if (c[s]) c[s]--;
        else break;
    }

    if (ok == -1){
        cout << -1 << endl;
        return 0;
    }

    for (int i=0; i<ok; i++){
        S += K[i];
        d[K[i]-'0']--;
    }
    for (int i=K[ok]-'0'+1; i<=9; i++){
        if (d[i]){
            S += i+'0';
            d[i]--;
            break;
        }
    }

    for (int i=1; i<=9; i++) S += string(d[i], i+'0');

    cout << S << endl;

    return 0;
}
0