Language: C++

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Elem {
    int val;
    int index;
};

int main() {
    int n;
    cin >> n;
    auto a = vector<int>(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i].val;
        a[i].index = i;
    }
    sort(a.begin(), a.end(), [](auto x, auto y) { return x.val < y.val; });

    auto ans = vector<int>(n);
    for (int i = 0; i < n - 1; i++) {
        cout << *max_element(a.begin() + (i + 1), a.begin() + (i + 1) + (n - 1)) << "\n";
        ans[a[i].index] = a.back().val;
    }

    ans[a.back().index] = (a.rbegin() + 1)->val;
    for (auto a : ans) {
        cout << a << "\n";
    }

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:16:21: error: request for member 'val' in 'a.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   16 |         cin >> a[i].val;
      |                     ^~~
a.cc:17:14: error: request for member 'index' in 'a.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   17 |         a[i].index = i;
      |              ^~~~~
a.cc:24:18: error: request for member 'index' in 'a.std::vector<int>::operator[](((std::vector<int>::size_type)i))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   24 |         ans[a[i].index] = a.back().val;
      |                  ^~~~~
a.cc:24:36: error: request for member 'val' in 'a.std::vector<int>::back()', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   24 |         ans[a[i].index] = a.back().val;
      |                                    ^~~
a.cc:27:18: error: request for member 'index' in 'a.std::vector<int>::back()', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'}
   27 |     ans[a.back().index] = (a.rbegin() + 1)->val;
      |                  ^~~~~
a.cc:27:45: error: request for member 'val' in '* std::reverse_iterator<_Iterator>::operator+(difference_type) const [with _Iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; difference_type = long int](1).std::reverse_iterator<__gnu_cxx::__normal_iterator<int*, std::vector<int> > >::operator->()', which is of non-class type 'int'
   27 |     ans[a.back().index] = (a.rbegin() + 1)->val;
      |                                             ^~~
a.cc: In instantiation of 'main()::<lambda(auto:1, auto:2)> [with auto:1 = int; auto:2 = int]':
/usr/include/c++/14/bits/predefined_ops.h:158:30:   required from 'constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Iterator2 = __gnu_cxx::__normal_iterator<int*, std::vector<int> >; _Compare = main()::<lambda(auto:1, auto:2)>]'
  158 |         { return bool(_M_comp(*__it1, *__it2)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1777:14:   required from 'void std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1, auto:2)> >]'
 1777 |           if (__comp(__i, __first))
      |               ~~~~~~^~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1817:25:   required from 'void std::__final_insertion_sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1, auto:2)> >]'
 1817 |           std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1908:31:   required from 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1, auto:2)> >]'
 1908 |           std::__final_insertion_sort(__first, __last, __comp);
      |           ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:4805:18:   required from 'void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter = __gnu_cxx::__normal_iterator<int*, vector<int> >; _Compare = main()::<lambda(auto:1, auto:2)>]'
 4805 |       std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp));
      |       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:19:9:   required from here
   19 |     sort(a.begin(), a.end(), [](auto x, auto y) { return x.val < y.val; });
      |     ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:19:60: error: request for member 'val' in 'x', which is of non-class type 'int'
   19 |     sort(a.begin(), a.end(), [](auto x, auto y) { return x.val < y.val; });
      |                                                          ~~^~~
a.cc:19:68: error: request for member 'val' in 'y', which is of non-class type 'int'
   19 |     sort(a.begin(), a.end(), [](auto x, auto y) { return x.val < y.val; });
      |                                                                  ~~^~~