public class SpaceWarDiv2 {
public int minimalFatigue(int[] magicalGirlStrength, int[] enemyStrength, int[] enemyCount) {
int n = magicalGirlStrength.Length;
magicalGirlStrength = magicalGirlStrength.OrderByDescending(s => s).ToArray();
var enemies = Enumerable.Range(0, enemyStrength.Length)
.SelectMany(i => Enumerable.Repeat(enemyStrength[i], enemyCount[i]))
.OrderByDescending(s => s)
.ToArray();
int[] fatigue = new int[n];
foreach (var s in enemies) {
int index = -1;
for (int i = 0; i < n; i++) {
if (s <= magicalGirlStrength[i] &&
(index == -1 || fatigue[index] > fatigue[i])) {
index = i;
}
}
if (index == -1) return -1;
fatigue[index]++;
}
return fatigue.Max();
}
}