题解 | #奖学金#
奖学金
http://www.nowcoder.com/practice/cee98a512ec246a2918ea8121f7612c8
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int r = sc.nextInt(); int avg = sc.nextInt(); int[][] arr = new int[n][2]; for (int i = 0; i < n; i++) { arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } long res = result(n, r, avg, arr); System.out.println(res); } sc.close(); } public static long result(long n, long r, long avg, int[][] arr) { Arrays.sort(arr, (o1, o2) -> (int) (o1[1] - o2[1])); long res = 0;//一共需复习时间 long socre = n * avg;//n门课拿奖学金所需的分数 int index=0; for (int[] num : arr) { socre -= num[0];//去除平时分,剩下的分数是复习所取得的 } while(socre>0){ if(socre>=r-arr[index][0]){ res+=arr[index][1]*(r-arr[index][0]); socre-=r-arr[index][0]; index++; }else{ res+=arr[index][1]*(socre); socre=0; } } return res; } }