#B4468. 符号选择 / opt

    ID: 16866 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 1 上传者: 标签>2025O2优化排序山西数组科创活动小学活动

符号选择 / opt

Problem Description

Given a sequence of nn natural numbers x1,x2,...,xnx_1, x_2, ..., x_n.

Now, assign either a positive sign (+xi+x_i) or a negative sign (xi-x_i) to each number xix_i in the sequence, and then compute the sum.

Specifically, among the nn numbers, exactly kk numbers must use the positive sign, and the remaining nkn-k numbers must use the negative sign.

Determine the maximum possible sum that can be obtained through this operation.

Input Format

The first line contains two integers nn and kk, representing the length of the sequence and the required number of positive signs, respectively.

The second line contains nn natural numbers x1,x2,...,xnx_1, x_2, ..., x_n, representing the given sequence. It is guaranteed that x1x2...xnx_1 \ge x_2 \ge ... \ge x_n.

Output Format

A single line containing an integer, representing the maximum possible sum that can be obtained.

3 2
5 2 1
6

Hint

[Constraints]

For 20%20\% of the data, 1n101 \le n \le 10.

For another 10%10\% of the data, n=kn = k.

For another 10%10\% of the data, all xix_i are equal.

For 100%100\% of the data, 1kn1051 \le k \le n \le 10^5, 0xi1090 \le x_i \le 10^9.


Translated by DeepSeek-V3