Language: C++




 #include <stdio.h>
 #include <bits/stdc++.h>






using namespace std;




template<class T, size_t... I>
void print_tuple(ostream& s, T const& a, index_sequence<I...>){
  using swallow = int[];
  (void)swallow{0, (void(s << (I == 0? "" : ", ") << get<I>(a)), 0)...};
}

template<class T>
ostream& print_collection(ostream& s, T const& a);
template<class... A>
ostream& operator<<(ostream& s, tuple<A...> const& a);
template<class A, class B>
ostream& operator<<(ostream& s, pair<A, B> const& a);

template<class T, size_t I>
ostream& operator<<(ostream& s, array<T, I> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, vector<T> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, deque<T> const& a) { return print_collection(s, a); }
template<class T, class U>
ostream& operator<<(ostream& s, multimap<T, U> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, multiset<T> const& a) { return print_collection(s, a); }
template<class T, class U>
ostream& operator<<(ostream& s, map<T, U> const& a) { return print_collection(s, a); }
template<class T>
ostream& operator<<(ostream& s, set<T> const& a) { return print_collection(s, a); }

template<class T>
ostream& print_collection(ostream& s, T const& a){
  s << '[';
  for(auto it = begin(a); it != end(a); ++it){
    s << *it;
    if(it != prev(end(a))) s << " ";
  }
  return s << ']';
}

template<class... A>
ostream& operator<<(ostream& s, tuple<A...> const& a){
  s << '(';
  print_tuple(s, a, index_sequence_for<A...>{});
  return s << ')';
}

template<class A, class B>
ostream& operator<<(ostream& s, pair<A, B> const& a){
  return s << "(" << get<0>(a) << ", " << get<1>(a) << ")";
}







using li = long long int;
using lu = long long unsigned;
using ld = long double;


using pii = tuple<li, li>;
using piii = tuple<li, li, li>;
using piiii = tuple<li, li, li, li>;
using vi = vector<li>;
using vii = vector<pii>;
using viii = vector<piii>;
using vvi = vector<vi>;
using vvii = vector<vii>;
using vviii = vector<viii>;

template<class T>
using min_queue = priority_queue<T, vector<T>, greater<T> >;
template<class T>
using max_queue = priority_queue<T>;

struct empty_t {};



namespace tuple_access {

  struct xx_t {} xx;
  struct yy_t {} yy;
  struct zz_t {} zz;
  struct ww_t {} ww;

  template<class T>
  typename tuple_element<0,T>::type& operator^(T& v, xx_t) { return get<0>(v); }
  template<class T>
  typename tuple_element<0,T>::type const& operator^(T const& v, xx_t) { return get<0>(v); }
  template<class T>
  typename tuple_element<0,T>::type&& operator^(T&& v, xx_t) { return get<0>(v); }

  template<class T>
  typename tuple_element<1,T>::type& operator^(T& v, yy_t) { return get<1>(v); }
  template<class T>
  typename tuple_element<1,T>::type const& operator^(T const& v, yy_t) { return get<1>(v); }
  template<class T>
  typename tuple_element<1,T>::type&& operator^(T&& v, yy_t) { return get<1>(v); }

  template<class T>
  typename tuple_element<2,T>::type& operator^(T& v, zz_t) { return get<2>(v); }
  template<class T>
  typename tuple_element<2,T>::type const& operator^(T const& v, zz_t) { return get<2>(v); }
  template<class T>
  typename tuple_element<2,T>::type&& operator^(T&& v, zz_t) { return get<2>(v); }

  template<class T>
  typename tuple_element<3,T>::type& operator^(T& v, ww_t) { return get<3>(v); }
  template<class T>
  typename tuple_element<3,T>::type const& operator^(T const& v, ww_t) { return get<3>(v); }
  template<class T>
  typename tuple_element<3,T>::type&& operator^(T&& v, ww_t) { return get<3>(v); }
}
using namespace tuple_access;


int ilog2(int x){ return 31 - __builtin_clz(x); }

template <class T>
struct identity : std::unary_function <T, T> {
  T operator() (const T& x) const {return x;}
};

template<class T>
T& smin(T& x, T const& y) { x = min(x,y); return x; }

template <class T>
T& smax(T& x, T const& y) { x = max(x, y); return x; }


template<typename T>
T isqrt(T const&x){
  static_assert(is_integral<T>::value, "is_integral<T>::value");
  assert(x>=T(0));
  T ret = static_cast<T>(sqrtl(x));
  while(ret>0 && ret*ret>x) --ret;
  while(x-ret*ret>2*ret) ++ret;
  return ret;
}

template<typename T>
T icbrt(T const&x) {
  static_assert(is_integral<T>::value, "is_integral<T>::value");
  assert(x>=T(0));
  T ret = static_cast<T>(cbrt(x));
  while(ret>0 && ret*ret*ret>x) --ret;
  while(x-ret*ret*ret>3*ret*(ret+1)) ++ret;
  return ret;
}

int main(){
  ios::sync_with_stdio(0); cin.tie(0);
  li n; cin>>n;
  vi A(n); for(li i = 0; i < (li)(n); ++i) cin>>A[i];

  vi I(n); iota(begin(I), end(I), 0);
  sort(begin(I), end(I), [&](int i, int j) { return A[i] < A[j]; });

  if(A[I[0]] > 2*A[I[n-1]]) {
    cout << -1 << endl;
    return 0;
  }
  int m = A[I[0]];
  vector<vi> V(2*m);
  for(li i = 0; i < (li)(n); ++i) {
    if(i < I[0]) {
      for(li j = 0; j < (li)(m); ++j) V[2*j].push_back(i);
      for(li j = 0; j < (li)(A[i]-m); ++j) V[2*j+1].push_back(i);
    } else if(i > I[0]) {
      for(li j = 0; j < (li)(m); ++j) V[2*j+1].push_back(i);
      for(li j = 0; j < (li)(A[i]-m); ++j) V[2*m - 2*j - 2].push_back(i);
    }
  }

  for(li i = 0; i < (li)(m); ++i) {
    vector<int> seen1(n);
    for(int a : V[2*i+1]) seen1[a] = 1;
    vector<int> seen2(n);
    for(int a : V[2*i]) seen2[a] = 1;
    vi v2, w2, v1, w1;
    for(int a : V[2*i]) if(seen[a]) v1.push_back(a); else w1.push_back(a);
    for(int a : V[2*i+1]) if(seen[a]) v2.push_back(a); else w2.push_back(a);

    sort(begin(v1), end(v1));
    sort(begin(w1), end(w1));
    sort(begin(v2), end(v2));
    sort(begin(w2), end(w2));

    for(int x : v1) cout << x+1 << ' ';
    for(int x : w1) cout << x+1 << ' ';
    cout << I[0]+1 << ' ';
    for(int x : w2) cout << x+1 << ' ';
    for(int x : v2) cout << x+1 << ' ';
  }
  cout << endl;

  return 0;
}


Compiler output:
a.cc:138:24: warning: 'template<class _Arg, class _Result> struct std::unary_function' is deprecated [-Wdeprecated-declarations]
  138 | struct identity : std::unary_function <T, T> {
      |                        ^~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_function.h:117:12: note: declared here
  117 |     struct unary_function
      |            ^~~~~~~~~~~~~~
a.cc: In function 'int main()':
a.cc:199:28: error: 'seen' was not declared in this scope; did you mean 'seen2'?
  199 |     for(int a : V[2*i]) if(seen[a]) v1.push_back(a); else w1.push_back(a);
      |                            ^~~~
      |                            seen2
a.cc:200:30: error: 'seen' was not declared in this scope; did you mean 'seen2'?
  200 |     for(int a : V[2*i+1]) if(seen[a]) v2.push_back(a); else w2.push_back(a);
      |                              ^~~~
      |                              seen2