# Editorial-Longest 6 |Encoding Junior November 21

Author: Srinjoy Pal
Tester: Abhisekh Paul ,Sayan Poddar
Editorialist: Srinjoy Pal

CAKEWALK

# PREREQUISITES:

Basic Math , Conditional Statements

# PROBLEM:

Finding max element of the array. Then performing simple math operations we have to find the cost of each steroid . find the minimum cost.

# EXPLANATION:

First we have to find the max element of the array. Subtracting N from the max element and adding 1 we get the required length to cover. Next, We have to find the number of each steroid required to cover the distance. Then , we have to find the minimum cost for buying out of the two.

O(n)

O(1)

# SOLUTION:

Setterâ€™s Solution
C++

``````#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("ecjnov5.in", "r", stdin);
freopen("ecjnov5.out", "w", stdout);
ll t;
cin>>t;
while(t--)
{
ll m,p,x,q,y,r,st1,st2;
cin>>m>>p>>x>>q>>y>>r;
ll a[p];
for(int i=0;i<p;i++)
{
cin>>a[i];
}
ll z=*max_element(a, a + p);
ll req= z-m+1;
if(req%x==0)
st1=req/x;
else if(req%x!=0)
st1=req/x+1;
if(req%y==0)
st2=req/y;
else if(req%y!=0)
st2=req/y+1;
cout<<min(st1*q,st2*r)<<endl;
}
return 0;
}
``````

Testerâ€™s Solution
Python

``````import math
d = int(input())
for i in range(0, d):
m,p,x,q,y,r= list(map(int, input().split()))
a = list(map(int, input().split()))
z=max(a)
n=z-m+1
print(min(((math.ceil(n/x))*q),((math.ceil(n / y)) * r)))
``````

Testerâ€™s Solution
Java

``````//package myplace;
import java.util.*;
class exam {
public static void  main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=Integer.parseInt(sc.nextLine());
while(t>0)
{
String s[]=sc.nextLine().split(" ");
int m=Integer.parseInt(s[0]);
int p=Integer.parseInt(s[1]);
int x=Integer.parseInt(s[2]);
int q=Integer.parseInt(s[3]);
int y=Integer.parseInt(s[4]);
int r=Integer.parseInt(s[5]);
String s1[]=sc.nextLine().split(" ");
int max=0;
for(int i=0;i<s1.length;i++)
{
if(Integer.parseInt(s1[i])>max)
{
max=Integer.parseInt(s1[i]);
}
}

double diff=max-m+1;
double diffx=diff/x;
double diffy=diff/y;
int a1= (int) (Math.ceil(diffx)*q);
int a2=(int) (Math.ceil(diffy)*r);
double cost=Math.min(a1,a2);
System.out.println((int)cost);

t--;

}
}
}``````
2 Likes