CF865D.Buy Low Sell High

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

你可以完美预测某只股票未来 NN 天的价格。你想利用这一信息获利,但每天最多只能交易一股股票。也就是说,每天你只能选择买入一股、卖出一股,或不进行任何操作。初始时你持有零股股票,且不能在持股数为零时卖出股票。在 NN 天结束时,你希望再次持有零股股票,但要使最终拥有的资金尽可能多。

输入格式

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

Following this is a line with exactly N integers _p_1, _p_2, ..., p__N (1 ≤ p__i ≤ 106). The price of one share of stock on the i-th day is given by p__i.

输入的第一行是一个整数 NN(2≤N≤3⋅1052 \leq N \leq 3\cdot10^5),表示天数。

接下来的一行包含恰好 NN 个整数 p1, p2, ..., pNp_1,\,p_2,\,...,\,p_N(1≤pi≤1061 \leq p_i \leq 10^6)。第 ii 天一股股票的价格为 pip_i。

输出格式

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

输出在 N 天结束时你能获得的最大金额。

输入输出样例

  • 输入#1

    9
    10 5 4 7 9 12 6 2 10

    输出#1

    20
  • 输入#2

    20
    3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4

    输出#2

    41

说明/提示

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.

在第一个例子中,以 5 的价格买入一股,再以 4 的价格买入一股,然后以 9 的价格卖出一股、以 12 的价格卖出另一股;接着以 2 的价格买入一股,再以 10 的价格卖出一股。总利润为 −5−4+9+12−2+10=20-5 - 4 + 9 + 12 - 2 + 10 = 20。

输入解题思路,AI测评打分。不知道怎么写?

首页