60分3个T 大佬求调(玄关)
查看原帖
60分3个T 大佬求调(玄关)
1802594
Stick_Man_楼主2025/7/24 10:56
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

const int MAXN = 1e7 + 5;
int a[MAXN];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    sort(a, a + n);
    
    queue<long long> q1, q2;
    for (int i = 0; i < n; ++i) {
        q1.push(a[i]);
    }
    
    long long ans = 0;
    for (int i = 1; i < n; ++i) {
        long long x, y;
        if (q2.empty() || (!q1.empty() && q1.front() <= q2.front())) {
            x = q1.front(); q1.pop();
        } else {
            x = q2.front(); q2.pop();
        }
        if (q2.empty() || (!q1.empty() && q1.front() <= q2.front())) {
            y = q1.front(); q1.pop();
        } else {
            y = q2.front(); q2.pop();
        }
        ans += x + y;
        q2.push(x + y);
    }
    cout << ans;
    return 0;
}

2025/7/24 10:56
加载中...