제출 #494360

#제출 시각아이디문제언어결과실행 시간메모리
494360cadmiumskyDevil's Share (RMI19_devil)C++14
100 / 100
1044 ms5444 KiB
#include <iostream>
#include <deque>
#include <algorithm>

using namespace std;

deque< pair<string,int> >deq;

int freq[10];

void testcase() {
  int n;
  cin >> n;
  --n;
  string s="",sus;
  for(int i=0; i<9; i++)
    cin >> freq[i];
  for(int i=8; i>=0; i--) {
    while(freq[i] && n) {
      n--;
      s+='1'+i;
      freq[i]--;
    }
  }
  reverse(s.begin(),s.end());
  for(int i=0; i<9; i++) {
    if(freq[i])
      sus='1'+i,deq.push_back(make_pair(sus,freq[i]));
  }
  while(deq.size()>1) {
    //cout << deq.size() <<'\n';
    auto x=deq.back();
    deq.pop_back();
    while(x.second>0) {
      auto temp=deq.front();
      deq.pop_front();
      auto nou=pair<string,int>{x.first+temp.first,min(temp.second,x.second)};
      x.second-=nou.second;
      temp.second-=nou.second;
      deq.push_back(nou);
      if(temp.second)
        deq.push_front(temp);
      //cout << "+ " << nou.first << ' '<< nou.second << '\n';
    }
  }
  auto x=deq.front();
  while(x.second--)
    cout << x.first;
  cout << s <<'\n';
  deq.pop_back();
}

int main() {
  int t;
  cin >> t;
  while(t--)
    testcase();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...