Can someone please point out the mistake in my code for GASOLINE

public static class Distance 
    {
        static int dis;
        static int price;
        Distance(int dis, int price)
        {
            this.dis=dis;
            this.price=price;
        }
        static int getDistance()
        {
            return dis;
        }
        static int getPrice()
        {
            return price;
        }
    }
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n=sc.nextInt();
            int f[]=new int[n];
            int c[]=new int[n];
            List<Distance> list=new ArrayList();
            for(int i=0;i<n;i++)
                f[i]=sc.nextInt();
            for(int i=0;i<n;i++)
                c[i]=sc.nextInt();
            for(int i=0;i<n;i++)
            {
                if(f[i]!=0 && c[i]!=0)
                    list.add(new Distance(f[i],c[i]));
            }
            Collections.sort(list,new Comparator<Distance>() {
                public int compare(Distance d1, Distance d2)
                {
                    if(d1.getPrice()>d2.getPrice())
                        return 1;
                    else
                        return -1;
                }
            });
            int count=0,i=0;
            long coin=0;
            List<Long> ans=new ArrayList();
            while(count<n)
            {
                int a0=list.get(i).getDistance();
                int a1=list.get(i).getPrice();
                System.out.println(a0+" "+a1);
                if(a0>=n)
                {
                    ans.add((long)(n*a1));
                    i++;
                    if(i==list.size())
                        break;
                    continue;
                }
                count=count+a0;
                coin=coin+(a1*a0);
                i++;
                if(i==list.size())
                    break;
            }
            if(ans.size()>0)
            {
                Collections.sort(ans);
                if(ans.get(0)<coin)
                    coin=ans.get(0);
            }
            System.out.println(coin);
        }
    }

few cases where ur code might go wrong

  1. integer overflow(the most probable case)
  2. f[i]!=0 is enough, why are you considering c[i]!=0 too i have no idea
  3. remove the System.out.println inside the while loop
  4. why are you taking a0>=n, you should greedily choose cars where a0>=n-count and not n
    (in short, you unnecessarily complicated ur while loop after figuring out the hard part)
    in case you wanna see my implementation, click here
1 Like

Thank you so much for your help. Your code helped me to understand the shortcomings of my solution. Thank you so very much. Sir could you also link your codeforces id as I would love to get some guidance from you (Since there is no feature to message you personally on codechef but I can do that on codeforces.)

/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static class Distance
{
static int dis;
static int price;
Distance(int dis, int price)
{
this.dis=dis;
this.price=price;
}
static int getDistance()
{
return dis;
}
static int getPrice()
{
return price;
}
}
public static void main (String[] args) throws java.lang.Exception
{
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t–>0)
{
int n=sc.nextInt();
int f[]=new int[n];
int c[]=new int[n];
List list=new ArrayList();
for(int i=0;i<n;i++)
f[i]=sc.nextInt();
for(int i=0;i<n;i++)
c[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
if(f[i]!=0)
list.add(new Distance(f[i],c[i]));
}
Collections.sort(list,new Comparator() {
public int compare(Distance d1, Distance d2)
{
if(d1.price>d2.price)
return 1;
else if(d1.price<d2.price)
return -1;
else
return d1.dis>d2.dis? 1:-1;
}
});
int count=0,i=0;
long coin=0;
while(count<n)
{
int a0=list.get(i).getDistance();
int a1=list.get(i).getPrice();
if(a0>=n-count)
{
coin=coin+(long)((n-count)a1);
break;
}
else
{
coin=coin+(long)(a0
a1);
count+=a0;
i++;
}
}
System.out.println(coin);
}
} catch(Exception e) {
} finally {
}
}
}

I’m still getting WA.

(Please someone tell me how to give proper indentation while posting my code in comments.)