In this HackerEarth Chandu and his toy stack problem solution After setting up the area. Chandu wanted all his toys to be stacked there in that area. So that all of them are accessible easily. Currently, He is having N stacks of toys each with height H1, H2...Hn (assuming all toys are of same height).Chandu did not like the configuration much and want to change the height of each stack. To increase the height of a particular stack he can add some toys to it and to decrease the height he can take out some toys from that stack. Chandu, knows that he requires X units of effort for putting up an item onto the stack and Y units of effort for removing it. Help, chandu in setting up his toys.


HackerEarth Chandu and his toy stack problem solution


HackerEarth Chandu and his toy stack problem solution.

#include <bits/stdc++.h>

using namespace std;

long long start[100000 + 10];
long long finall[100000 + 10];

int main()
{

long long t , x , y, n ,ans;

cin >> t;

while(t--){

ans = 0;

cin >> n >> x >> y;

for(int i = 0; i < n; i++) cin >> start[i] >> finall[i];

sort(start, start + n);
sort(finall, finall + n);

for(int i = 0; i < n; i++){

if(finall[i] > start[i]) ans += (finall[i] - start[i]) * x;
else if (finall[i] < start[i]) ans += (start[i] - finall[i]) * y;
}

cout << ans << "\n";
}


return 0;
}

Second solution

#include<iostream>
#include<algorithm>
using namespace std;

int main()
{ int t,pen,n,x,y,i;
scanf("%d",&t);
while(t--)
{ pen=0;
scanf("%d%d%d",&n,&x,&y);
int a[n],b[n];
for(i=0;i<n;i++)

scanf("%d%d",&a[i],&b[i]);
sort(a,a+n);
sort(b,b+n);

for(i=0;i<n;i++)
{
if(a[i]==b[i])
continue;

else if(b[i]>a[i])
pen+=(b[i]-a[i])*x;

else
pen+=(a[i]-b[i])*y;
}

printf("%d\n",pen);
}
return 0;
}