You can perfectly predict the price of a certain stock for the next N days. You would like to profit on this knowledge, but only want to transact one share of stock per day. That is, each day you will either buy one share, sell one share, or do nothing. Initially you own zero shares, and you cannot sell shares when you don't own any. At the end of the N days you would like to again own zero shares, but want to have as much money as possible.

Input

Input begins with an integer N (2 ≤ N ≤ 3·105), the number of days.

Following this is a line with exactly N integers p1, p2, ..., pN (1 ≤ pi ≤ 106). The price of one share of stock on the i-th day is given by pi.

Output

Print the maximum amount of money you can end up with at the end of N days.

Examples

Input
9
10 5 4 7 9 12 6 2 10
Output
20
Input
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output
41

Note

In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is  - 5 - 4 + 9 + 12 - 2 + 10 = 20.

思路:依旧是贪心,维护一个优先队列,每次读取一个数与队顶判断,如果大于就出队并入队2次读取的数,答案加上差值,否则就入队一次。为什么要入队两次,因为有可能这次不应该卖,应该买,所以就需要入2次,例如:1 3 5 6,读取3时,答案加上2,这次应该是不卖而是买,入队2次,这样下次就是叠加上次的操作,5 - 3 + 3 - 1 == 5 - 1代码如下:

int n;

int main() {
scanf("%d", &n);
priority_queue<LL,vector<LL>,greater<LL>> q;
LL sum = , now;
scanf("%I64d", &now);
q.push(now);
for(int i = ; i < n; ++i) {
scanf("%I64d", &now);
if(now > q.top()) {
sum += now - q.top();
q.pop();
q.push(now);
}
q.push(now);
}
printf("%I64d\n",sum);
return ;
}

Day3-F-Buy Low Sell High-CodeForces867E的更多相关文章

  1. 【CF865D】Buy Low Sell High(贪心)

    [CF865D]Buy Low Sell High(贪心) 题面 洛谷 CF 题解 首先有一个\(O(n^2)\)的\(dp\)很显然,设\(f[i][j]\)表示前\(i\)天手中还有\(j\)股股 ...

  2. Codeforces Round #437 E. Buy Low Sell High

    题意:买卖股票,给你n个数,你可以选择买进或者卖出或者什么都不做,问你最后获得的最大收益是多少. Examples Input 910 5 4 7 9 12 6 2 10 Output 20 Inpu ...

  3. CodeForces - 867E Buy Low Sell High (贪心 +小顶堆)

    https://vjudge.net/problem/CodeForces-867E 题意 一个物品在n天内有n种价格,每天仅能进行买入或卖出或不作为一种操作,可以同时拥有多种物品,问交易后的最大利益 ...

  4. CF865D Buy Low Sell High

    /* 贪心来选择, 如果能找到比当前小的, 就用最小的来更新当前的 优先队列即可 */ #include<cstdio> #include<algorithm> #includ ...

  5. [ CodeForces 865 D ] Buy Low Sell High

    \(\\​\) \(Description\) 给出\(N\)天股票的价钱\(A_1,...,A_N\),每天可以什么都不做,或者买入或卖出\(1\)支股票,分别花出或收入\(A_i\)元,求最大收益 ...

  6. CF867E: Buy Low Sell High(贪心, STL) (hdu6438)

    Description 有nn个城市,第ii个城市商品价格为aiai​,从11城市出发依次经过这nn个城市到达n n城市,在每个城市可以把手头商品出售也可以至多买一个商品,问最大收益. Input 第 ...

  7. CF865D Buy Low Sell High 贪心

    正解:贪心 解题报告: 传送门! 这题首先有个很显然的dp,太基础了不说QAQ 然后考虑dp是n2的,显然过不去,所以换一个角度 然后发现这题和普通的dp的题有什么不同呢?就它这儿是一天只能买一支股, ...

  8. Buy Low Sell High CodeForces - 867E (思维,贪心)

    大意: 第i天可以花$a_i$元买入或卖出一股或者什么也不干, 初始没钱, 求i天后最大收益 考虑贪心, 对于第$x$股, 如果$x$之前有比它便宜的, 就在之前的那一天买, 直接将$x$卖掉. 并不 ...

  9. 【CodeForces】866D. Buy Low Sell High

    [题意]已知n天股价,每天可以买入一股或卖出一股或不作为,最后必须持0股,求最大收益. [算法]堆 贪心? [题解] 不作为思想:[不作为=买入再卖出] 根据不作为思想,可以推出中转站思想. 中转站思 ...

  10. leetcode 第188题,我的解法,Best Time to Buy and Sell Stock IV

    <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255) ...

随机推荐

  1. 获取天气预报java代码

    import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; imp ...

  2. 如果谷歌浏览器突然打不开网页,而且显示:"网页可能暂时无法连接,或者它已永久性地移动到了新网址,返回ERR_TUNNEL_CONNECTION_FAILED",怎么办?用这个方法,亲试有效!!!

    打开cmd: 依次输入: ipconfig /flushdnsnbtstat –rnetsh int ip resetnetsh winsock reset 效果图 然后我的浏览器就能正常使用了,很有 ...

  3. highcharts饼图

    效果: JSON加载数据: var chartseries2 = [ { name: '完成' + data.rate + '%', y: data.rate }, { name: '未完成' + d ...

  4. 多Python版本共存

    Python 3.4 和 3.7 共存 我的电脑上同时安装了 Python 3.4 和 Python 3.7 两个 Python 版本.现在打开终端窗口进入指定的版本. py -3.4 py -3.7 ...

  5. 使用Servlet处理AJAX请求

    AJAX用于异步更新页面的局部内容. ajax常用的请求数据类型 text    纯文本字符串 json    json数据 使用ajax获取text示例 此种方式常用于前端向后台查询实体的一个属性( ...

  6. android 支持上拉加载,下拉刷新的列表控件SwipeRefreshLayout的二次封装

    上拉加载,下拉刷新的列表控件,大家一定都封装过,或者使用过 源代码,我会在最后贴出来 这篇代码主要是为了解决两个问题 1.滑动冲突得问题 2.listview无数据时,无数据布局的展示问题 下方列出的 ...

  7. unity优化-内存(网上整理)

    内存优化内存的开销无外乎以下三大部分:1.资源内存占用:2.引擎模块自身内存占用:3.托管堆内存占用.在一个较为复杂的大中型项目中,资源的内存占用往往占据了总体内存的70%以上.因此,资源使用是否恰当 ...

  8. Java基础知识笔记第五章:子类与继承

    子类与父类 子类 class 子类名 extends 父类名{ ....... } 类的树形结构 子类的继承性 子类和父类在同一包中的继承性 子类继承了父类不是private的成员属性和成员方法   ...

  9. SSH整合hibernate无法正常自动生成表

    检查持久化类的属性和映射文件是否正确配置,比如date格式的属性最容易配置错误

  10. day1-2基本数据类型

    /*5种基本数据类型 1,Undefined var a; a = undefined Undefined派生自Null 2,Null(本质属于object,单独细分出来的) var a = null ...