優先度付きキュー(ABC 062 D問題)
便利だね
問題
以下ネタバレ注意
問題
以下ネタバレ注意
- ll a[300030];
- ll Left[3000030], Right[300030];
- int main() {
- int N;
- cin >> N;
- REP(i, 3 * N) {
- cin >> a[i];
- }
- ll left = 0;
- //小さい順に取り出す
- priority_queue<ll, vector<ll>, greater<ll>> minpq;
- REP(i, N) {
- left += a[i];
- minpq.push(a[i]);
- }
- Left[N-1] = left;
- FOR(i, N, 2*N) {
- minpq.push(a[i]);
- Left[i] =Left[i-1]+ a[i];
- ll c = minpq.top();
- minpq.pop();
- Left[i]-= c;
- }
- ll right = 0;
- priority_queue<ll> maxpq;
- FORR(i, 3*N - 1, 2*N) {
- right += a[i];
- maxpq.push(a[i]);
- }
- Right[2 * N] = right;
- FORR(i, 2*N - 1, N-1) {
- Right[i] =Right[i+1]+ a[i];
- maxpq.push(a[i]);
- ll c = maxpq.top();
- maxpq.pop();
- Right[i] -= c;
- }
- ll ans = -9999999999999999;
- FOR(i,N, 2 * N+1) {
- ans = max(ans, (Left[i - 1] - Right[i]));
- }
- cout << ans << endl;
- }