跳到正文

剪贴板

剪贴板 qxrhaz8d

原文
剪贴板信息

剪贴板信息

收录
更新
C++
const int N = 1005, inf = 1e9;
using node = pair<int, int>;
int n; string s;
node x[N], f[N];
node operator+ (const node& a, const node& b) {
    return node(a.first + b.first, a.second + b.second);
}

void _main() {
    cin >> n >> s;
    for (int i = 1; i <= n; i++) cin >> x[i];
    sort(x + 1, x + n + 1, [](const node& a, const node& b) -> bool {
        if (a.first != b.first) return a.first > b.first;
        return a.second < b.second;
    });
    int bg = s == "Petra" ? 2 : 1, sum = 0;
    int m = (n - bg + 2) / 2;
    fill(f + 1, f + m + 1, node(-inf, -inf));
    for (int i = bg; i <= n; i++) {
        auto [a, b] = x[i]; sum += a;
        for (int j = min(i, m); j >= 1; j--) {
            if (i < 2 * j + bg - 2) continue;
            chkmax(f[j], f[j - 1] + node(b, -a));
        }
    }
    int p = sum + f[m].second, q = f[m].first;
    if (bg == 2) p += x[1].first;
    cout << p << ' ' << q << '\n';
}