Codeforces Round #835 (Div. 4) C. Advantage
Make a copy of the array s s s: call it t t t. Sort t t t in non-decreasing order, so that t 1 t_1 t1 is the maximum strength and t 2 t_2 t2 — the second maximum strength.
Then for everyone but the best person, they should compare with the best person who has strength t 1 t_1 t1. So for all i i i such that s i si si ≠ ≠ = t 1 t_1 t1, we should output s i s_i si − t 1 . t_1. t1. Otherwise, output s i s_i si − t 2 t_2 t2 — the second highest strength, which is the next best person.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T=1;cin>>T;
while(T--)
{
int n;cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
auto b=a;
sort(b.begin(),b.end());
for(int i=0;i<n;i++)
{
if(a[i]==b[n-1]) cout<<a[i]-b[n-2]<<" ";
else cout<<a[i]-b[n-1]<<" ";
}
cout<<endl;
}
return 0;
}