Competitive Programming Solutions

GitHub
back to dashboard

C. Choose Elements

Codeforces - Contest #3.1 · limit: 1s · memory: 256MB · view on codeforces

input

2 2
1 2

output

3
Main.java
static void solve() {

        // Write your solution here
        int n = in.nextInt();
        int k = in.nextInt();
        int[] a = readIntArray(n);
        safeSort(a);
        long sum = 0;
        for(int i = n - 1; i >= n - k; i--){
            sum += (a[i] >= 0) ? a[i] : 0;
        }
        out.print(sum);
    }