{"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 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.\n\n## Input\n\nInput begins with an integer _N_ (2 ≤ _N_ ≤ 3·105), the number of days.\n\nFollowing 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_.\n\n## Output\n\nPrint the maximum amount of money you can end up with at the end of _N_ days.\n\n[samples]\n\n## Note\n\nIn 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.","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·105)]，表示天数。\n\n接下来是一行包含恰好 #cf_span[N] 个整数 #cf_span[p1, p2, ..., pN] #cf_span[(1 ≤ pi ≤ 106)]。第 #cf_span[i] 天一股股票的价格为 #cf_span[pi]。\n\n请输出你在第 #cf_span[N] 天结束时能拥有的最大金额。\n\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]。\n\n## Input\n\n输入的第一行为一个整数 #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]。\n\n## Output\n\n请输出你在第 #cf_span[N] 天结束时能拥有的最大金额。\n\n[samples]\n\n## Note\n\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]。","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} $ and $ 1 \\leq p_i \\leq 10^6 $ for all $ i \\in \\{1, \\dots, N\\} $.  \n\nLet $ x_i \\in \\{-1, 0, 1\\} $ denote the action on day $ i $:  \n- $ x_i = 1 $: buy one share,  \n- $ x_i = -1 $: sell one share,  \n- $ x_i = 0 $: do nothing.  \n\nLet $ s_i = \\sum_{j=1}^i x_j $ be the cumulative number of shares owned after day $ i $.  \n\n**Constraints**  \n1. $ s_0 = 0 $ (initially zero shares),  \n2. $ s_N = 0 $ (end with zero shares),  \n3. $ s_i \\geq 0 $ for all $ i \\in \\{1, \\dots, N\\} $ (cannot sell short),  \n4. $ |x_i| \\leq 1 $ for all $ i \\in \\{1, \\dots, N\\} $.  \n\n**Objective**  \nMaximize the total profit:  \n$$\n\\sum_{i=1}^N x_i \\cdot p_i\n$$","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF866D","tags":["data structures","greedy","two pointers"],"sample_group":[["9\n10 5 4 7 9 12 6 2 10","20"],["20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4","41"]],"created_at":"2026-03-03 11:00:39"}}