跳到正文

个人记录

666

dl_001约 2 分钟
原文

这是 2026年8月24日 存档的第 10 版,不是当前版本。查看当前版本

文章信息与版本

#include<iostream> #include<cstdio> #include<deque> #include<stack> using namespace std; deque<long long> de; stack<long long> st; int n, cnt; int a[100010]; long long ans; int main() { scanf(“%d”, &n); for (int i = 1;i <= n;i++) { scanf(“%d”, &cnt); a[cnt]++; } if (n == 1) { printf(“%d”, cnt); return 0; } int x = 1; while (n != 0) { while (a[x] != 0) { de.push_back(x); a[x]—; n—; } x++; } while (de.size() > 1) { long long x = de.front(); de.pop_front(); long long y = de.front(); de.pop_front(); long long z = x + y; ans += z; if (!de.empty()) { while (!de.empty() && z > de.front()) { st.push(de.front()); de.pop_front(); } } de.push_front(z); while (!st.empty()) { de.push_front(st.top()); st.pop(); } } printf(“%d”, ans); return 0; }