 My Approach :-

1. If it is possible to make anyone equal to 0 than do it .
else
2. First try to decrease the smaller value.
3. If two values are same than decrease that value which can be reduced to lowest value first.

code:-

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll ;
int main()
{
int t ; cin>>t;
for(int i=0;i<t;i++)
{
ll a,b,x,y,n; cin>>a>>b>>x>>y>>n;
ll val1,val2,val1by,val2by ;

if(((x==0)&&(a<=n))||((y==0)&&(b<=n)))
cout<<0<<endl;

else
{
if(a<b)
{ val1=a; val2=b; val1by=x; val2by=y; }
else if(a>b)
{ val1=b; val2=a; val1by=y; val2by=x; }
else
{
if((a-x)>=(b-y))
{
val1=a; val2=b; val1by=x; val2by=y;
}
else
{
val1=b; val2=a; val1by=y; val2by=x;
}
}

ll curr1,curr2;

if((val1-val1by)>=n)
{
curr1=n;
n=0;
curr2=0;
}
else
{
curr1=(val1-val1by);
n-=curr1;
if((val2-val2by)>=n)
curr2=n;
else
curr2=(val2-val2by);
}

val1-=curr1;
val2-=curr2;
//cout<<"val1 and val2 is "<<val1<<" "<<val2<<endl;
cout<<val1*val2<<endl;

}

}

return 0;
}

The problem is identical to 1409B.

You can read the editorial here.

1 Like

Help needed 