-
Notifications
You must be signed in to change notification settings - Fork 17
/
(medium) The Gift.js
35 lines (31 loc) · 1.17 KB
/
(medium) The Gift.js
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
/**
* The Gift https://www.codingame.com/training/medium/the-gift
* In this puzzle, you have to manipulate large lists and use simple math
* concepts (e.g. min, max, average) to optimize the value of a variable.
*
* Statement:
* Given a list of persons and their budgets, and the price of the present
* they wish to buy, you have to find the amount each person gives. You have
* to find this optimal distribution that minimize the highest contribution.
*
* Story:
* The TARDIS time-and-space-ship lands on a strange planet. The local
* aliens, the Oods, want to make a present for a fellow Ood, but they can't
* seem a way to figure out how to manage everyone's budget. Help the Doctor
* find a system to decide the contribution of each Ood.
*/
let N = +readline();
let C = +readline();
const wallets = [...Array(N)].map(_ => +readline()).sort((a, b) => a - b);
const arraySum = (arr) => arr.reduce((a, b) => a + b, 0);
C > arraySum(wallets)
? print('IMPOSSIBLE')
: (
wallets.forEach((wallet) => {
let part = Math.floor(C / N);
let contrib = Math.min(part, wallet);
C -= contrib;
N -= 1;
print(contrib);
})
);