AcWing 5980. 训练士兵 JAVA 前缀和
原题链接
简单
作者:
彧_05
,
2025-04-03 14:25:15
· 江西
,
所有人可见
,
阅读 2
java 前缀和
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long s = sc.nextLong();
int t = 0;
long ans = 0;
long cost = 0;
int N = 10000000 + 10;
long[] a = new long[N];
for (int i = 0; i < n; i++) {
int p = sc.nextInt();
int c = sc.nextInt();
cost += p;
a[c] += p;
t = Math.max(t, c);
}
for (int i = 1; i <= t; i++) {
if (cost > s) {
ans += s;
} else {
ans += cost;
}
cost -= a[i];
}
System.out.println(ans);
}
}