Dementia rated contest - Problem Code: TOWIN

Can any one please tell me what’s going wrong??? :confounded: :confounded: :confounded:

#include <bits/stdc++.h>
using namespace std;

#define endl ("\n")
#define pi (3.141592653589)
#define mod 1e9+7
#define ll long long
#define rfo(i, n) for(int i = n-1; i >=0; i--)
#define fo(i, n) for(int i = 0; i < n; i++)
#define ifo(i, k, n) for(int i = k; i < n; i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

void solve();

int main() {
	fast

	// Forget ratings, enjoy the experience
	
	int t = 1;
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}
// write code here
void solve() {
	int n;
    cin >> n;
    vector<ll> a;
    a.resize(n);
    fo(i, n) {
        cin >> a[i];
    }
    int p1 = 0, p2 = 0;
    sort(a.begin(), a.end(), greater<ll>());

    fo(i, n) {
        if (i == 0 || (i % 2 == 1 && i != 1))
        p1 += a[i];
        else
        {
            p2 += a[i];
        }
    }
    if (p1 > p2)
        cout << "first\n";
    else if (p1 < p2)
        cout << "second\n";
    else
    {
        cout << "draw\n";
    }
}[quote="its_naveen, post:1, topic:74594, full:true"]
1 Like

i pasted #pragma GCC optimize “trapv” on top of my program… still shows wrong answer.
also pls explain me whats wrong in my program, i didnt get u actually :sweat_smile: :sweat_smile:

i just changed

int p1 = 0, p2 = 0
to
long long p1 = 0, p2 = 0

and it run succesfully now. u r right that it is integer overflow but can u pls exlain what is

#pragma GCC optimize “trapv”
used for??

Gah - looks like this trick doesn’t work in some cases - presumably when the result of a computation does not cause overflow, but assigning the result to an int does.

Anyway, you’re getting overflow because there are testcases which would cause p1 and p2 to be assigned values that ints are two small to contain.

Declare p1 and p2 as ll instead of int and try again.

2 Likes

For detecting overflow XD Unfortunately, it seems that there are situations where it doesn’t work.

2 Likes

do you want to enlight this case too…
WA - solution1

Runtime Error - solution2

the only difference is #pragma GCC optimize “trapv”