Skip to content

Instantly share code, notes, and snippets.

@Jack-Saleem
Created July 17, 2016 13:49
Show Gist options
  • Save Jack-Saleem/d13a89447606dfa118ba46ecbcbb3d13 to your computer and use it in GitHub Desktop.
Save Jack-Saleem/d13a89447606dfa118ba46ecbcbb3d13 to your computer and use it in GitHub Desktop.
codeforces 368A SerejaandCoatRack program in java
import java.util.Arrays;
import java.util.Scanner;
public class SerejaandCoatRack
{
public static void main(String[] args)
{
Scanner z=new Scanner(System.in);
int n=z.nextInt();
int d=z.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i]=z.nextInt();
int m=z.nextInt();
Arrays.sort(a);
int sum=0;
if(a.length>=m){
for(int i=0;i<m;i++)
sum+=a[i];
System.out.println(sum);
} else {
for(int i=0;i<n;i++)
sum+=a[i];
System.out.println(sum-((m-n)*d));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment