{"problem":{"name":"A. Functions again","description":{"content":"Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arr","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":1000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF788A"},"statements":[{"statement_type":"Markdown","content":"Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function _f_, which is defined as follows:\n\nIn the above formula, 1 ≤ _l_ < _r_ ≤ _n_ must hold, where _n_ is the size of the Main Uzhlyandian Array _a_, and |_x_| means absolute value of _x_. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of _f_ among all possible values of _l_ and _r_ for the given array _a_.\n\n## Input\n\nThe first line contains single integer _n_ (2 ≤ _n_ ≤ 105) — the size of the array _a_.\n\nThe second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (-109 ≤ _a__i_ ≤ 109) — the array elements.\n\n## Output\n\nPrint the only integer — the maximum value of _f_.\n\n[samples]\n\n## Note\n\nIn the first sample case, the optimal value of _f_ is reached on intervals \\[1, 2\\] and \\[2, 5\\].\n\nIn the second case maximal value of _f_ is reachable only on the whole array.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"乌日兰迪亚又发生了什么事……街头爆发了骚乱……著名的乌日兰迪亚超级英雄羊神谢恩和长颈鹿神斯塔斯被召来挽救局势。到达后，他们发现市民们正担忧主乌日兰迪亚函数 #cf_span[f] 的最大值，该函数定义如下：\n\n在上述公式中，必须满足 #cf_span[1 ≤ l < r ≤ n]，其中 #cf_span[n] 是主乌日兰迪亚数组 #cf_span[a] 的大小，而 #cf_span[|x|] 表示 #cf_span[x] 的绝对值。但这些英雄在学校逃掉了数学课，因此他们向你求助。请帮助他们计算在给定数组 #cf_span[a] 的所有可能的 #cf_span[l] 和 #cf_span[r] 取值下，#cf_span[f] 的最大值。\n\n第一行包含一个整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 105]) —— 数组 #cf_span[a] 的大小。\n\n第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an] (-#cf_span[109 ≤ ai ≤ 109]) —— 数组元素。\n\n请输出一个整数 —— #cf_span[f] 的最大值。\n\n在第一个样例中，#cf_span[f] 的最优值在区间 #cf_span[[1, 2]] 和 #cf_span[[2, 5]] 上取得。\n\n在第二个样例中，#cf_span[f] 的最大值仅在完整数组上可达。\n\n## Input\n\n第一行包含一个整数 #cf_span[n] (#cf_span[2 ≤ n ≤ 105]) —— 数组 #cf_span[a] 的大小。第二行包含 #cf_span[n] 个整数 #cf_span[a1, a2, ..., an] (-#cf_span[109 ≤ ai ≤ 109]) —— 数组元素。\n\n## Output\n\n请输出一个整数 —— #cf_span[f] 的最大值。\n\n[samples]\n\n## Note\n\n在第一个样例中，#cf_span[f] 的最优值在区间 #cf_span[[1, 2]] 和 #cf_span[[2, 5]] 上取得。在第二个样例中，#cf_span[f] 的最大值仅在完整数组上可达。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"Let $ a = [a_1, a_2, \\dots, a_n] $ be an array of $ n $ integers, where $ 2 \\leq n \\leq 10^5 $ and $ -10^9 \\leq a_i \\leq 10^9 $.\n\nDefine the function $ f(l, r) $ for $ 1 \\leq l < r \\leq n $ as:\n\n$$\nf(l, r) = \\left| \\sum_{i=l+1}^{r-1} a_i \\right|\n$$\n\nThat is, $ f(l, r) $ is the absolute value of the sum of elements strictly between indices $ l $ and $ r $ (excluding $ a_l $ and $ a_r $).\n\n**Objective:**  \nFind the maximum value of $ f(l, r) $ over all valid pairs $ (l, r) $ with $ 1 \\leq l < r \\leq n $.\n\n---\n\n**Equivalent Reformulation:**  \nLet $ S(i, j) = \\sum_{k=i}^{j} a_k $ denote the sum of subarray from index $ i $ to $ j $ (inclusive).  \nThen:\n\n$$\nf(l, r) = \\left| S(l+1, r-1) \\right|\n$$\n\nWe seek:\n\n$$\n\\max_{1 \\leq l < r \\leq n} \\left| S(l+1, r-1) \\right|\n$$\n\nNote: When $ r = l+1 $, the inner sum is over an empty range, so $ f(l, l+1) = 0 $.\n\nThus, the problem reduces to:  \n**Find the maximum absolute value of the sum of any contiguous subarray of length at least 1 (i.e., excluding single-element subarrays from being considered as inner sums), but note that the inner sum $ S(l+1, r-1) $ can be of length 0 (yielding 0) or more.**\n\nBut since we are taking absolute value and seeking maximum, we can rephrase:\n\nLet $ b $ be the array of all contiguous subarray sums of $ a $, excluding those of length 0 and those that correspond to $ l+1 > r-1 $ (i.e., inner sums of length ≥ 1). Then we want $ \\max |s| $ over all such $ s \\in b $.\n\nAlternatively, since $ S(l+1, r-1) = S(1, r-1) - S(1, l) $, we can define prefix sums:\n\nLet $ p_0 = 0 $, and $ p_i = \\sum_{k=1}^{i} a_k $ for $ 1 \\leq i \\leq n $.\n\nThen:\n\n$$\nS(l+1, r-1) = p_{r-1} - p_l\n$$\n\nSo:\n\n$$\nf(l, r) = |p_{r-1} - p_l|, \\quad \\text{for } 0 \\leq l < r-1 \\leq n-1\n$$\n\nLet $ i = l $, $ j = r-1 $. Then the constraint $ 1 \\leq l < r \\leq n $ becomes:\n\n- $ l \\in [0, n-2] $ (since $ l = 0 $ corresponds to $ a_1 $ being excluded, $ l = n-2 $ means $ r = n $)\n- $ j \\in [1, n-1] $\n- and $ i < j $\n\nSo we are to compute:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} |p_j - p_i|\n$$\n\nThis is the well-known problem: **Maximum absolute difference between two prefix sums with $ i < j $**.\n\nWhich is equivalent to:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} (p_j - p_i) \\quad \\text{and} \\quad \\max_{0 \\leq i < j \\leq n-1} (p_i - p_j)\n$$\n\nSo:\n\n$$\n\\max \\left( \\max_{j} p_j - \\min_{i < j} p_i, \\max_{i} p_i - \\min_{j > i} p_j \\right)\n$$\n\nBut since we are taking absolute value, it's simply:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} |p_j - p_i| = \\max(p_{\\max} - p_{\\min})\n$$\n\nwhere $ p_{\\max} = \\max_{0 \\leq k \\leq n-1} p_k $, $ p_{\\min} = \\min_{0 \\leq k \\leq n-1} p_k $, and we require that the maximum occurs for some $ i < j $, which is guaranteed if we consider the global min and max over the entire prefix array (as long as the min occurs before the max, or vice versa — but if not, we can still compute the maximum difference over all pairs).\n\nActually, the maximum absolute difference between any two prefix sums $ p_i, p_j $ with $ i < j $ is equal to:\n\n$$\n\\max\\left( \\max_{0 \\leq j \\leq n-1} p_j - \\min_{0 \\leq i < j} p_i, \\max_{0 \\leq i \\leq n-1} p_i - \\min_{i < j \\leq n-1} p_j \\right)\n$$\n\nBut a simpler and correct approach is:\n\n> The maximum value of $ |p_j - p_i| $ for $ 0 \\leq i < j \\leq n-1 $ is equal to $ \\max p_k - \\min p_k $ over $ k \\in [0, n-1] $, **provided** that the global maximum occurs after the global minimum. If not, we need to ensure $ i < j $.\n\nBut note: we are allowed to choose any $ i < j $. So the maximum difference $ p_j - p_i $ over $ i < j $ is:\n\n$$\n\\max_{j} \\left( p_j - \\min_{i < j} p_i \\right)\n$$\n\nSimilarly, the maximum of $ p_i - p_j $ for $ i < j $ is:\n\n$$\n\\max_{i} \\left( p_i - \\min_{j > i} p_j \\right)\n$$\n\nBut since we take absolute value, we can compute:\n\n$$\n\\max \\left( \\max_{0 \\leq i < j \\leq n-1} (p_j - p_i), \\max_{0 \\leq i < j \\leq n-1} (p_i - p_j) \\right)\n= \\max_{0 \\leq i, j \\leq n-1, i \\ne j} |p_i - p_j|\n$$\n\nAnd since the maximum absolute difference between any two elements in a set is $ \\max - \\min $, we can compute:\n\nLet $ P = \\{ p_0, p_1, \\dots, p_{n-1} \\} $\n\nThen:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} |p_j - p_i| = \\max(P) - \\min(P)\n$$\n\n**Why?** Because if the global maximum occurs at index $ j $ and global minimum at index $ i $, and if $ i < j $, then we are done. If $ i > j $, then we can still find some pair $ i' < j' $ such that $ p_{j'} - p_{i'} = \\max(P) - \\min(P) $, because the maximum difference in absolute value over all unordered pairs is always $ \\max - \\min $, and since we are allowed to pick any $ i < j $, as long as $ \\max $ and $ \\min $ are in the prefix array, and we can always find a pair where the larger one comes after the smaller one (or vice versa for negative), but the absolute value makes it symmetric.\n\nActually, **this is not always true** if the global min occurs after the global max. For example: $ p = [10, 1, 5] $, then $ \\max = 10 $, $ \\min = 1 $, but $ \\max - \\min = 9 $, but if the max is at index 0 and min at index 1, then we cannot use $ p_0 - p_1 $ as $ i < j $, but we can use $ p_2 - p_1 = 4 $, or $ p_0 - p_1 = 9 $, but $ i=0, j=1 $, and $ i < j $, so we **can** use it.\n\nWait — $ i $ and $ j $ are indices in the prefix array, and we require $ i < j $, meaning the index of the first prefix sum is less than the index of the second.\n\nSo if $ p_i = \\min $ at index $ i $, $ p_j = \\max $ at index $ j $, and $ i < j $, then $ p_j - p_i = \\max - \\min $ is valid.\n\nIf $ i > j $, then $ p_i - p_j = \\max - \\min $, but $ i > j $, so we cannot use that pair because we require $ i < j $ in the pair $ (i, j) $ for $ p_j - p_i $.\n\nWait — in our expression $ f(l, r) = |p_{r-1} - p_l| $, and we have $ l < r-1 $, so $ i = l $, $ j = r-1 $, and $ i < j $. So we require the index of the first prefix to be less than the index of the second.\n\nTherefore, the maximum value of $ p_j - p_i $ for $ i < j $ is:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} (p_j - p_i)\n$$\n\nAnd the maximum value of $ p_i - p_j $ for $ i < j $ is:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} (p_i - p_j) = - \\min_{0 \\leq i < j \\leq n-1} (p_j - p_i)\n$$\n\nBut we take absolute value, so:\n\n$$\n\\max_{0 \\leq i < j \\leq n-1} |p_j - p_i| = \\max\\left( \\max_{i<j} (p_j - p_i), \\max_{i<j} (p_i - p_j) \\right)\n= \\max\\left( \\max_{i<j} (p_j - p_i), \\max_{i<j} (p_i - p_j) \\right)\n$$\n\nBut note: $ \\max_{i<j} (p_i - p_j) = \\max_{i<j} -(p_j - p_i) = - \\min_{i<j} (p_j - p_i) $\n\nSo the maximum absolute value is the maximum of:\n\n- The maximum positive difference $ \\max_{i<j} (p_j - p_i) $\n- The maximum negative difference's absolute value: $ \\max_{i<j} (p_i - p_j) = \\max_{i<j} (-(p_j - p_i)) $\n\nBut this is equal to:\n\n$$\n\\max\\left( \\max_{i<j} (p_j - p_i), \\max_{i<j} (p_i - p_j) \\right) = \\max\\left( \\max_{i<j} (p_j - p_i), \\max_{i<j} (p_i - p_j) \\right)\n$$\n\nAnd since $ \\max_{i<j} (p_i - p_j) = \\max_{i<j} |p_i - p_j| $ when $ p_i > p_j $, we can compute both.\n\nBut there is a standard result:  \n> The maximum absolute difference between any two elements in a sequence, with the constraint that the first element comes before the second, is equal to the maximum difference between any two prefix sums where the later one is subtracted by the earlier one, and we take the maximum over all such pairs.\n\nActually, the maximum value of $ |p_j - p_i| $ for $ i < j $ is equal to:\n\n$$\n\\max\\left( \\max_{0 \\leq j \\leq n-1} p_j - \\min_{0 \\leq i < j} p_i, \\max_{0 \\leq i \\leq n-1} p_i - \\min_{i < j \\leq n-1} p_j \\right)\n$$\n\nBut a simpler and correct method is:\n\nLet $ m = \\min_{0 \\leq k \\leq n-1} p_k $, $ M = \\max_{0 \\leq k \\leq n-1} p_k $\n\nThen the maximum absolute difference over all $ i < j $ is not necessarily $ M - m $, because if $ m $ occurs after $ M $, then $ M - m $ cannot be achieved with $ i < j $.\n\nBut we can compute:\n\n- $ \\text{max\\_diff} = 0 $\n- $ \\text{min\\_so\\_far} = p_0 $\n- For $ j = 1 $ to $ n-1 $:\n  - $ \\text{max\\_diff} = \\max(\\text{max\\_diff}, p_j - \\text{min\\_so\\_far}) $\n  - $ \\text{min\\_so\\_far} = \\min(\\text{min\\_so\\_far}, p_j) $\n\nThen the maximum of $ p_j - p_i $ for $ i < j $ is this `max_diff`.\n\nSimilarly, to get the maximum of $ p_i - p_j $ for $ i < j $, we can reverse: compute maximum of $ p_i - p_j $ for $ i < j $, which is equivalent to computing $ \\max_{i<j} (p_i - p_j) = \\max_{i<j} (-(p_j - p_i)) $, so we can compute:\n\n- $ \\text{max\\_so\\_far} = p_0 $\n- For $ j = 1 $ to $ n-1 $:\n  - $ \\text{max\\_diff\\_neg} = \\max(\\text{max\\_diff\\_neg}, \\text{max\\_so\\_far} - p_j) $\n  - $ \\text{max\\_so\\_far} = \\max(\\text{max\\_so\\_far}, p_j) $\n\nThen the answer is:\n\n$$\n\\max(\\text{max\\_diff}, \\text{max\\_diff\\_neg})\n$$\n\nBut note: $ \\text{max\\_diff\\_neg} = \\max_{i<j} (p_i - p_j) $, and since we take absolute value, we want the maximum of the absolute values, so:\n\n$$\n\\boxed{ \\max\\left( \\max_{0 \\leq i < j \\leq n-1} (p_j - p_i), \\max_{0 \\leq i < j \\leq n-1} (p_i - p_j) \\right) }\n$$\n\nWhich is equivalent to:\n\n$$\n\\boxed{ \\max_{0 \\leq i < j \\leq n-1} |p_j - p_i| }\n$$\n\nAnd this can be computed in $ O(n) $ time by:\n\n1. Compute prefix sums $ p_0, p_1, \\dots, p_{n-1} $, where $ p_0 = 0 $, $ p_i = p_{i-1} + a_i $ for $ i = 1 $ to $ n-1 $.\n2. Compute:\n   - $ \\text{pos\\_max} = \\max_{i<j} (p_j - p_i) $ via scanning left to right, tracking min prefix so far.\n   - $ \\text{neg\\_max} = \\max_{i<j} (p_i - p_j) $ via scanning left to right, tracking max prefix so far.\n\nThen answer = $ \\max(\\text{pos\\_max}, \\text{neg\\_max}) $\n\nBut note: $ \\text{neg\\_max} = \\max_{i<j} (p_i - p_j) = \\max_{i<j} |p_i - p_j| $ when $ p_i > p_j $, so we are covering both cases.\n\nAlternatively, since $ \\max_{i<j} |p_j - p_i| = \\max( \\max_{i<j} (p_j - p_i), \\max_{i<j} (p_i - p_j) ) $, and both can be computed in one pass each.\n\nHowever, note that $ \\max_{i<j} |p_j - p_i| $ is equal to the maximum of:\n\n- $ \\max p_k - \\min p_k $, if the min occurs before the max\n- or the maximum gap where a high value is followed by a low value\n\nBut the standard solution to \"maximum difference between two elements with smaller index first\" is to compute:\n\n$$\n\\max_{j} (p_j - \\min_{i < j} p_i)\n$$\n\nThis gives the maximum positive difference.\n\nSimilarly, to get the maximum negative difference in absolute value, we compute:\n\n$$\n\\max_{j} (\\max_{i < j} p_i - p_j)\n$$\n\nSo final algorithm:\n\nLet $ p[0] = 0 $\n\nFor $ i = 1 $ to $ n-1 $:  \n$ p[i] = p[i-1] + a[i] $\n\nThen:\n\n- $ \\min\\_prev = p[0] $\n- $ \\max\\_pos = 0 $\n- For $ j = 1 $ to $ n-1 $:\n  - $ \\max\\_pos = \\max(\\max\\_pos, p[j] - \\min\\_prev) $\n  - $ \\min\\_prev = \\min(\\min\\_prev, p[j]) $\n\n- $ \\max\\_prev = p[0] $\n- $ \\max\\_neg = 0 $\n- For $ j = 1 $ to $ n-1 $:\n  - $ \\max\\_neg = \\max(\\max\\_neg, \\max\\_prev - p[j]) $\n  - $ \\max\\_prev = \\max(\\max\\_prev, p[j]) $\n\nAnswer: $ \\max(\\max\\_pos, \\max\\_neg) $\n\nBut note: $ \\max\\_neg $ is the maximum of $ p_i - p_j $ for $ i < j $, which is the maximum absolute value when the prefix decreases.\n\nThis is correct.\n\nBut observe: since we are taking absolute value, and the two cases are symmetric, we can also note that:\n\n$$\n\\max_{i<j} |p_j - p_i| = \\max_{i,j \\in [0, n-1], i \\ne j} |p_i - p_j|\n$$\n\nbecause if the global max and min occur in any order, and since we are allowed to pick any $ i < j $, then the maximum absolute difference over all unordered pairs is $ \\max p_k - \\min p_k $, and if the min occurs after the max, then we cannot use that pair, but we can still achieve the same value if there exists some pair where the difference is $ \\max - \\min $, but only if the max occurs before the min? No.\n\nCounterexample:  \nLet $ p = [5, 1, 3] $  \nThen $ \\max p = 5 $, $ \\min p = 1 $, difference = 4  \nBut $ p_0 = 5 $, $ p_1 = 1 $, $ i=0, j=1 $: $ |p_1 - p_0| = 4 $, and $ i < j $, so it's allowed.  \nSo even if the min comes after the max, as long as we are taking absolute value and the indices satisfy $ i < j $, we can still get $ |p_j - p_i| = \\max - \\min $ if the max is at an earlier index and min at a later one.\n\nWait — in this case, $ p_0 = 5 $, $ p_1 = 1 $, then $ |p_1 - p_0| = 4 = 5 - 1 $, which is $ \\max - \\min $, and $ i=0 < j=1 $, so it's valid.\n\nAnother example: $ p = [1, 5, 3] $  \n$ \\max = 5 $, $ \\min = 1 $, difference = 4  \n$ p_0 = 1 $, $ p_1 = 5 $, $ |p_1 - p_0| = 4 $, $ i=0 < j=1 $, valid.\n\nAnother: $ p = [3, 1, 5] $  \n$ \\max = 5 $, $ \\min = 1 $, difference = 4  \nCan we get 4?  \n$ |p_2 - p_1| = |5 - 1| = 4 $, and $ i=1 < j=2 $, valid.\n\nSo in all cases, as long as the global maximum and global minimum appear somewhere in the prefix array, then the pair consisting of the index of the global min and global max will have $ |p_j - p_i| = \\max - \\min $, and since the two indices are distinct, one must come before the other. So we can always find a pair $ i < j $ such that $ |p_j - p_i| = \\max p_k - \\min p_k $.\n\nTherefore:\n\n$$\n\\boxed{ \\max_{0 \\leq i < j \\leq n-1} |p_j - p_i| = \\max_{0 \\leq k \\leq n-1} p_k - \\min_{0 \\leq k \\leq n-1} p_k }\n$$\n\n**Final Answer:**  \nCompute prefix sums $ p_0 = 0, p_1 = a_1, p_2 = a_1 + a_2, \\dots, p_{n-1} = \\sum_{i=1}^{n-1} a_i $  \n(Note: $ p_i = \\sum_{k=1}^{i} a_k $ for $ i \\geq 1 $, and $ p_0 = 0 $)\n\nThen:\n\n$$\n\\boxed{ \\max_{0 \\leq i \\leq n-1} p_i - \\min_{0 \\leq i \\leq n-1} p_i }\n$$\n\nThis is the maximum value of $ f(l, r) $.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF788A","tags":["dp","two pointers"],"sample_group":[["5\n1 4 2 3 1","3"],["4\n1 5 4 7","6"]],"created_at":"2026-03-03 11:00:39"}}