Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerEarth Milly and Chocolates IV problem solution

YASH PAL, 31 July 2024
In this HackerEarth Milly and Chocolates IV problem solution, Milly loves to eat chocolates. She has N different chocolates. She needs to choose only one of them. At the moment, ith chocolate already has Pi pieces. The jth piece of the ith chocolate requires Tij seconds to eat. Milly knows that she will take K seconds to break one piece from any chocolate and wait for M seconds after eating any piece.
Your task is to help her in selecting chocolate that she will eat completely in the minimum possible time (including the waiting time).
HackerEarth Milly and Chocolates IV problem solution

HackerEarth Milly and Chocolates IV problem solution.

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <limits.h>
#include <vector>
#include <assert.h>

using namespace std;


int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
long long k, m;
scanf("%d%lld%lld", &n, &k, &m);

vector <int> p(n);
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
long long ans_time = 1e18;
int ans_index = 1;
for (int i = 0; i < n; i++) {
long long sum = 0;
for (int j = 0; j < p[i]; j++) {
long long tij;
scanf("%lld", &tij);
sum += tij;
}
long long temp_time = k*p[i] + sum + m*(p[i]-1);
if (temp_time < ans_time) {
ans_time = temp_time;
ans_index = i + 1;
}
}
printf("%d %lldn", ans_index, ans_time);
}
}

Second solution

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pi(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define pn printf("n")
ll pow_mod(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
int p[1005];
ll T[1005][105];
int main() {
int t;
cin >> t;
assert(t >= 1 && t <= 10);
while(t--) {
int n, k, m;
cin >> n >> k >> m;
assert(n >= 1 && n <= 1000);
assert(k >= 1 && k <= 100000);
assert(m >= 1 && m <= 100000);
for(int i = 0; i < n; ++i) {
cin >> p[i];
assert(p[i] >= 1 && p[i] <= 100);
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < p[i]; ++j) {
cin >> T[i][j];
assert(T[i][j] >= 1 && T[i][j] <= (ll)(1e9));
}
}
ll res = LONG_LONG_MAX;
int idx = -1;
for(int i = 0; i < n; ++i) {
ll tmp = 0;
for(int j = 0; j < p[i]; ++j) {
tmp += T[i][j];
tmp += k;
}
tmp += ((p[i] - 1) * m);
if(tmp < res) {
res = tmp;
idx = i;
}
}
cout << (idx + 1) << " " << res << endl;
}
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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