Let N be a positive integer.

There is a numerical sequence of length 3N, a=(a1,a2,…,a3N). Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements. Here, the score of a' is defined as follows: (the sum of the elements in the first half of a')−(the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

  • 1≤N≤105
  • ai is an integer.
  • 1≤ai≤109

Partial Score

  • In the test set worth 300 points, N≤1000.

Input

Input is given from Standard Input in the following format:

N
a1 a2 a3N

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a2 and a6 are removed, a' will be (3,4,1,5), which has a score of (3+4)−(1+5)=1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a1 are removed, a' will be (2,3), which has a score of 2−3=−1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a2, a3 and a9 are removed, a' will be (8,7,4,6,5,3), which has a score of (8+7+4)−(6+5+3)=5.

要求删去N个数之后,前面一半的sum-后面一半的sum最大值;

我们可以用一个小根堆维护前面,用一个大根堆维护后面;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ int n;
ll a[maxn];
ll lftmax[maxn], minrgt[maxn];
int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i <3* n; i++)rdint(a[i]);
priority_queue<int, vector<int>, greater<int> >q1;
priority_queue<int>q2;
for (int i = 0; i < n; i++) {
lftmax[0] += a[i]; q1.push(a[i]);
minrgt[n + 1] += a[3 * n - i - 1]; q2.push(a[3 * n - i - 1]);
}
for (int i = 1; i <= n; i++) {
q1.push(a[n + i - 1]);
lftmax[i] = lftmax[i - 1] + a[n + i - 1] - q1.top();
q1.pop();
q2.push(a[n * 2 - i]);
minrgt[n - i + 1] = minrgt[n + 1 - i + 1] + a[n * 2 - i] - q2.top();
q2.pop();
}
ll res = -inf;
for (int i = 0; i <= n; i++) {
if (i == 0)res = lftmax[i] - minrgt[i + 1];
else res = max(res, lftmax[i] - minrgt[i + 1]);
}
cout << res << endl;
return 0;
}

AtCoder - 2566 优先队列的更多相关文章

  1. Atcoder 2566 3N Numbers(优先队列优化DP)

    問題文N を 1 以上の整数とします. 長さ 3N の数列 a=(a1,a2,…,a3N) があります. すぬけ君は.a からちょうど N 個の要素を取り除き.残った 2N 個の要素を元の順序で並べ. ...

  2. Atcoder D - 3N Numbers(优先队列+dp)

    题目链接:http://abc062.contest.atcoder.jp/tasks/arc074_b 题意:给出3*n个数要求去掉n个数使得剩下的前n个数-后n个数的差最大. 题解:显然是一道如果 ...

  3. Atcoder arc080E Young Maids(线段树+优先队列)

    给出一个n排列,每次可以选择相邻的两个数字放在新的排列首部,问最后形成的新的排列字典序最小是? 考虑新排列的第一个数字,则应是下标为奇数的最小数,下标不妨设为i.第二个数字应该下标大于i且为偶数的最小 ...

  4. Atcoder Grand Contest 037C(贪心,优先队列,思维)

    #define HAVE_STRUCT_TIMESPEC//编译器中time.h和phread.h头文件中timespec结构体重名,故加此行#include<bits/stdc++.h> ...

  5. Atcoder Beginner Contest 070 D - Transit Tree Path

    题意:n个点,n-1条边,组成一个无向的联通图,然后给出q和k,q次询问,每次给出两个点,问这两个点之间的最短距离但必须经过k点. 思路:我当时是用优化的Dijkstra写的(当天刚学的),求出k点到 ...

  6. 文字列大好きいろはちゃんイージー / Iroha Loves Strings (ABC Edition) (优先队列)

    题目链接:http://abc042.contest.atcoder.jp/tasks/abc042_b Time limit : 2sec / Memory limit : 256MB Score ...

  7. AtCoder ABC 085C/D

    C - Otoshidama 传送门:https://abc085.contest.atcoder.jp/tasks/abc085_c 有面值为10000.5000.1000(YEN)的纸币.试用N张 ...

  8. AtCoder刷题记录

    构造题都是神仙题 /kk ARC066C Addition and Subtraction Hard 首先要发现两个性质: 加号右边不会有括号:显然,有括号也可以被删去,答案不变. \(op_i\)和 ...

  9. AtCoder Beginner Contest 223

    AtCoder Beginner Contest 223 A是纯纯的水题,就不说了 B - String Shifting 思路分析 我真的sb,一开始想了好久是不是和全排列有关,然后读了好几遍题目也 ...

随机推荐

  1. python拷贝文件小程序(windows)

    #!/usr/bin/python import os source='F:\\lh.jpg' target='E:\\' copy_command="xcopy %s %s"%( ...

  2. libevent源码深度剖析十二

    libevent源码深度剖析十二 ——让libevent支持多线程 张亮 Libevent本身不是多线程安全的,在多核的时代,如何能充分利用CPU的能力呢,这一节来说说如何在多线程环境中使用libev ...

  3. libevent源码深度剖析八

    libevent源码深度剖析八 ——集成信号处理 张亮 现在我们已经了解了libevent的基本框架:事件管理框架和事件主循环.上节提到了libevent中I/O事件和Signal以及Timer事件的 ...

  4. SaeStorage使用示例

    新浪SAE官方地址:http://apidoc.sinaapp.com/sae/SaeStorage.html SaeStorage的代码详细:http://apidoc.sinaapp.com/__ ...

  5. msql 计算连续签到天数

    刚刚写了一个签到计算天数的sql, 记录下来. 思路如下: 获取当前签到的最后时间(今天或昨天), 定义一个变量@i 对签到时间进行天数自减, 然后查询出当前记录签到时间是否与自减后的时间匹配.   ...

  6. 507. Perfect Number 因数求和

    [抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...

  7. IP协议、ARP协议等之温故知新

    今天才知道: 1.IP协议的固定部分长度为20字节.(貌似有一家运维工程师面试我的时候,问过我这个问题呢.) 2.IP数据包首部中的协议?? 答:协议:占8位,指出此数据报携带的数据使用何种协议以便目 ...

  8. Linq to SQL Like Operator

    As a response for customer's question, I decided to write about using Like Operator in Linq to SQL q ...

  9. 积分之谜——第六届蓝桥杯C语言B组(国赛)第一题

    原创 标题:积分之迷 小明开了个网上商店,卖风铃.共有3个品牌:A,B,C. 为了促销,每件商品都会返固定的积分. 小明开业第一天收到了三笔订单: 第一笔:3个A + 7个B + 1个C,共返积分:3 ...

  10. wc.exe C++实现

    目录 Github项目地址 PSP表格 解题思路 设计实现过程 测试运行 项目小结 Github项目地址 wc-project PSP表格 PSP2.1 Personal Software Proce ...