我当时思路和你一样,但没有考虑另一端的人工龄更小的问题,只有40通过。我再去试试你的思路。
刚刚答完看见有位大佬的帖子,思路很清晰。试着按大佬的思路写了一下。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
for (int i = 0; i < n; i++) {
int score = scanner.nextInt();
if (!map.containsKey(score)) {
map.put(score, new ArrayList<>());
}
map.get(score).add(i);
}
int[] money = new int[n];
int[] temp = new int[n];
while (!map.isEmpty()) {
int lowestKey = map.firstKey();
for (Integer i : map.get(lowestKey)) {
if (i == 0) {
temp[i] = money[i + 1] + 100;
}else if (i == n - 1) {
temp[i] = money[i - 1] + 100;
} else {
temp[i] = Math.max(money[i - 1], money[i + 1]) + 100;
}
}
for (Integer i : map.get(lowestKey)) {
money[i] = temp[i];
}
map.remove(lowestKey);
}
int sum = 0;
for (Integer i : money) {
sum += i;
}
System.out.println(sum);
}
}