function solution(users, emoticons) {
const discount = Array(emoticons.length).fill(40);
const used = new Set([discount.join('')]);
const discountKing = (percent, max) => {
const sales = users.reduce((acc, [wish, money]) => {
let sum = 0;
for (let i = 0; i < percent.length; i++) {
if (percent[i] < wish) continue;
sum += emoticons[i] / 100 * (100 - percent[i]);
if (money <= sum) {
acc[0] += 1;
return acc;
}
}
acc[1] += sum;
return acc;
}, [0, 0]);
let result = getMax(sales, max);
for (let i = 0; i < percent.length; i++) {
const next = [...percent];
next[i] -= 10;
if (next[i] < 10) continue;
const use = next.join('');
if (used.has(use)) continue;
used.add(use);
result = getMax(sales, discountKing(next, result));
}
return result;
}
return discountKing(discount, [0, 0]);
}
const getMax = (a, b) =>
a[0] < b[0] ? b :
a[0] > b[0] ? a :
a[1] > b[1] ? a : b;