Why this need to be sorting? (TOWIN)

problem code : TOWIN Problem - CodeChef
#include
#include<bits/stdc++.h>
using namespace std;

int main() {
int t;
cin>>t;
while(t–)
{
long long n,a[10010],c=0;
long long s1=0,s2=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n,greater());
for(int i=0;i<n;i++)
{

        if(i==2)
        c=1;
        
        if(c==0)
        {
            s1+=a[i];
            c=1;
        }
        else
        {
            s2+=a[i];
            c=0;
        }
            
        }
        if(s1>s2)
        cout<<"first";
        else if(s2>s1)
        cout<<"second";
        else
        cout<<"draw";
        cout<<endl;
}
return 0;

}

because it was said in the question that they will play optimally

2 Likes

you have to pick the highest value each time(since they told both the player chooses optimally)