Problem: 2052A - Adrenaline Rush
Language: C++17 (GCC 7-32)

Previous submission (verdict: Wrong answer on test 5):
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define Yes cout<<"YES\n"
#define No cout<<"NO\n"
#define no cout<<"No\n"
#define yes cout<<"Yes\n"
#define all(v) v.begin(),v.end()
#define inp(arr) for(auto &it:arr) cin>>it;
#define outp(arr) for(auto &it:arr) cout<<it<<" ";cout<<endl;
#define sum(arr) accumulate(all(arr),0);
#define what_is(val1) cout<<#val1<<": "<<val1<<endl;
#define unique_sort(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define mach_speed  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
const int mod=1000000007;
const int INF=1000000000;

void solve() {
  int n;cin>>n;
  int arr[n];
  inp(arr);
  vector<pair<int,int>> ans;
  int val=1;
  map<int,int> mp;
  for (int i = 0; i < n; i++)
  {
    if(arr[i]>val){
      for (int j = val; j < arr[i]; j++)
      {
        if(mp[j]==0)  ans.push_back({arr[i],j});
      }
    }
    else{
      mp[val]++;
      while (mp[val]>0)
      {
        val++;
      }
    }
    mp[arr[i]]++;
  }
  int start=0;
  for (int i = 1; i < n; i++)
  {
    if(arr[i]<arr[i-1]){
      for (int j = i-1; j > start; j--)
      {
        for (int k = j-1; k >=start ; k--)
        {
          ans.push_back({arr[j],arr[k]});
        }        
      }
      for (int j = start; j < i-1; j++)
      {
        for (int k = j+1; k<i; k++)
        {
          ans.push_back({arr[j],arr[k]});
        }        
      }
      start=i;
    }
  }
  if(start<n-1){
    for (int j = n-1; j > start; j--)
    {
      for (int k = j-1; k >=start ; k--)
      {
        ans.push_back({arr[j],arr[k]});
      }        
    }
    for (int j = start; j < n-1; j++)
    {
      for (int k = j+1; k<n; k++)
      {
        ans.push_back({arr[j],arr[k]});
      }        
    }
  }
  cout<<ans.size()<<endl;
  for(auto it:ans) cout<<it.first<<" "<<it.second<<endl;
}

int32_t main()
{
  mach_speed    
  // #ifdef LOCAL
  // #endif
  int t=1;
  // cin>>t;
  while (t--)
  {
    // cout<<t;
    solve();
  }
}

Submission (verdict: Wrong answer on test 7):
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
#define Yes cout<<"YES\n"
#define No cout<<"NO\n"
#define no cout<<"No\n"
#define yes cout<<"Yes\n"
#define all(v) v.begin(),v.end()
#define inp(arr) for(auto &it:arr) cin>>it;
#define outp(arr) for(auto &it:arr) cout<<it<<" ";cout<<endl;
#define sum(arr) accumulate(all(arr),0);
#define what_is(val1) cout<<#val1<<": "<<val1<<endl;
#define unique_sort(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define mach_speed  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
const int mod=1000000007;
const int INF=1000000000;

void solve() {
  int n;cin>>n;
  int arr[n];
  inp(arr);
  vector<pair<int,int>> ans;
  int val=1;
  map<int,int> mp;
  for (int i = 0; i < n; i++)
  {
    if(arr[i]>val){
      for (int j = arr[i]-1; j >= val; j--)
      {
        if(mp[j]==0)  ans.push_back({arr[i],j});
      }
    }
    else{
      mp[val]++;
      while (mp[val]>0)
      {
        val++;
      }
    }
    mp[arr[i]]++;
  }
  int start=0;
  for (int i = 1; i < n; i++)
  {
    if(arr[i]<arr[i-1]){
      for (int j = i-1; j > start; j--)
      {
        for (int k = j-1; k >=start ; k--)
        {
          ans.push_back({arr[j],arr[k]});
        }        
      }
      for (int j = start; j < i-1; j++)
      {
        for (int k = j+1; k<i; k++)
        {
          ans.push_back({arr[j],arr[k]});
        }        
      }
      start=i;
    }
  }
  if(start<n-1){
    for (int j = n-1; j > start; j--)
    {
      for (int k = j-1; k >=start ; k--)
      {
        ans.push_back({arr[j],arr[k]});
      }        
    }
    for (int j = start; j < n-1; j++)
    {
      for (int k = j+1; k<n; k++)
      {
        ans.push_back({arr[j],arr[k]});
      }        
    }
  }
  cout<<ans.size()<<endl;
  for(auto it:ans) cout<<it.first<<" "<<it.second<<endl;
}

int32_t main()
{
  mach_speed    
  // #ifdef LOCAL
  // #endif
  int t=1;
  // cin>>t;
  while (t--)
  {
    // cout<<t;
    solve();
  }
}