D. Buy Low Sell High

Codeforces
IDCF866D
Time2000ms
Memory256MB
Difficulty
data structuresgreedytwo pointers
English · Original
Chinese · Translation
Formal · Original
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 _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_. ## Output Print the maximum amount of money you can end up with at the end of _N_ days. [samples] ## 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.
你可以完美预测未来 #cf_span[N] 天内某种股票的价格。你希望利用这一知识获利,但每天只能交易一股股票。也就是说,每天你只能选择买入一股、卖出一股,或不进行任何操作。初始时你持有 0 股,且不能在没有股票时卖出。在第 #cf_span[N] 天结束时,你希望再次持有 0 股,但希望拥有尽可能多的钱。 输入的第一行为一个整数 #cf_span[N] #cf_span[(2 ≤ N ≤ 3·105)],表示天数。 接下来是一行包含恰好 #cf_span[N] 个整数 #cf_span[p1, p2, ..., pN] #cf_span[(1 ≤ pi ≤ 106)]。第 #cf_span[i] 天一股股票的价格为 #cf_span[pi]。 请输出你在第 #cf_span[N] 天结束时能拥有的最大金额。 在第一个例子中,以价格 #cf_span[5] 买入一股,以价格 #cf_span[4] 再买入一股,然后以价格 #cf_span[9] 卖出一股,再以价格 #cf_span[12] 卖出另一股。接着以价格 #cf_span[2] 买入一股,以价格 #cf_span[10] 卖出。总利润为 #cf_span[ - 5 - 4 + 9 + 12 - 2 + 10 = 20]。 ## Input 输入的第一行为一个整数 #cf_span[N] #cf_span[(2 ≤ N ≤ 3·105)],表示天数。接下来是一行包含恰好 #cf_span[N] 个整数 #cf_span[p1, p2, ..., pN] #cf_span[(1 ≤ pi ≤ 106)]。第 #cf_span[i] 天一股股票的价格为 #cf_span[pi]。 ## Output 请输出你在第 #cf_span[N] 天结束时能拥有的最大金额。 [samples] ## Note 在第一个例子中,以价格 #cf_span[5] 买入一股,以价格 #cf_span[4] 再买入一股,然后以价格 #cf_span[9] 卖出一股,再以价格 #cf_span[12] 卖出另一股。接着以价格 #cf_span[2] 买入一股,以价格 #cf_span[10] 卖出。总利润为 #cf_span[ - 5 - 4 + 9 + 12 - 2 + 10 = 20]。
**Definitions** Let $ N \in \mathbb{Z} $ be the number of days, with $ 2 \leq N \leq 3 \cdot 10^5 $. Let $ P = (p_1, p_2, \dots, p_N) $ be a sequence of stock prices, where $ p_i \in \mathbb{Z} $ and $ 1 \leq p_i \leq 10^6 $ for all $ i \in \{1, \dots, N\} $. Let $ x_i \in \{-1, 0, 1\} $ denote the action on day $ i $: - $ x_i = 1 $: buy one share, - $ x_i = -1 $: sell one share, - $ x_i = 0 $: do nothing. Let $ s_i = \sum_{j=1}^i x_j $ be the cumulative number of shares owned after day $ i $. **Constraints** 1. $ s_0 = 0 $ (initially zero shares), 2. $ s_N = 0 $ (end with zero shares), 3. $ s_i \geq 0 $ for all $ i \in \{1, \dots, N\} $ (cannot sell short), 4. $ |x_i| \leq 1 $ for all $ i \in \{1, \dots, N\} $. **Objective** Maximize the total profit: $$ \sum_{i=1}^N x_i \cdot p_i $$
Samples
Input #1
9
10 5 4 7 9 12 6 2 10
Output #1
20
Input #2
20
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4
Output #2
41
API Response (JSON)
{
  "problem": {
    "name": "D. Buy Low Sell High",
    "description": {
      "content": "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 wi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF866D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 wi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你可以完美预测未来 #cf_span[N] 天内某种股票的价格。你希望利用这一知识获利,但每天只能交易一股股票。也就是说,每天你只能选择买入一股、卖出一股,或不进行任何操作。初始时你持有 0 股,且不能在没有股票时卖出。在第 #cf_span[N] 天结束时,你希望再次持有 0 股,但希望拥有尽可能多的钱。\n\n输入的第一行为一个整数 #cf_span[N] #cf_span[(2 ≤ N ≤ 3·...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ N \\in \\mathbb{Z} $ be the number of days, with $ 2 \\leq N \\leq 3 \\cdot 10^5 $.  \nLet $ P = (p_1, p_2, \\dots, p_N) $ be a sequence of stock prices, where $ p_i \\in \\mathbb{Z} $ ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments