3 3 20 19
if x>y we use x because we have no adjacent pairs to sum with and compare with y, so answer is always x
you commented above for a condition x > y, isnt the max value a cell can hold x ?
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";
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
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.
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;
}
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 .
My code : CodeChef: Practical coding for everyone
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
Failed the base case very early on.Never felt this stupid all my life and i have been pretty stupid all my life.
in else if condition it is ans=xnum instead of ans=2x*num hope it helps
I came to a point where i was like BFS it is and i let my actual working code behind. Real stupid.