//              +-- -- --++-- +-In the name of ALLAH-+ --++-- -- --+              \\
/* Some Makoto Shinkai's : 
  “Who cares if we can't see any sunshine? I want you more than any blue sky!!!”
    - Tenki no Ko
    
  "By the time the date is over, the comet will be visible in the sky."
    - Kimi no Nawa
    
  “No matter what happens, even if the stars fall, I will live.”
    - Byōsoku 5 Centimeter
  
*/
#include <bits/stdc++.h>
#define Raveluk ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define tii tuple<int,int,int>
#define g1 get<0>
#define g2 get<1>
#define g3 get<2>
#define qf q.front()
#define all(x) (x).begin(), (x).end()
using namespace std;
int main()
{
  Raveluk
  int n,m,i,j,idx=0,simpan,id;
  cin>>n>>m;
  int anu[n+1];
  for(i=1;i<=n;i++) cin>>anu[i];
  sort(anu+1,anu+n+1);
  // true terbanyak yang bisa didapat
  for(i=n;i>=1;i--){
    if(anu[i] <= n-i){
      idx = i;
      simpan = idx+1;
      break;
    }
  }
  // true paling kanan berada di idx
  // kita gabisa banyakin true 
  if(n-idx > m){
    cout<<-1;
    return 0;
  }
  //cout<<idx<<" "<<simpan<<endl;
  while(n-idx < m){
    // banyakin false 
    id = max(simpan,n-anu[idx]+1);
    bool aseli = false;
    while(true){
      if(id > n) break;
      if(n-idx < anu[id]){
        aseli = true;
        swap(anu[idx],anu[id]);
        break;
      }
      id++;
    }
    simpan = id+1;
    if(aseli) idx--;
    if(simpan > n) break;
  }
  if(n-idx == m){
    for(i=1;i<=n;i++) cout<<anu[i]<<" ";
  }
  else cout<<-1;
  
  return 0;
}
| # | 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... | 
| # | 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... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |