fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, a, ret[1000004], mx, mx2;
  4. vector<int> v;
  5. int main(){
  6. ios_base::sync_with_stdio(false);
  7. cin.tie(NULL); cout.tie(NULL);
  8. cin >> n;
  9. for(int i = 0; i < n; i++){
  10. cin >> a;
  11. v.push_back(a);
  12. mx = max(mx, a);
  13. }
  14. for(int i = v.size() - 1; i >= 0; i--){
  15. if(v[i] == mx) ret[i] = -1;
  16. else {
  17. mx2 = max(mx2, v[i]);
  18. ret[i] = mx2;
  19. }
  20. }
  21.  
  22. for(int i = 0; i < n; i++){
  23. cout << ret[i] << ' ';
  24. }
  25. }
Success #stdin #stdout 0s 5312KB
stdin
4
3 5 2 7
stdout
5 5 2 -1