Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Chandu and his toy stack problem solution

YASH PAL, 31 July 202416 February 2026
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("%dn",pen);
}
return 0;
}
 
coding problems solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes