{"raw_statement":[{"iden":"statement","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."},{"iden":"input","content":"Input 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_."},{"iden":"output","content":"Print the maximum amount of money you can end up with at the end of _N_ days."},{"iden":"examples","content":"Input\n\n9\n10 5 4 7 9 12 6 2 10\n\nOutput\n\n20\n\nInput\n\n20\n3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4\n\nOutput\n\n41"},{"iden":"note","content":"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."}],"translated_statement":[{"iden":"statement","content":"你可以完美预测某只股票在未来 #cf_span[N] 天内的价格。你希望利用这一知识获利，但每天最多只能交易一股股票。也就是说，每天你只能选择买入一股、卖出一股，或不进行任何操作。初始时你拥有零股，且在没有股票时不能卖出。在第 #cf_span[N] 天结束时，你希望再次拥有零股，但希望拥有尽可能多的钱。\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"},{"iden":"input","content":"输入的第一行为一个整数 #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]。"},{"iden":"output","content":"请输出你在第 #cf_span[N] 天结束时所能拥有的最大金额。"},{"iden":"examples","content":"输入910 5 4 7 9 12 6 2 10输出20输入203 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4输出41"},{"iden":"note","content":"在第一个例子中，以 #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]。"}],"sample_group":[],"show_order":[],"formal_statement":"**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 the 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_k = \\sum_{i=1}^k x_i $ be the cumulative number of shares owned after day $ k $, with $ s_0 = 0 $.  \n\n**Constraints**  \n1. $ s_k \\geq 0 $ for all $ k \\in \\{0, 1, \\dots, N\\} $ (cannot sell short).  \n2. $ s_N = 0 $ (end with zero shares).  \n\n**Objective**  \nMaximize the total profit:  \n$$\n\\sum_{i=1}^N x_i \\cdot p_i\n$$","simple_statement":null,"has_page_source":false}