#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define all(x) x.begin() , x.end()
int k,cnt[9];
pair<string,string> pai;
string str;
void dfs(int x){
if(x == 0){
string maxi;
for(int i = 0;i+k-1<sz(str);i++){
string cand = str.substr(i , i + k);
if(maxi.empty() or maxi < cand)maxi = cand;
}
if(pai.first.empty() or pai.first > maxi){
pai = {maxi , str};
}
return;
}
for(int i = 0;i<9;i++){
if(cnt[i]){
cnt[i]--;
str += to_string(i+1);
dfs(x-1);
str.pop_back();
cnt[i]++;
}
}
}
void solve(){
pai = {"",""};
str = "";
cin >> k;
int sum = 0;
for(int i = 0;i<9;i++){
cin >> cnt[i];
sum += cnt[i];
}
if(k == 2){
vector<int>arr(3*sum , -1);
int sayac = 1 , maxi = -1;
for(int i = 8;i>=0;i--){
if(cnt[i]){
maxi = i;
for(int j = 0;j<i;j++){
while(cnt[j]--){
arr[sayac] = i;
sayac += 2;
}
}
break;
}
}
arr[sayac-1] = maxi;
cnt[maxi]--;
sayac = 0;
while(cnt[maxi]--){
arr[sayac] = maxi;
sayac += 2;
}
for(int i = 0;i<sz(arr);i++){
if(arr[i] != -1){
cout << arr[i];
}
}
cout << endl;
}
else {
dfs(sum);
assert(sz(pai.second) == sum);
cout << pai.second << endl;
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase=1;cin >> testcase;
while(testcase--)solve();
cerr << 1000.0 * clock() / CLOCKS_PER_SEC << " ms" << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |