CHECHOC - Editorial

3 3 20 19

1 Like

if x>y we use x because we have no adjacent pairs to sum with and compare with y, so answer is always x

2 Likes

you commented above for a condition x > y, isnt the max value a cell can hold x ?

1 Like

Can anybody tell me where am I going wrong?

  LL n,m,x,y; cin>>n>>m>>x>>y;
  if(y>=2*x) {cout<<n*m*x<<"\n"; continue;}
  if(x>y) x=y;
  int a=0,b=0;
  FOR(i,0,n) 
      FOR(j,0,m) 
         if((i+j)&1) a++; else b++;

  cout<<max( a* x + b*(y-x), a*(y-x) + b*x )<<"\n";

same mistake i made @anon96650828
got accepted just changing that.
@tengiz05 thanks man

1 Like

two suggestions which worked for everyone, check for 1x1 matrix and also when y<x.

1 1 10 8
ans - 10
2 3 5 2
ans - 6

2 Likes

https://www.codechef.com/viewsolution/35990949

Can somebody tell whats wrong with my approach?

understood buddy… @tengiz05
u r genius…
just that cost me 100 and most of my time and confidence
thanks

man this was the only thing missing in my code. xD the void i feel rn is immense.

2 Likes

guys, can anyone tell mistake in my code.
i have also applied the same approach.

#include
#include<bits/stdc++.h>
using namespace std;
typedef long long int lt;
lt maxi(lt c, lt b)
{
if(c>b)return c;
else return b;
}
int main()
{
lt t,n,m,x,y;
cin>>t;
while(t–)
{
cin>>n>>m>>x>>y;
lt p;
lt a[n][m];
if(x<y)
{
if(y-x>x)
p=x;
else
p=y-x;
}
else {x=y;p=0;}
for(lt i=0;i<n;i++)
{
if(i%2==0)
{
for(lt j=0;j<m;j++)
{
if(j%2==0)
a[i][j]=p;
else a[i][j]=x;
}
}
else
{
for(lt j=0;j<m;j++)
{
if(j%2==0)
a[i][j]=x;
else a[i][j]=p;
}
}
}
lt sum=0;
for(lt i=0;i<n;i++)
{
for(lt j=0;j<m;j++)
sum+=a[i][j];
}
cout<<sum<<endl;
// if(n%2==0 && m%2==0)
// {
// lt k=n/2;
// lt sum=m*(kx+kp);
// cout<<sum<<endl;
// }
// else if(n%2!=0 && m%2!=0)
// {
// lt p1= (m/2+1)*((n/2+1)*x+(n/2)p);
// lt p2=(m/2)
((n/2)*x+(n/2+1)p);
// lt sum=p1+p2;
// cout<<sum<<endl;
// }
// else if(n%2==0 && m%2!=0)
// {
// lt p1= (m/2+1)
((n/2)*x+(n/2)p);
// lt p2=(m/2)
((n/2)*x+(n/2)p);
// lt sum=p1+p2;
// cout<<sum<<endl;
// }
// else if(n%2!=0 && m%2==0)
// {
// lt p1= (m/2)
((n/2)*x+(n/2+1)p);
// lt p2=(m/2)
((n/2+1)*x+(n/2)*p);
// lt sum=p1+p2;
// cout<<sum<<endl;
// }
}
// your code goes here
return 0;
}

https://www.codechef.com/viewsolution/35936885

Can some one help me where did I go wrong… Plz.

my bad…
got my answer buddy thats why deleted.
thanks @vinalltime

I used dp to solve the problem using simple top to down approach . But just to find the edge test-case of (n=1 and m=1) , I messed up the whole contest :sob::sob: .

My code : CodeChef: Practical coding for everyone

1 Like

Bhot accha hai Bro! Thanks!

3 3 20 19 answer is 95

import math

test=int(input())

for t in range(test):

a=input().split()

n=int(a[0])

m=int(a[1])

x=int(a[2])

y=int(a[3])

p=int(math.ceil((n*m)/2))

q=(n*m)-p

if n>=1 and n<=100 and m>=1 and m<=100 and x>=1 and x<=10000 and y>=1 and y<=10000:

    if y>=x and y<=(2*x):

        ans=(p*x)+(q*(y-x))

    elif y>x and y>2*x: 

        ans=(p*x)+(q*x)   

    elif y<x:

        ans=p*y

    print(ans)

else:

    print(-1)

what’s wrong with this?? giving correct ans in every edge case in dry run.

Yeah I was missing this too but finally after one hour I got it. I used so many approaches but nothing passed

1 Like

Failed the base case very early on.Never felt this stupid all my life and i have been pretty stupid all my life.

1 Like

in else if condition it is ans=xnum instead of ans=2x*num hope it helps

1 Like

I came to a point where i was like BFS it is and i let my actual working code behind. Real stupid.

1 Like