[SWEA] 최대 성적표 만들기

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

내림차순 정렬 후 앞에서부터 더해준다.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1; tc<=T; tc++) {
			 int N = sc.nextInt();
			 int K = sc.nextInt();
			 Integer[] ar = new Integer[N];
			 for(int i=0; i<N; i++) {
				 ar[i] = sc.nextInt();
			 }
			 
			 Arrays.sort(ar,Collections.reverseOrder());
			 int sum = 0;
			 for(int i=0; i<K; i++) {
				 sum += ar[i];
			 }
			 System.out.println("#"+tc+" "+sum);
		}
	}

}