p485.in1
======================
1 2
4 1 8 2 0 5

=================
p485.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
}

int n;
vector<int> vals;

void read() {
    vals.resize(3 * n);
    cin >> vals;
}

int dp[1 << 25];
int8_t popcnt[1 << 25];

void precompute() {
    popcnt[0] = 0;
    for(int mask = 1; mask < (1 << 25); mask++) {
        popcnt[mask] = popcnt[mask >> 1] + (mask & 1);
    }
}

void solve() {
    vector<pair<int, int>> v;
    for(int i = 0; i < 3 * n; i++) {
        v.emplace_back(vals[i], i);
    }

    sort(v.begin(), v.end());

    vector<pair<int, int>> B, AC;
    // It's always optimal to take the first n elements for C
    B.insert(B.end(), v.begin(), v.begin() + n);
    AC.insert(AC.end(), v.begin() + n, v.end());

    // We maintain a rolling window starting from the end of AC. The
    // mask represents the elements we have taken. Note that it's always
    // best to put the A[cnt] at the end of the mask, or the last zero bit.
    // For the C, we can put it anywhere before. After this step, we need to
    // "recycle" the mask by shifting it left and filling the last zero.
    // Without the recycling, the naive solution would be O*(4^n), but the
    // core observation that allows us to do this recycling idea is that it
    // never makes sense to put the C with the corresponding A very far from
    // each other.

    memset(dp, 0, (1 << n) * sizeof(int));
    int top_zero = n;
    for(int mask = 0; mask < (1 << n); mask++) {
        int cnt = popcnt[mask];
        int next_mask = mask << 1;
        if((next_mask >> top_zero) & 1) {
            top_zero--;
        }

        next_mask |= (1 << top_zero);
        next_mask &= (1 << n) - 1;

        for(int i = 0; i < n; i++) {
            if((next_mask >> i) & 1) {
                break;
            }

            int Bj = B[cnt].first;
            int Aj = AC[n - cnt + top_zero - 1].first;
            int Cj = AC[n - cnt + i - 1].first;

            dp[next_mask | (1 << i)] =
                max(dp[next_mask | (1 << i)], dp[mask] + (Aj - Bj) * Cj);
        }
    }

    cout << dp[(1 << n) - 1] << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    precompute();

    int T = 1;
    cin >> T >> n;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p485.ans1
======================
46

=================
statement.txt
======================
485. Arrays
Time limit per test: 1.75 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



You are given a sequence of 3· N integers (X1, X2, ·s, X3· N). Create three sequences (A1, A2, ·s, AN), (B1, B2, ·s, BN) and (C1, C2, ·s, CN) such that:


each of the integers from 1 to 3· N belongs to exactly one of the sequences A, B or C;

and the value of:

SUM from i=1 to N (Ai - Bi) * Ci 

is the largest possible.


Input

Constraints on N	Constraints on T
1 ≤ N ≤ 10	1 ≤ T ≤ 1000
11 ≤ N ≤ 15	1 ≤ T ≤ 100
16 ≤ N ≤ 20	1 ≤ T ≤ 10
21 ≤ N ≤ 25	T = 1

The input file contains T test cases, all having the same value of N. The first line of the input file contains the integers T and N, constrained as shown in the adjacent table. Each of the following T lines describes one test case and contains 3· N integers, the members of the sequence X. All these values are in the range from 0 to 1000.

Output
The output file should consist of T lines. Each line should contain the largest possible value of S for the corresponding test case from the input file.

Example(s)
sample input
sample output
1 2
4 1 8 2 0 5
46



Note. The maximal value is attained by taking A = (1, 3), B = (2, 5), C = (4, 6).


=================
