#P16276. [蓝桥杯 2026 省 C] 回收处理

[蓝桥杯 2026 省 C] 回收处理

Problem Description

On the conveyor line of an automated factory, there are 3N3N parts arranged in order. Each part has a marked price aia_i: if it is recycled, this price is counted as revenue; if it is processed, this price is counted as cost.

As the person in charge of the factory area, Xiao Lan needs to choose two specific groups from these 3N3N parts:

  1. Recycling group: choose exactly NN parts to recycle, with total revenue denoted by RR.
  2. Processing group: choose exactly NN parts to process, with total cost denoted by CC.

Because the conveyor line is irreversible and moves in only one direction, the selection must follow a strict order: every recycled part must appear earlier than all processed parts in the original sequence (that is, if the indices of recycled parts are p1<p2<⋯<pNp_1 < p_2 < \cdots < p_N and the indices of processed parts are q1<q2<⋯<qNq_1 < q_2 < \cdots < q_N, then it must hold that pN<q1p_N < q_1).

Under this order constraint, the remaining NN parts on the line will be discarded directly, producing no revenue or cost.

Now Xiao Lan wants to find a plan that makes the total recycling revenue minus the total processing cost (R−CR - C) as large as possible. Please compute the maximum possible value of this difference.

Input Format

The first line contains an integer NN, representing the number of parts to be selected in each group.

The second line contains 3N3N integers a1,a2,…,a3Na_1, a_2, \dots, a_{3N}, representing the marked prices of the parts on the line from left to right.

Output Format

Output one integer, representing the maximum possible value of R−CR - C under all constraints.

2
1 10 5 1 2 1
13

Hint

Sample Explanation

The optimal plan is: recycle the 22nd and 33rd parts, and process the 44th and 66th parts. Then R=10+5=15R = 10 + 5 = 15, C=1+1=2C = 1 + 1 = 2, and R−C=13R - C = 13.

Constraints

For 40%40\% of the testdata, 1≤N≤10001 \leq N \leq 1000.

For all testdata, 1≤N≤1051 \leq N \leq 10^5, 1≤ai≤1091 \leq a_i \leq 10^9.

Translated by ChatGPT 5