public static void main (String[] args) throws java.lang.Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(r.readLine());
while(t-->0) {
int n = Integer.parseInt(r.readLine());
String[] s = r.readLine().split(" ");
long l = s.length;
long min = Long.parseLong(s[0]);
for(int i=0;i<(int)l;i++) {
if(Long.parseLong(s[i])<min) {
min = Long.parseLong(s[i]);
}
}
System.out.println((l-1)*min);
}
}
My solution is as many have submitted and appears right,still got only 60 points? I got wrong answer for the last subtask,I don’t understand how.Can someone help? https://www.codechef.com/viewsolution/7955688
@karthik_bhata7 The question says to minimize the cost which can be done if u pick the smallest number and compare it with its consecutive number and adding the cost.
But in your solution, you haven’t selected the smallest so it will always give cost greater than the minimum cost.
e.g suppose 5, 4, 3 are the numbers so your solution will give ( 5 > 4) = 4, ( 4 > 3 ) = 3
so cost = 4 + 3 = 7
but if we pick the smallest and then start comparing ( 3 < 4 ) = 3, ( 3 < 5) = 3
so cost = 3 + 3 = 6
Please Help Me…I have written code for this problem it also giving the desired output.But When i submitted it,It giving me wrong answer.Please Help Me.I am a beginner.Here is my solution https://www.codechef.com/viewsolution/10613974
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int n = sc.nextInt();
ArrayList al = new ArrayList();
for(int i=0;i<n;i++){
int x = sc.nextInt();
al.add(x);
}
/*for(int i=0;i<n;i++){
System.out.println(al.get(i));
}*/
int sum = 0;
while(al.size() > 1){
if((int)al.get(0) > (int)al.get(1)){
sum = sum+(int)al.get(1);
al.remove(0);
}
else{
sum = sum+(int)al.get(0);
al.remove(1);
}
}
System.out.println(sum);
}
}
as min and n-1 both are int so their product will also be an int and it will be assigned to ans.
and in the product overflow will happen as its int only