-
Notifications
You must be signed in to change notification settings - Fork 6
/
DoctorWho-theGift.txt
50 lines (39 loc) · 1.19 KB
/
DoctorWho-theGift.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Read inputs from System.in, Write outputs to System.out.
// Your class name has to be Solution
import java.util.*;
import java.io.*;
import java.math.*;
class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int nbPerson = in.nextInt();
int montantCadeau = in.nextInt();
ArrayList<Integer> participant = new ArrayList<>();
ArrayList<Integer> participantSomme = new ArrayList<>();
int sommeTotale = 0;
for (int i = 0; i < nbPerson; i++) {
int montant = in.nextInt();
participant.add(montant);
sommeTotale += montant;
}
Collections.sort(participant);
if(sommeTotale<montantCadeau){
System.out.println("IMPOSSIBLE");
}else {
int reste = montantCadeau;
for (int i = 0; i < nbPerson; i++) {
int part = reste/(nbPerson-i);
if(participant.get(i)<part){
participantSomme.add(participant.get(i));
reste -= participant.get(i);
}else{
participantSomme.add(part);
reste -= part;
}
}
for (int i = 0; i < nbPerson; i++) {
System.out.println(participantSomme.get(i));
}
}
}
}