Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } long long b[n - 1], c[n - 1]; for (long long i = 0; i < n - 1; i++) { long long val = abs(a[i] - a[i + 1]); if (i % 2) { b[i] = -val; c[i] = val; } else { b[i] = val; c[i] = -val; } } long long sum[n - 1], sum1[n - 1]; sum[0] = b[0]; sum1[0] = c[0]; for (long long i = 1; i < n - 1; i++) { sum[i] = sum[i - 1] + b[i]; sum1[i] = sum1[i - 1] + c[i]; } long long ans = 0, minn = 0; for (long long i = 0; i < n - 1; i++) { ans = max(ans, sum[i] - minn); minn = min(minn, sum[i]); } minn = 0; for (long long i = 0; i < n - 1; i++) { ans = max(ans, sum1[i] - minn); minn = min(minn, sum1[i]); } cout << ans; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Ribhav */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CFunctionsAgain solver = new CFunctionsAgain(); solver.solve(1, in, out); out.close(); } static class CFunctionsAgain { public void solve(int testNumber, FastReader s, PrintWriter out) { int n = s.nextInt(); long[] arr = s.nextLongArray(n); long ans = 0L; long mul = 1L; long fin = 0L; for (int i = 0; i < n - 1; i++) { ans += ((Math.abs(arr[i] - arr[i + 1])) * mul); mul *= -1; ans = Math.max(ans, 0); fin = Math.max(fin, ans); } ans = 0L; mul = 1L; for (int i = 1; i < n - 1; i++) { ans += ((Math.abs(arr[i] - arr[i + 1])) * mul); mul *= -1; ans = Math.max(ans, 0); fin = Math.max(fin, ans); } out.println(fin); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; ++i) array[i] = nextLong(); return array; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; cin >> n; vector<long long int> a(n), b; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = 1; i < n; i++) b.push_back(abs(a[i] - a[i - 1])); n = b.size(); long long int dp[n][2]; dp[n - 1][0] = b[n - 1]; dp[n - 1][1] = -b[n - 1]; long long int ans = -(long long int)(1e16); for (long long int i = n - 1 - 1; i >= 0; i--) { dp[i][0] = max(b[i], b[i] + dp[i + 1][1]); dp[i][1] = max(-b[i], -b[i] + dp[i + 1][0]); } for (long long int i = 0; i < n; i++) { ans = max(ans, dp[i][0]); } cout << ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; t = 1; while (t--) { solve(); } return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int N; long long a[100000]; void input() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%I64d", &a[i]); } } long long b[100000]; long long c[100000]; long long maxSumSubarr() { long long sum = c[0]; long long max_sum = sum; for (int i = 1; i < N - 1; i++) { sum += c[i]; if (sum > max_sum) max_sum = sum; if (sum < c[i]) sum = c[i]; } if (sum > max_sum) max_sum = sum; return max_sum; } void solve() { for (int i = 1; i < N; i++) { b[i - 1] = abs(a[i - 1] - a[i]); } int sign = 1; for (int i = 0; i < N - 1; i++) { c[i] = b[i] * sign; sign *= -1; } long long ans1 = maxSumSubarr(); sign = -1; for (int i = 0; i < N - 1; i++) { c[i] = b[i] * sign; sign *= -1; } long long ans2 = maxSumSubarr(); printf("%I64d\n", max(ans1, ans2)); } int main() { input(); solve(); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.util.*; import java.lang.*; import java.io.*; public class Prac { public static long max(long a,long b){ if(a>b) return a; else return b; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int i; long arr[]=new long[n+1]; for(i=1;i<=n;i++) arr[i]=sc.nextInt(); long dp[]=new long[n+1]; for(i=1;i<n;i++){ long a=Math.abs(arr[i]-arr[i+1]); dp[i]=a; } long dpevenpos[]=new long[n+1]; long dpoddpos[]=new long[n+1]; for(i=1;i<=n;i++){ if(i%2==0){ dpevenpos[i]=dp[i]; dpoddpos[i]=-dp[i]; } else{ dpevenpos[i]=-dp[i]; dpoddpos[i]=dp[i]; } } long max_current_of_even=dpevenpos[1]; long max_so_far_of_even=dpevenpos[1]; long max_current_of_odd=dpoddpos[1]; long max_so_far_of_odd=dpoddpos[1]; for(i=2;i<=n;i++){ max_current_of_even=max(dpevenpos[i],max_current_of_even+dpevenpos[i]); max_so_far_of_even=max(max_so_far_of_even,max_current_of_even); max_current_of_odd=max(dpoddpos[i],max_current_of_odd+dpoddpos[i]); max_so_far_of_odd=max(max_so_far_of_odd,max_current_of_odd); } System.out.println(max(max_so_far_of_even,max_so_far_of_odd)); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { static class Pair implements Comparable<Pair> { int v; long d; Pair(int v, long d) { this.v = v; this.d = d; } @Override public int compareTo(Pair o) { return Long.compare(d, o.d); } @Override public String toString() { return "(" + v + ", " + d + ")"; } } int MOD = 1000 * 1000 * 1000 + 7; class Edge implements Comparable<Edge> { int to; long w; public Edge(int to, long w) { this.to = to; this.w = w; } @Override public int compareTo(Edge o) { return Long.compare(w, o.w); } } private ArrayList<Integer>[] G; private long oo = 100_000_000_000_000L; private void solve() { int n = in.nextInt(); int[] tab = new int[n]; for (int i = 0; i < n; i++) { tab[i] = in.nextInt(); } long[] b = new long[n - 1]; for (int i = 0; i < n - 1; i++) { b[i] = Math.abs(tab[i + 1] - tab[i]); } long maxPos = 0; long maxNeg = 0; long best = 0; for (int i = n - 2; i >= 0; i--) { long curr = b[i]; long oldMaxPos = maxPos; long oldMaxNeg = maxNeg; maxPos = Math.max(curr, curr + oldMaxNeg); maxNeg = Math.max(-curr, -curr + oldMaxPos); best = Math.max(maxPos, best); } out.print(best); } /* * * */ // -------------------------------------------------------- private FastScanner in; private PrintWriter out; void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { new Main().runIO(); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
def max_sum(a): s, ans = 0, 0 for i in a: s += i ans = max(s, ans) s = max(0, s) return ans n = int(input()) *a, = map(int, input().split()) a = [abs(a[i] - a[i + 1]) for i in range(n - 1)] b = [-1 * a[i] if i & 1 else a[i] for i in range(n - 1)] a = [a[i] if i & 1 else -1 * a[i] for i in range(n - 1)] print(max(max_sum(a), max_sum(b)))
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long was[211111][2]; int a[211111]; int n; long long f(int x, int o) { if (was[x][o]) return was[x][o] - 1; long long ret = 0; if (x + 1 < n) { long long val = f(x + 1, o ^ 1); if (o == 0) val += abs(a[x + 1] - a[x]); else val -= abs(a[x + 1] - a[x]); ret = max(ret, val); } was[x][o] = ret + 1; return ret; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 0; i < n; i++) ans = max(ans, f(i, 0)); cout << ans << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) arr = list(map(int,input().split())) even = [0 for i in range(n)] odd = [0 for i in range(n)] diff = [] for i in range(n-1): diff.append(abs(arr[i]-arr[i+1])) for i in range(len(diff)): even[i] = max(diff[i],diff[i]+odd[i-1]) odd[i] = max(-diff[i],-diff[i]+even[i-1]) print(max(max(even),max(odd)))
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
// Don't place your source in a package import java.util.*; import java.lang.*; import java.io.*; import java.math.*; // Please name your class Main public class Main { //static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); /*static int read() throws IOException { in.nextToken(); return (int) in.nval; } static String readString() throws IOException { in.nextToken(); return in.sval; }*/ static Scanner in = new Scanner(System.in); public static void main (String[] args) throws java.lang.Exception { //InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int T=1; for(int t=0;t<T;t++){ int n=Int(); long A[]=new long[n]; for(int i=0;i<n;i++){ A[i]=Long(); } Solution sol=new Solution(); sol.solution(A); } out.flush(); } public static long Long(){ return in.nextLong(); } public static int Int(){ return in.nextInt(); } public static String Str(){ return in.next(); } } class Solution{ //constant variable final int MAX=Integer.MAX_VALUE; final int MIN=Integer.MIN_VALUE; //Set<Integer>adjecent[]; ////////////////////////////// public void fastprint(PrintWriter out,String s){ out.print(s); } public void fastprintln(PrintWriter out,String s){ out.println(s); } public void solution(long A[]){ long res=MIN; long dif[]=new long[A.length-1]; for(int i=0;i<A.length-1;i++){ dif[i]=Math.abs(A[i+1]-A[i]); } long sum1=0,sum2=0; PriorityQueue<Long>odd=new PriorityQueue<>(); PriorityQueue<Long>even=new PriorityQueue<>(); //odd.add(0l);even.add(0l); //print1(dif); for(int i=0;i<dif.length;i++){ long cur=0; res=Math.max(res,dif[i]); if(i%2==0){ sum1+=dif[i]; cur=sum1-sum2; } else{ sum2+=dif[i]; cur=sum2-sum1; } if(i%2==0){ if(i==0){ even.add(0l); } else{ res=Math.max(res,cur-even.peek()); even.add(cur-dif[i]); } } else{ if(i==1){ odd.add(-dif[0]); }else{ res=Math.max(res,cur-odd.peek()); odd.add(cur-dif[i]); } } //msg(i+" "+res); //System.out.println(odd); //System.out.println(even); } msg(res+""); } /*class LCA{ int p1,p2; boolean visit[]; int res=-1; boolean good=false; public LCA(int p1,int p2){ this.p1=p1;this.p2=p2; visit=new boolean[adjecent.length]; } public boolean dfs(int root){ visit[root]=true; List<Integer>next=adjecent[root]; boolean ans=false; if(root==p1||root==p2){ ans=true; } int cnt=0; for(int c:next){ if(visit[c])continue; boolean v=dfs(c); ans|=v; if(v)cnt++; } if(cnt==2){ if(!good){ good=true; res=root; } } else if(cnt==1){ if(!good&&(root==p1||root==p2)){ good=true; res=root; } } return ans; } }*/ /*public void delete(Dnode node){ Dnode pre=node.pre; Dnode next=node.next; pre.next=next; next.pre=pre; map.remove(node.v); } public void add(int v){ Dnode node=new Dnode(v); Dnode pre=tail.pre; pre.next=node; node.pre=pre; node.next=tail; tail.pre=node; map.put(v,node); } class Dnode{ Dnode next=null; Dnode pre=null; int v; public Dnode(int v){ this.v=v; } }*/ public String tobin(int i){ return Integer.toBinaryString(i); } public void reverse(int A[]){ List<Integer>l=new ArrayList<>(); for(int i:A)l.add(i); Collections.reverse(l); for(int i=0;i<A.length;i++){ A[i]=l.get(i); } } public void sort(int A[]){ Arrays.sort(A); } public void swap(int A[],int l,int r){ int t=A[l]; A[l]=A[r]; A[r]=t; } public long C(long fact[],int i,int j){ // C(20,3)=20!/(17!*3!) // take a/b where a=20! b=17!*3! if(j>i)return 1; if(j<=0)return 1; long mod=998244353; long a=fact[i]; long b=((fact[i-j]%mod)*(fact[j]%mod))%mod; BigInteger B= BigInteger.valueOf(b); long binverse=B.modInverse(BigInteger.valueOf(mod)).longValue(); return ((a)*(binverse%mod))%mod; } public long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } //map operation public void put(Map<Integer,Integer>map,int i){ if(!map.containsKey(i))map.put(i,0); map.put(i,map.get(i)+1); } public void delete(Map<Integer,Integer>map,int i){ map.put(i,map.get(i)-1); if(map.get(i)==0)map.remove(i); } /*public void tarjan(int p,int r){ if(cut)return; List<Integer>childs=adjecent[r]; dis[r]=low[r]=time; time++; //core for tarjan int son=0; for(int c:childs){ if(ban==c||c==p)continue; if(dis[c]==-1){ son++; tarjan(r,c); low[r]=Math.min(low[r],low[c]); if((r==root&&son>1)||(low[c]>=dis[r]&&r!=root)){ cut=true; return; } }else{ if(c!=p){ low[r]=Math.min(low[r],dis[c]); } } } }*/ //helper function I would use public void remove(Map<Integer,Integer>map,int i){ map.put(i,map.get(i)-1); if(map.get(i)==0)map.remove(i); } public void ascii(String s){ for(char c:s.toCharArray()){ System.out.print((c-'a')+" "); } msg(""); } public int flip(int i){ if(i==0)return 1; else return 0; } public boolean[] primes(int n){ boolean A[]=new boolean[n+1]; for(int i=2;i<=n;i++){ if(A[i]==false){ for(int j=i+i;j<=n;j+=i){ A[j]=true; } } } return A; } public void msg(String s){ System.out.println(s); } public void msg1(String s){ System.out.print(s); } public int[] kmpPre(String p){ int pre[]=new int[p.length()]; int l=0,r=1; while(r<p.length()){ if(p.charAt(l)==p.charAt(r)){ pre[r]=l+1; l++;r++; }else{ if(l==0)r++; else l=pre[l-1]; } } return pre; } public boolean isP(String s){ int l=0,r=s.length()-1; while(l<r){ if(s.charAt(l)!=s.charAt(r))return false; l++;r--; } return true; } public int find(int nums[],int x){//union find => find method if(nums[x]==x)return x; int root=find(nums,nums[x]); nums[x]=root; return root; } public int[] copy1(int A[]){ int a[]=new int[A.length]; for(int i=0;i<a.length;i++)a[i]=A[i]; return a; } public void print1(long A[]){ for(long i:A)System.out.print(i+" "); System.out.println(); } public void print2(int A[][]){ for(int i=0;i<A.length;i++){ for(int j=0;j<A[0].length;j++){ System.out.print(A[i][j]+" "); }System.out.println(); } } public int min(int a,int b){ return Math.min(a,b); } public int[][] matrixdp(int[][] grid) { if(grid.length==0)return new int[][]{}; int res[][]=new int[grid.length][grid[0].length]; for(int i=0;i<grid.length;i++){ for(int j=0;j<grid[0].length;j++){ res[i][j]=grid[i][j]+get(res,i-1,j)+get(res,i,j-1)-get(res,i-1,j-1); } } return res; } public int get(int grid[][],int i,int j){ if(i<0||j<0||i>=grid.length||j>=grid[0].length)return 0; return grid[i][j]; } public int[] suffixArray(String s){ int n=s.length(); Suffix A[]=new Suffix[n]; for(int i=0;i<n;i++){ A[i]=new Suffix(i,s.charAt(i)-'a',0); } for(int i=0;i<n;i++){ if(i==n-1){ A[i].next=-1; }else{ A[i].next=A[i+1].rank; } } Arrays.sort(A); for(int len=4;len<A.length*2;len<<=1){ int in[]=new int[A.length]; int rank=0; int pre=A[0].rank; A[0].rank=rank; in[A[0].index]=0; for(int i=1;i<A.length;i++){//rank for the first two letter if(A[i].rank==pre&&A[i].next==A[i-1].next){ pre=A[i].rank; A[i].rank=rank; }else{ pre=A[i].rank; A[i].rank=++rank; } in[A[i].index]=i; } for(int i=0;i<A.length;i++){ int next=A[i].index+len/2; if(next>=A.length){ A[i].next=-1; }else{ A[i].next=A[in[next]].rank; } } Arrays.sort(A); } int su[]=new int[A.length]; for(int i=0;i<su.length;i++){ su[i]=A[i].index; } return su; } } //suffix array Struct class Suffix implements Comparable<Suffix>{ int index; int rank; int next; public Suffix(int i,int rank,int next){ this.index=i; this.rank=rank; this.next=next; } @Override public int compareTo(Suffix other) { if(this.rank==other.rank){ return this.next-other.next; } return this.rank-other.rank; } public String toString(){ return this.index+" "+this.rank+" "+this.next+" "; } } class Wrapper implements Comparable<Wrapper>{ int spf;int cnt; public Wrapper(int spf,int cnt){ this.spf=spf; this.cnt=cnt; } @Override public int compareTo(Wrapper other) { return this.spf-other.spf; } } class Node{//what the range would be for that particular node boolean state=false; int l=0,r=0; int ll=0,rr=0; public Node(boolean state){ this.state=state; } } class Seg1{ int A[]; public Seg1(int A[]){ this.A=A; } public void update(int left,int right,int val,int s,int e,int id){ if(left<0||right<0||left>right)return; if(left==s&&right==e){ A[id]+=val; return; } int mid=s+(e-s)/2; //[s,mid] [mid+1,e] if(left>=mid+1){ update(left,right,val,mid+1,e,id*2+2); }else if(right<=mid){ update(left,right,val,s,mid,id*2+1); }else{ update(left,mid,val,s,mid,id*2+1); update(mid+1,right,val,mid+1,e,id*2+2); } } public int query(int i,int add,int s,int e,int id){ if(s==e&&i==s){ return A[id]+add; } int mid=s+(e-s)/2; //[s,mid] [mid+1,e] if(i>=mid+1){ return query(i,A[id]+add,mid+1,e,id*2+2); }else{ return query(i,A[id]+add,s,mid,id*2+1); } } } class MaxFlow{ public static List<Edge>[] createGraph(int nodes) { List<Edge>[] graph = new List[nodes]; for (int i = 0; i < nodes; i++) graph[i] = new ArrayList<>(); return graph; } public static void addEdge(List<Edge>[] graph, int s, int t, int cap) { graph[s].add(new Edge(t, graph[t].size(), cap)); graph[t].add(new Edge(s, graph[s].size() - 1, 0)); } static boolean dinicBfs(List<Edge>[] graph, int src, int dest, int[] dist) { Arrays.fill(dist, -1); dist[src] = 0; int[] Q = new int[graph.length]; int sizeQ = 0; Q[sizeQ++] = src; for (int i = 0; i < sizeQ; i++) { int u = Q[i]; for (Edge e : graph[u]) { if (dist[e.t] < 0 && e.f < e.cap) { dist[e.t] = dist[u] + 1; Q[sizeQ++] = e.t; } } } return dist[dest] >= 0; } static int dinicDfs(List<Edge>[] graph, int[] ptr, int[] dist, int dest, int u, int f) { if (u == dest) return f; for (; ptr[u] < graph[u].size(); ++ptr[u]) { Edge e = graph[u].get(ptr[u]); if (dist[e.t] == dist[u] + 1 && e.f < e.cap) { int df = dinicDfs(graph, ptr, dist, dest, e.t, Math.min(f, e.cap - e.f)); if (df > 0) { e.f += df; graph[e.t].get(e.rev).f -= df; return df; } } } return 0; } public static int maxFlow(List<Edge>[] graph, int src, int dest) { int flow = 0; int[] dist = new int[graph.length]; while (dinicBfs(graph, src, dest, dist)) { int[] ptr = new int[graph.length]; while (true) { int df = dinicDfs(graph, ptr, dist, dest, src, Integer.MAX_VALUE); if (df == 0) break; flow += df; } } return flow; } } class Edge { int t, rev, cap, f; public Edge(int t, int rev, int cap) { this.t = t; this.rev = rev; this.cap = cap; } } class Seg{ int l,r; int min=Integer.MAX_VALUE; Seg left=null,right=null; int tree[]; public Seg(int l,int r){ this.l=l; this.r=r; if(l!=r){ int mid=l+(r-l)/2; if(l<=mid)left=new Seg(l,mid); if(r>=mid+1)right=new Seg(mid+1,r); if(left!=null)min=Math.min(left.min,min); if(right!=null)min=Math.min(right.min,min); }else{ min=tree[l]; } } public int query(int s,int e){ if(l==s&&r==e){ return min; } int mid=l+(r-l)/2; //left : to mid-1, if(e<=mid){ return left.query(s,e); } else if(s>=mid+1){ return right.query(s,e); }else{ return Math.min(left.query(s,mid),right.query(mid+1,e)); } } public void update(int index){ if(l==r){ min=tree[l]; return; } int mid=l+(r-l)/2; if(index<=mid){ left.update(index); }else{ right.update(index); } this.min=Math.min(left.min,right.min); } } class Fenwick { int tree[];//1-index based int A[]; int arr[]; public Fenwick(int[] A) { this.A=A; arr=new int[A.length]; tree=new int[A.length+1]; int sum=0; for(int i=0;i<A.length;i++){ update(i,A[i]); } } public void update(int i, int val) { arr[i]+=val; i++; while(i<tree.length){ tree[i]+=val; i+=(i&-i); } } public int sumRange(int i, int j) { return pre(j+1)-pre(i); } public int pre(int i){ int sum=0; while(i>0){ sum+=tree[i]; i-=(i&-i); } return sum; } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n=int(input()) a=list(map(int,input().split())) da,p=[],1 for i in range(n-1): da.append(p*abs(a[i]-a[i+1]));p*=-1 m1,m2,s1,s2=0,0,0,0 for x in da: s1+=x if s1<0: s1=0 s2-=x if s2<0: s2=0 m1=max(m1,s1);m2=max(m2,s2) print(max(m1,m2)) # Made By Mostafa_Khaled
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) a = list(map(int, input().split())) first = [-abs(a[i]-a[i-1]) if (i+1)%2 else abs(a[i]-a[i-1]) for i in range(1, n)] second = [abs(a[i]-a[i-1]) if (i+1)%2 else -abs(a[i]-a[i-1]) for i in range(2, n)] mx = -10**18 cur = 0 for i in range(len(first)): cur = first[i] + max(cur, 0) if i%2 == 0: mx = max(mx, cur) cur = 0 for i in range(len(second)): cur = second[i] + max(cur, 0) if i%2 == 0: mx = max(mx, cur) print(mx)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const int inf = (int)1e9 + 10; const int mod = (int)1e9 + 7; const double eps = 1e-7; const double pi = 3.14159265359; const int maxLen = (int)1e5 + 2; int A[maxLen]; long long D[maxLen]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); D[1] = abs(A[1] - A[2]); for (int i = 2; i < n; i++) { D[i] = D[i - 1]; if (i % 2) D[i] += abs(A[i] - A[i + 1]); else D[i] -= abs(A[i] - A[i + 1]); } long long ans = D[1]; long long minSum = 0; long long maxSum = 0; for (int i = 1; i < n; i++) { if (i % 2) ans = max(ans, D[i] - minSum); else ans = max(ans, maxSum - D[i]); minSum = min(D[i], minSum); maxSum = max(D[i], maxSum); } printf("%lld", ans); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, i, s = 0, m = 0, x = 0, y = 0, l = 0; cin >> n; vector<long long> a(n); for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n - 1; i++) a[i] = abs(a[i] - a[i + 1]); for (i = 0; i < n - 1; i++) { if (i % 2 == 0) x += a[i]; else x -= a[i]; if (a[i] > x && i % 2 == 0) x = a[i]; if (i % 2) y += a[i]; else if (i % 2 == 0 && i > 0) y -= a[i]; if (i % 2 && a[i] > y) y = a[i]; if (x > s) s = x; if (y > s) s = y; } cout << s << endl; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.*; import java.util.*; public class test { int INF = (int)1e9; long MOD = 1000000007; void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); long[] a = new long[n]; for(int i=0; i<n; i++) { a[i] = in.nextInt(); } long[] d = new long[n+10]; for(int i=0; i<n-1; i++) { d[i] = Math.abs(a[i]-a[i+1]); } long[] dp = new long[n+10]; long rs = -1; for(int i=n-2; i>=0; i--) { dp[i] = d[i] + Math.max(0, dp[i+2]-d[i+1]); rs = Math.max(rs, dp[i]); } out.println(rs); } public static void main(String[] args) throws IOException { if(args.length>0 && args[0].equalsIgnoreCase("d")) { DEBUG_FLAG = true; } InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); int t = 1;//in.nextInt(); long start = System.nanoTime(); while(t-- >0) { new test().solve(in, out); } long end = System.nanoTime(); debug("\nTime: " + (end-start)/1e6 + " \n\n"); out.close(); } static boolean DEBUG_FLAG = false; static void debug(String s) { if(DEBUG_FLAG) System.out.print(s); } public static void show(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { static BufferedReader br; static StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class C789 { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); StringTokenizer tok = new StringTokenizer(in.readLine()); int[] vals = new int[n]; vals[0] = Integer.parseInt(tok.nextToken()); long pSum = 0; long maxSum = Integer.MIN_VALUE; long minSum = Integer.MAX_VALUE; for (int i = 1; i < n; i++) { vals[i] = Integer.parseInt(tok.nextToken()); long diff = vals[i] - vals[i-1]; if (diff < 0) { diff *= -1; } if (i%2==1) { pSum += diff; } else { pSum -= diff; } if (pSum > maxSum) { maxSum = pSum; } if (pSum < minSum) { minSum = pSum; } } if (minSum < 0) { System.out.println(maxSum-minSum); } else { System.out.println(maxSum); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; public class CFD { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; final long MOD = 1000L * 1000L * 1000L + 7; int[] dx = {0, -1, 0, 1}; int[] dy = {1, 0, -1, 0}; void solve() throws IOException { int n = nextInt(); long[] arr = nextLongArr(n); long[] diff = new long[n - 1]; for (int i = 0; i < n - 1; i++) { diff[i] = Math.abs(arr[i] - arr[i + 1]); } long[] cp1 = Arrays.copyOf(diff, diff.length); long[] cp2 = Arrays.copyOf(diff, diff.length); for (int i = 0; i < cp1.length; i++) { if (i % 2 == 0) { cp1[i] = -cp1[i]; } else { cp2[i] = -cp2[i]; } } long r1 = helper(cp1); long r2 = helper(cp2); out(Math.max(r1, r2)); } long helper(long[] arr) { int n = arr.length; long res = 0; long[] dp = new long[n]; dp[0] = arr[0]; for (int i = 1; i < n; i++) { if (dp[i - 1] > 0) { dp[i] = arr[i] + dp[i - 1]; } else { dp[i] = arr[i]; } } for (int i = 0; i < n; i++) { res = Math.max(res, dp[i]); } return res; } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } public CFD() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFD(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
from itertools import accumulate inf = 10**9 + 1 def solve(): n = int(input()) a = [int(i) for i in input().split()] ad = [abs(a[i + 1] - a[i]) for i in range(n - 1)] ap = [0] + list(accumulate(ai*(-1)**i for i, ai in enumerate(ad))) max_dif, min_dif = max_min_profit(ap) ans = max(max_dif, -min_dif) print(ans) def max_min_profit(a): max_dif = -inf * 2 min_dif = inf * 2 min_v = max_v = a[0] for i in range(1, len(a)): max_dif = max(max_dif, a[i] - min_v) min_v = min(min_v, a[i]) min_dif = min(min_dif, a[i] - max_v) max_v = max(max_v, a[i]) return max_dif, min_dif if __name__ == '__main__': solve()
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ll OO = 1e18; int di[8] = {0, 0, 1, -1, -1, 1, -1, 1}; int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1}; string ys = "YES", no = "NO"; const long double dgr = acos(-1) / 180, dg = 180 / acos(-1); const int mod = 1e9 + 7, N = 5e5 + 5, M = 1001; int n; ll dp[N][2]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; vector<int> v(n); for (auto& i : v) cin >> i; n--; for (int i = 0; i < n; i++) v[i] = abs(v[i] - v[i + 1]); dp[n][0] = dp[n][1] = 0; ll ans = 0; for (int i = n - 1; i >= 0; i--) { dp[i][0] = v[i] + max(0ll, dp[i + 1][1]); ans = max(ans, dp[i][0]); dp[i][1] = -v[i] + max(0ll, dp[i + 1][0]); } cout << ans << '\n'; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.*; import java.util.*; public class TestClass { static PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); String s[] = in.readLine().split(" "); long a[] = new long[n]; for(int i=0;i<n;i++) { a[i] = Long.parseLong(s[i]); } long b[] = new long[n-1]; long c[] = new long[n-1]; for(int i=1;i<n;i++) { if(i%2!=0) { b[i-1] = Math.abs(a[i]-a[i-1]); c[i-1] = -Math.abs(a[i]-a[i-1]); } else { b[i-1] = -Math.abs(a[i]-a[i-1]); c[i-1] = Math.abs(a[i]-a[i-1]); } //System.out.println(b[i-1]); } long max =Long.MIN_VALUE,sum=0; for(int i=0;i<n-1;i++) { sum += b[i]; max = Math.max(sum,max); if(sum<0) { sum=0; } } sum=0; for(int i=0;i<n-1;i++) { sum += c[i]; max = Math.max(sum,max); if(sum<0) { sum=0; } } out.println(max); out.close(); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.util.*; import java.io.*; public class Forces{ public static void main(String ...arg) { Read cin = new Read(); int n = cin.nextInt(); Long even[] = new Long[n-1]; Long odd[] = new Long[n-1]; long a = cin.nextLong(); long b = a; int sign; long val; for(int i=0;i<n-1;i++) { a = b; b = cin.nextLong(); sign = (int) Math.pow(-1,i); val = Math.abs(a-b); even[i] = val * sign; odd[i] = val * sign * -1; } System.out.println(Math.max(kadaneAlgo(even),kadaneAlgo(odd))); } public static long kadaneAlgo(Long[] arr) { long cursum=0,max=0; int l=0; for(int i=0;i<arr.length;i++) { cursum += arr[i]; if(cursum > max) max = cursum; if(cursum < 0) { l=i+1; cursum = 0; } } return max; } } class Read { private BufferedReader br; private StringTokenizer st; public Read() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(IOException e) {e.printStackTrace();} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try {str = br.readLine();} catch(IOException e) {e.printStackTrace();} return str; } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long a[100010]; long long b[100010]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n - 1; i++) { b[i] = abs(a[i] - a[i + 1]); if (i % 2 == 0) b[i] *= -1; } long long x = 0, ans = 0; for (int i = 1; i < n; i++) { x += b[i]; ans = max(ans, x); if (x < 0) x = 0; } for (int i = 1; i < n; i++) b[i] *= -1; x = 0; long long temp = 0; for (int i = 1; i < n; i++) { x += b[i]; temp = max(temp, x); if (x < 0) x = 0; } ans = max(ans, temp); cout << ans; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const int inf = (int)1e9 + 10; const int mod = (int)1e9 + 7; const double eps = 1e-7; const double pi = 3.14159265359; const int maxLen = (int)1e5 + 2; int A[maxLen]; long long D[maxLen]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); D[1] = abs(A[1] - A[2]); for (int i = 2; i < n; i++) { D[i] = D[i - 1]; if (i % 2) D[i] += abs(A[i] - A[i + 1]); else D[i] -= abs(A[i] - A[i + 1]); } long long ans = D[1]; long long minSum = 0; long long maxSum = 0; for (int i = 1; i < n; i++) { if (i % 2) ans = max({ans, D[i] - minSum, D[i] - maxSum}); else ans = max({ans, maxSum - D[i], minSum - D[i]}); minSum = min(D[i], minSum); maxSum = max(D[i], maxSum); } printf("%lld", ans); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int N, A[100500]; long long B[100500]; long long tbs[100500], tbb[100500]; long long ans = (long long)-1e18; int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d", &A[i]); } for (int i = 1; i < N; i++) { B[i] = A[i] - A[i + 1]; if (B[i] < 0) B[i] = -B[i]; if (i % 2 == 0) B[i] = -B[i]; } for (int i = N - 1; i >= 1; i--) { tbs[i] = min(B[i], tbs[i + 1] + B[i]); tbb[i] = max(B[i], tbb[i + 1] + B[i]); if (i % 2 == 0) ans = max(ans, -tbs[i]); if (i % 2 == 1) ans = max(ans, tbb[i]); } printf("%lld\n", ans); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; public class Main { public static void main(String[] args) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); InputReader inputReader = new InputReader(in); PrintWriter out = new PrintWriter(System.out); int n = inputReader.getNextInt(); long[] values = new long[n]; Long[][] dp = new Long[n][2]; long sol = 0; for(int i = 0; i < n; i++) { values[i] = inputReader.getNextLong(); } dp[0][0] = 0L; for (int i = 1; i < n; i++) { dp[i][0] = 0L; if (dp[i-1][0] != null) { if (dp[i][1] == null) { dp[i][1] = dp[i-1][0] + Math.abs(values[i] - values[i-1]); } if (dp[i][1] < dp[i-1][0] + Math.abs(values[i] - values[i-1])) { dp[i][1] = dp[i-1][0] + Math.abs(values[i] - values[i-1]); } } if (dp[i-1][1] != null) { if (dp[i][0] == null) { dp[i][0] = dp[i-1][1] - Math.abs(values[i] - values[i-1]); } if (dp[i][0] < dp[i-1][1] - Math.abs(values[i] - values[i-1])) { dp[i][0] = dp[i-1][1] - Math.abs(values[i] - values[i-1]); } } sol = dp[i][0] != null ? Math.max(dp[i][0], sol) : sol; sol = dp[i][1] != null ? Math.max(dp[i][1], sol) : sol; } out.println(sol); in.close(); out.close(); } public static class InputReader { static final String SEPARATOR = " "; String[] split; int head = 0; BufferedReader in; public InputReader(BufferedReader in) { this.in = in; } private void fillBuffer() throws RuntimeException { try { if (split == null || head >= split.length) { head = 0; split = in.readLine().split(SEPARATOR); } } catch(Exception e) { throw new RuntimeException(e); } } public String getNextToken() throws RuntimeException { fillBuffer(); return split[head++]; } public int getNextInt() throws RuntimeException { return Integer.parseInt(getNextToken()); } public long getNextLong() throws RuntimeException { return Long.parseLong(getNextToken()); } } public static <T> String arrayToString(T[] arr) { StringBuilder stringBuilder = new StringBuilder(); for(T x: arr) { stringBuilder.append(x + " "); } return stringBuilder.toString(); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long getBrute(vector<long long> &a) { long long n = a.size(); long long ans = -INF; for (long long l = 0; l < n; l++) { long long cur = 0; for (long long r = l + 1; r < n; r++) { if ((r - l) % 2 == 1) { cur += abs(a[r] - a[r - 1]); } else { cur -= abs(a[r] - a[r - 1]); } ans = max(ans, cur); } } return ans; } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long random(long long a, long long b) { return uniform_int_distribution<long long>(a, b)(rng); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; vector<long long> dif(n - 1, 0); vector<long long> a(n); for (long long i = 0; i < (long long)n; i++) { cin >> a[i]; } reverse(a.begin(), a.end()); for (int i = 1; i < n; i++) { dif[i - 1] = abs(a[i] - a[i - 1]); } vector<long long> pref(n - 1, 0); for (int i = 0; i < n - 1; i++) { if (i < 2) { pref[i] = dif[i]; } else { pref[i] = pref[i - 2] + dif[i]; } } vector<long long> f(n - 1, 0); vector<long long> minF(2, 0); long long ans = dif[0]; for (int i = 1; i < n - 1; i++) { f[i] = pref[i] - pref[i - 1]; ans = max(ans, f[i] - minF[i % 2]); ans = max(ans, dif[i]); if (i >= 2) { minF[(i - 1) % 2] = min(minF[(i - 1) % 2], f[i - 1] - dif[i - 1]); } } cout << ans << "\n"; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(raw_input()) a = [int(x) for x in raw_input().split()] def drawdown(l): cumsum = 0 min_x = 0 answer = 0 for i,val in enumerate(l): if i%2 == 0: min_x = min(min_x, cumsum) cumsum += val answer = max(answer, cumsum - min_x) return answer poso = [abs(a[i]-a[i+1])*(-1)**i for i in range(n-1)] nego = [abs(a[i]-a[i+1])*(-1)**(i-1) for i in range(1,n-1)] print max(drawdown(poso), drawdown(nego))
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; void fun() { int n; cin >> n; long long a[n]; for (auto &i : a) cin >> i; vector<long long> s; for (int i = 1; i < n; i++) s.push_back(abs(a[i] - a[i - 1])); long long dpn[n - 1], dpp[n - 1]; dpp[0] = s[0]; dpn[0] = -s[0]; for (int i = 1; i < n - 1; i++) { dpp[i] = max(s[i], s[i] + dpn[i - 1]); dpn[i] = max(-s[i], -s[i] + dpp[i - 1]); } long long ans = *max_element(dpp, dpp + n - 1); ans = max(ans, *max_element(dpn, dpn + n - 1)); cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(time(0)); ; int t = 1; while (t--) { fun(); } }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
from itertools import accumulate inf = 10**9 + 1 def solve(): n = int(input()) a = [int(i) for i in input().split()] ad = [abs(a[i + 1] - a[i]) for i in range(n - 1)] ap = [0] + list(accumulate(ai*(-1)**i for i, ai in enumerate(ad))) max_dif, min_dif = max_min_profit(ap) ans = max(max_dif, -min_dif) print(ans) def max_min_profit(a): max_dif = min_dif = 0 min_v = max_v = 0 for i in range(1, len(a)): if a[i] < min_v: min_v = a[i] else: max_dif = max(max_dif, a[i] - min_v) if a[i] > max_v: max_v = a[i] else: min_dif = min(min_dif, a[i] - max_v) return max_dif, min_dif if __name__ == '__main__': solve()
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int N = 100010; map<long long, int> mp; int main() { long long n, i; long long a[100010], b[100010]; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%lld", &a[i]); if (i >= 1) b[i] = abs(a[i] - a[i - 1]); } long long ans = 0; long long maxx = b[1]; for (i = 1; i < n; i++) { if (ans < 0) ans = 0; if (i % 2 == 1) ans += b[i]; else ans -= b[i]; maxx = max(maxx, ans); } ans = 0; for (i = 1; i < n; i++) { if (ans < 0) ans = 0; if (i % 2 == 0) ans += b[i]; else ans -= b[i]; maxx = max(maxx, ans); } printf("%lld", maxx); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) _l = list(map(int, input().split())) l = [abs(_l[i] - _l[i + 1]) for i in range(n - 1)] p, n, res = 0, 0, 0 for e in l: _p = max(0, n + e) _n = max(0, p - e) p, n = _p, _n res = max(p, n, res) print(res)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long const N = 1e6 + 5; long long dp[N][2], a[N]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; n--; for (int i = 0; i < n; i++) a[i] = abs(a[i] - a[i + 1]); long long mx = a[n - 1]; dp[n - 1][0] = a[n - 1], dp[n - 1][1] = -1 * a[n - 1]; for (int i = n - 2; i >= 0; i--) { dp[i][0] = max(a[i], a[i] + dp[i + 1][1]); dp[i][1] = max(-1 * a[i], dp[i + 1][0] - a[i]); mx = max(mx, dp[i][0]); } cout << mx << endl; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX_N = 1e5 + 10; const long long mod = 1e9 + 9; const double PI = acos(-1); int num[MAX_N]; long long dp[MAX_N]; int ab(int x) { return x > 0 ? x : -x; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &num[i]); for (int i = 1; i < n; ++i) num[i] = ab(num[i + 1] - num[i]); num[n] = 0; dp[0] = num[1]; long long ans = num[1]; for (int i = 1;; ++i) { if ((i << 1 | 1) >= n) break; dp[i] = max(dp[i - 1] + num[i << 1 | 1] - num[i << 1], (long long)num[i << 1 | 1]); ans = max(ans, dp[i]); } dp[1] = num[2]; ans = max(ans, dp[1]); for (int i = 2;; ++i) { if ((i << 1) >= n) break; dp[i] = max(dp[i - 1] + num[i << 1] - num[(i - 1) << 1 | 1], (long long)num[i << 1]); ans = max(ans, dp[i]); } printf("%I64d\n", ans); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } long[] d = new long[n - 1]; for (int i = 0; i + 1 < n; i++) { d[i] = Math.abs(a[i] - a[i + 1]); } long ans = d[0]; long sPlus = d[0]; long sMinus = 0; long minPlus = 0; long minMinus = 0; for (int i = 1; i < d.length; i++) { int sign = i % 2 == 1 ? -1 : +1; sPlus += sign * d[i]; sMinus -= sign * d[i]; ans = Math.max(ans, sPlus - minPlus); ans = Math.max(ans, sMinus - minMinus); minPlus = Math.min(minPlus, sPlus); minMinus = Math.min(minMinus, sMinus); } out.println(ans); } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { String rl = in.readLine(); if (rl == null) { return null; } st = new StringTokenizer(rl); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long fastPow(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret *= a; a *= a; b >>= 1; } return ret; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n, e, o, ce, co, ret = INT_MIN, c; cin >> n; vector<long long> nums(n + 1); for (int i = int(1); i < int(n + 1); i++) cin >> nums[i]; e = 0, o = 0, ce = 0, co = 0, c = 0; for (int l = int(1); l < int(n); l++) { if (!(l % 2)) { e = abs(nums[l] - nums[l + 1]); ce = max(ce + e, c); o = e * -1; co = max(co + o, c); } else { e = abs(nums[l] - nums[l + 1]) * -1; ce = max(ce + e, c); o = e * -1; co = max(co + o, c); } ret = max(ret, max(ce, co)); } cout << ret << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n - 1; i++) { arr[i] = abs(arr[i] - arr[i + 1]); } vector<long long> a(n), b(n); a[0] = arr[0]; long long ans1 = a[0], ans2 = 0; for (int i = 1; i < n - 1; i++) { if (i & 1) { a[i] = max(a[i - 1] + arr[i] * -1, arr[i] * -1); } else { a[i] = max(a[i - 1] + arr[i], arr[i]); } ans1 = max(ans1, a[i]); } b[0] = -1 * arr[0]; ans2 = b[0]; for (int i = 1; i < n - 1; i++) { if (i & 1) { b[i] = max(b[i - 1] + arr[i], arr[i]); } else { b[i] = max(b[i - 1] + arr[i] * -1, arr[i] * -1); } ans2 = max(ans2, b[i]); } cout << max(ans1, ans2); }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5, INF = 0x7F; long long n, a[maxn], f[maxn], sum[maxn], Max = -INF, Min = INF; int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); Max = max(Max, 0ll); Min = min(Min, 0ll); for (int i = 1; i < n; i++) { f[i] = abs(a[i] - a[i + 1]); sum[i] = sum[i - 1] + (i & 1 ? f[i] : -f[i]); Max = max(sum[i], Max); Min = min(sum[i], Min); } printf("%lld\n", Max - Min); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(raw_input()) arr = map(int,raw_input().split()) arr1 = [] arr2 = [] for i in range(n-1): if i%2 == 0: arr1.append(abs(arr[i]-arr[i+1])) arr2.append(-abs(arr[i]-arr[i+1])) else: arr1.append(-abs(arr[i]-arr[i+1])) arr2.append(abs(arr[i]-arr[i+1])) max1 = arr1[0] curMax = arr1[0] for i in range(1,n-1): curMax += arr1[i] if max1 < curMax: max1 = curMax if curMax < 0: curMax = 0 if n >= 3: curMax = arr2[1] for i in range(2,n-1): curMax += arr2[i] if max1 < curMax: max1 = curMax if curMax < 0: curMax = 0 print max(max1,curMax)
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) l = tuple(map(int, input().split())) a = [] for i in range(n - 1): a.append(abs(l[i] - l[i + 1])) ev = [(a[i] if i % 2 == 0 else -a[i]) for i in range(n - 1)] od = [-i for i in ev] od[0] = 0 dp = [ev[0]] st = ["ev"] # print(a) # print(ev) # print(od) vmax = dp[0] evsum = evans = 0 odsum = odans = 0 for i in range(0, n - 1): evsum += ev[i] odsum += od[i] evans = max(evsum, evans) odans = max(odsum, odans) if evsum < 0 and i % 2 == 1: evsum = 0 if odsum < 0 and i % 2 == 0: odsum = 0 # print(evans, odans) print(max(evans, odans))
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.*; import java.util.*; public class C { static StringBuilder st = new StringBuilder(); static int n ; static long [][] memo ; static int [] a ; static long dp(int idx , int neg) { if(idx == n - 1) return 0 ; if(memo[neg][idx] != -1)return memo[neg][idx] ; long diff = (neg == 1 ? -1 : 1 ) * Math.abs(a[idx] - a[idx + 1]) ; return memo[neg][idx] = Math.max(diff, diff + dp(idx + 1, neg ^ 1)) ; } public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in) ; PrintWriter out = new PrintWriter(System.out) ; n = sc.nextInt() ; a = new int [n+1] ; for(int i = 0 ; i < n ;i++)a[i] = sc.nextInt() ; memo = new long [2][n+1]; for(int i = 0 ; i< 2 ; i++) Arrays.fill(memo[i], -1); long ans = Long.MIN_VALUE; for(int i = 0 ; i < n - 1 ; i++) ans = Math.max(ans, dp(i, 0)) ; out.println(ans); out.flush(); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(System.in)); } String next() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(next()); } long nextLong() throws Exception { return Long.parseLong(next()); } double nextDouble() throws Exception { return Double.parseDouble(next());} } static void shuffle(int[] a) { int n = a.length; for (int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.*; import java.text.DecimalFormat; import java.util.*; public class tennis { static boolean DEBUG_FLAG = false; int INF = (int)1e9; long MOD = 1000000007; long p = 101; long ans = 0; static void debug(String s) { if(DEBUG_FLAG) { System.out.print(s); } } void solve(InputReader in, PrintWriter out) throws IOException { int n = in.nextInt(); int[] arr = new int[n+1]; for(int i = 1;i<=n;i++) arr[i] = in.nextInt(); int[] value = new int[n]; for(int i = 1;i<n;i++){ value[i] = Math.abs(arr[i] - arr[i + 1]); } long max = 0; { max = 0; long max_till = 0; int flag = 0; for(int i= 1;i<n;i++){ if(flag == 0){ max_till = max_till + value[i]; flag = 1; } else{ max_till = max_till - value[i]; flag = 0; } //System.out.println(max + " kl" + max_till ); if(max_till < 0) max_till = 0; if(max < max_till) max = max_till; } } //System.out.println(max); long art = max; max = 0; long max_till = 0; int flag = 0; for(int i= 2;i<n;i++){ if(flag == 0){ max_till = max_till + value[i]; flag = 1; } else{ max_till = max_till - value[i]; flag = 0; } if(max_till < 0) max_till = 0; if(max < max_till) max = max_till; } if(art > max) System.out.println(art); else System.out.println(max); } public static void main(String[] args) throws IOException { if(args.length>0 && args[0].equalsIgnoreCase("d")) { DEBUG_FLAG = true; } InputReader in = new InputReader(); PrintWriter out = new PrintWriter(System.out); int t =1; long start = System.nanoTime(); while(t-- >0) { new tennis().solve(in, out); } long end = System.nanoTime(); debug("\nTime: " + (end-start)/1e6 + " \n\n"); out.close(); } static class InputReader { static BufferedReader br; static StringTokenizer st; public InputReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.*; import java.math.*; import java.util.*; public class Main { static FastReader in; static PrintWriter o; public static void solve() { int n = in.nextInt(); long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } long[] arr1 = new long[n-1]; long[] arr2 = new long[n-1]; for (int i = 0; i < n - 1; i++) { arr1[i] = Math.abs(arr[i] - arr[i+1]); arr2[i] = Math.abs(arr[i] - arr[i+1]); if (i % 2 == 0) { arr1[i] = -1 * arr1[i]; } if (i % 2 == 1) { arr2[i] = -1 * arr2[i]; } } long max = 0; long sum = 0; for (int i = 0; i < n - 1; i++) { sum += arr1[i]; max = Math.max(sum, max); if (sum < 0) sum = 0; } sum = 0; for (int i = 0; i < n - 1; i++) { sum += arr2[i]; max = Math.max(sum, max); if (sum < 0) sum = 0; } o.println(max); o.close(); return; } public static void main(String[] args) { in = new FastReader(); o = new PrintWriter(System.out); solve(); return; } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long int a[1000005]; long long int dp[1000005][2]; int main() { long long int n; scanf("%lld", &n); for (long long int i = 1; i <= (long long int)n; i++) scanf("%lld", a + i); dp[1][0] = abs(a[1] - a[1 + 1]); dp[1][1] = -abs(a[1] - a[1 + 1]); long long int ans = -1000000000000000LL; for (long long int i = 2; i <= (long long int)n - 1; i++) { dp[i][0] = max(abs(a[i] - a[i + 1]), dp[i - 1][1] + abs(a[i] - a[i + 1])); dp[i][1] = max(-abs(a[i] - a[i + 1]), dp[i - 1][0] - abs(a[i] - a[i + 1])); } for (long long int i = 1; i <= (long long int)n - 1; i++) ans = max(ans, dp[i][0]); for (long long int i = 1; i <= (long long int)n - 1; i++) ans = max(ans, dp[i][1]); printf("%lld\n", ans); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const long long INF = 200000000000000000LL; long long kadane(vector<long long> v) { long long sum = 0; long long ans = sum; for (long long i : v) { sum = max(sum + i, i); ans = max(ans, sum); } return ans; } int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i <= n - 1; i++) { scanf("%d", &a[i]); } vector<vector<long long>> dp(2, vector<long long>(n - 1)); int r = -1; for (int i = 0; i <= n - 2; i++) { r *= -1; dp[0][i] = abs(a[i] - a[i + 1]) * r; dp[1][i] = abs(a[i] - a[i + 1]) * (-r); } cout << max(kadane(dp[0]), kadane(dp[1])) << endl; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long flag(vector<long long> &); int main() { vector<long long> a, b; long long n, x, y, sum = 0, s, q; cin >> n; for (int i = 1; i != n + 1; ++i) { if (i == 1) { cin >> x; continue; } cin >> y; q = y - x; if (q < 0) q = -q; if (i % 2 == 0) { a.push_back(q); b.push_back(-q); } else { a.push_back(-q); b.push_back(q); } x = y; } sum = flag(a); s = flag(b); if (s > sum) sum = s; cout << sum; return 0; } long long flag(vector<long long> &c) { long long da = 0, er = 0; for (auto i = 0; i != c.size(); ++i) { if (er + c[i] > 0) { er += c[i]; } else { er = 0; } if (er > da) da = er; } return da; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
/** * Created by sachgoya on 2/24/2017. */ import java.io.*; import java.util.Arrays; import java.util.StringTokenizer; /** * Created by sachin.goyal on 07/02/17. */ public class C { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int n = in.nextInt(); int arr[] = new int[n]; int full[] = new int[n - 1]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } if(n == 2){ int ans = Math.abs(arr[0] - arr[1]); out.println(ans); return; } int less[] = new int[n - 2]; for (int i = 0; i < n - 1; i++) { full[i] = (int) (Math.pow(-1, i) * Math.abs(arr[i] - arr[i + 1])); } for(int i=0;i<n-2;i++){ less[i] = -1 * full[i+1]; } long max = maxSubArraySum(full); long max1 = maxSubArraySum(less); out.println(max > max1 ? max : max1); } public long maxSubArraySum(int a[]) { int size = a.length; long max_so_far = Long.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) { max_so_far = max_ending_here; } if (max_ending_here < 0) { max_ending_here = 0; } } return max_so_far; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int const oo = 1e9, bound = 1e6 + 1, mod = oo + 7; long long const OO = 1e18; int n; int aa[bound]; long long dp[bound][3][2]; long long rec(int idx, int tk, int sg) { long long &ret = dp[idx][tk][sg]; if (ret != -1) return ret; if (idx == n - 1) return ret = 0; if (tk == 2) return ret = 0; if (tk == 0) return ret = max(rec(idx + 1, tk, sg), rec(idx + 1, 1, 1) + abs(aa[idx] - aa[idx + 1])); if (tk == 1) return ret = max(rec(idx + 1, 2, !sg), rec(idx + 1, tk, !sg) + abs(aa[idx] - aa[idx + 1]) * (sg == 0 ? 1 : -1)); } int main() { memset(dp, -1, sizeof dp); scanf("%d", &n); for (int(i) = 0; (i) < (n); (i)++) scanf("%d", &aa[i]); printf("%lld\n", rec(0, 0, 0)); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n=input() lst=map(int,raw_input().split()) lst2=[] lst3=[] for i in range(0,n-1): vr=abs(lst[i+1]-lst[i])*(pow(-1,i)) lst2.append(vr) lst3.append(-1*vr) # Python program to find maximum contiguous subarray # Function to find the maximum contiguous subarray from sys import maxint def maxSubArraySum(a,size): max_so_far = -maxint - 1 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here): max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far a = lst2 print max(maxSubArraySum(a,len(a)),maxSubArraySum(lst3,len(lst3)))
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = input() a = map(int,raw_input().split()) ans = 0 #even indexed p = 0 x = 1 cursum = 0 while p < len(a)-1: cursum += abs(a[p]-a[p+1]) * x if cursum < 0: cursum = 0 if p % 2 != 0: p += 1 else: p += 2 x = 1 else: ans = max(ans,cursum) p += 1 x *= -1 # odd indexed p = 1 x = 1 cursum = 0 while p < len(a)-1: cursum += abs(a[p]-a[p+1]) * x if cursum < 0: cursum = 0 if p % 2 == 0: p += 1 else: p += 2 x = 1 else: ans = max(ans,cursum) p += 1 x *= -1 print ans
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
from itertools import accumulate inf = 10**9 + 1 def solve(): n = int(input()) a = [int(i) for i in input().split()] ad = [abs(a[i + 1] - a[i]) for i in range(n - 1)] ap = [0] + list(accumulate(ai*(-1)**i for i, ai in enumerate(ad))) am = [0] + list(accumulate(ai*(-1)**(i + 1) for i, ai in enumerate(ad))) ans = max_profit(ap) ans = max(ans, max_profit(am)) print(ans) def max_profit(a): max_dif = 0 min_v = inf for i in range(len(a)): if a[i] < min_v: min_v = a[i] else: max_dif = max(max_dif, a[i] - min_v) return max_dif if __name__ == '__main__': solve()
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.PriorityQueue; public final class CF_407_Functions { static void log(int[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } static void log(long[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } static void log(Object[] X){ int L=X.length; for (int i=0;i<L;i++){ logWln(X[i]+" "); } log(""); } static void log(Object o){ logWln(o+"\n"); } static void logWln(Object o){ System.err.print(o); // outputWln(o); } static void info(Object o){ System.out.println(o); //output(o); } static void output(Object o){ outputWln(""+o+"\n"); } static void outputWln(Object o){ // System.out.print(o); try { out.write(""+ o); } catch (Exception e) { } } static boolean check(String s,String w,int[] remove,int time){ int L=s.length(); int W=w.length(); int last=0; boolean[] ok=new boolean[W]; for (int i=0;i<L;i++){ if (time<remove[i]){ char c=s.charAt(i); if (c==w.charAt(last)) last++; if (last==W) return true; } } return false; } static void solve(String s,String w,int[] p,int [] remove){ int n=p.length; int lo=0; int hi=s.length(); while (lo+1<hi){ int mid=(hi+lo)/2; boolean b=check(s,w,remove,mid); if (b) lo=mid; else hi=mid; } output(lo); } // Global vars static BufferedWriter out; static InputReader reader; static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader=new InputReader(System.in); int n=reader.readInt(); long[] a=new long[n]; for (int i=0;i<n;i++){ a[i]=reader.readInt(); } // first even long min=0; long sum=0; long max=0; for (int i=0;i+1<n;i++){ long x=Math.abs(a[i]-a[i+1]); if (i%2==0) { sum+=x; max=Math.max(max,sum-min); } else { sum-=x; if (sum<min) min=sum; } } min=0; sum=0; for (int i=1;i+1<n;i++){ long x=Math.abs(a[i]-a[i+1]); if (i%2==1) { sum+=x; max=Math.max(max,sum-min); } else { sum-=x; if (sum<min) min=sum; } } output(max); try { out.close(); } catch (Exception e){} } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res=new StringBuilder(); do { res.append((char)c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg=false; while (isSpaceChar(c)) { c = read(); } char d=(char)c; //log("d:"+d); if (d=='-') { neg=true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); //log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long int cal(vector<int> f) { long long int sum = 0; long long int cur = 0; for (int i = 0; i < f.size(); ++i) { cur += f[i]; if (cur < 0) { cur = 0; } if (cur > sum) { sum = cur; } } return sum; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<int> a(n), f(n - 1); for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int i = 1; i < n; ++i) { f[i - 1] = pow(-1, i - 1) * abs(a[i - 1] - a[i]); } long long int sum = cal(f); for (int i = 1; i < n; ++i) { f[i - 1] = pow(-1, i) * abs(a[i - 1] - a[i]); } long long int sum2 = cal(f); cout << max(sum, sum2) << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import math def findmax(a): c = ret = a[0] for i in range(1, len(a)): c = max(a[i], c + a[i]) ret = max(ret, c) return ret n = int(raw_input()) a = map(int, raw_input().split()) p = [] np = [] m = 1 for i in range(len(a) - 1): p.append(abs(a[i] - a[i + 1]) * m) m *= -1 if i > 0: np.append(abs(p[-1]) * m) if np: print max(findmax(p), findmax(np)) else: print findmax(p)
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class Abood2C { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = sc.nextInt(); long[] t = new long[n]; for (int i = 0; i < n; i++) t[i] = sc.nextInt(); long[] d = new long[n - 1]; for (int i = 1; i < n; i++) { d[i - 1] = Math.abs(t[i] - t[i - 1]); } long ans = 0; long sum = 0; for (int i = 0; i < d.length; i++) { long v = d[i] * (i % 2 == 0 ? 1 : -1); sum = Math.max(0, sum + v); ans = Math.max(ans, sum); } sum = 0; for (int i = 1; i < d.length; i++) { long v = d[i] * (i % 2 == 1 ? 1 : -1); sum = Math.max(0, sum + v); ans = Math.max(ans, sum); } out.println(ans); out.flush(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Codeforces { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); int n = in.nextInt(); long[] arr = in.nextLongArr(n); long[][] dp = new long[arr.length][2]; dp[1][0] = 0; dp[1][1] = Math.abs(arr[0] - arr[1]); for (int i = 2; i < arr.length; i++) { dp[i][0] = Math.max(0, dp[i - 1][1] - Math.abs(arr[i - 1] - arr[i])); dp[i][1] = Math.max(Math.abs(arr[i - 1] - arr[i]), dp[i - 1][0] + Math.abs(arr[i - 1] - arr[i])); } long res = Integer.MIN_VALUE; for (int i = 1; i < arr.length; i++) { res = Math.max(res, dp[i][0]); res = Math.max(res, dp[i][1]); } out.println(res); out.close(); } public static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public int[] nextIntArr(int n) { int[] arr = new int[n]; for (int j = 0; j < arr.length; j++) { arr[j] = nextInt(); } return arr; } public long[] nextLongArr(int n) { long[] arr = new long[n]; for (int j = 0; j < arr.length; j++) { arr[j] = nextLong(); } return arr; } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; /** * * @author msagimbekov */ public class Solution798C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String l = br.readLine(); int n = Integer.parseInt(l); String[] line = br.readLine().split(" "); long[] a = new long[n]; long[] diff = new long[n - 1]; for (int i = 0; i < n; i++) { a[i] = Long.parseLong(line[i]); if (i > 0) { diff[i - 1] = Math.abs(a[i] - a[i - 1]); } } long[] plus = new long[n - 1]; long[] minus = new long[n - 1]; plus[0] = diff[0]; minus[0] = 0; long max = plus[0]; for (int i = 1; i < n - 1; i++) { plus[i] = Math.max(diff[i], diff[i] + minus[i - 1]); minus[i] = plus[i - 1] - diff[i]; if (plus[i] > max) { max = plus[i]; } if (minus[i] > max) { max = minus[i]; } } System.out.println(max); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = (int)1e5 + 5; long long v[MAXN]; long long a[MAXN]; long long dp[MAXN][2]; int m; inline int sign(int x) { if (x == 0) { return 1; } return -1; } long long solve(int pos, int flag) { if (pos >= m) { return 0; } if (dp[pos][flag] != -1) { return dp[pos][flag]; } long long aux = max(0LL, a[pos] * sign(flag) + solve(pos + 1, 1 - flag)); dp[pos][flag] = aux; return aux; } int main(void) { int n; scanf(" %d", &n); for (int i = 0; i < n; i++) { scanf(" %lld", &v[i]); } m = n - 1; for (int i = 1; i < n; i++) { a[i - 1] = abs(v[i - 1] - v[i]); } memset(dp, -1, sizeof(dp)); long long res = 0; for (int i = 0; i < m; i++) { res = max(res, solve(i, 0)); } printf("%lld\n", res); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long int n, ans, s = 0, f = 0, arr[100005] = {0}, arr2[100005] = {0}, mx = -10000000000000000; void kadane() { for (int i = 0; i < n - 1; i++) { if (arr2[i] + s < 0) { s = 0; } else { s += arr2[i]; } if (s > mx) { mx = s; } } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n - 1; i++) { arr2[i] = abs(arr[i] - arr[i + 1]); if (i % 2 != 0) { arr2[i] = -arr2[i]; } } s = 0; kadane(); s = 0; for (int i = 0; i < n - 1; i++) { arr2[i] = -arr2[i]; } s = 0; kadane(); cout << mx << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.util.Scanner; public class B { public static void main(String[] args) { Scanner x = new Scanner(System.in); int n=x.nextInt(); long arr[] = new long [n]; for(int i=0;i<n;i++){ arr[i]=x.nextLong(); } long[] sum = new long[n-1]; for(int i=0;i<n-1;i++) { sum[i]= Math.abs(arr[i] - arr[i+1]) ; } long max2= 0; long first = sum[0]; long max1= first; long maxoooooo=first; for(int i=1;i<sum.length;i++){ if(i%2==0){ max1 = Math.max(0+sum[i], max1+sum[i]); max2 = Math.max(0-sum[i], max2-sum[i]); }else{ max1 = Math.max(0-sum[i], max1-sum[i]); max2 = Math.max(0+sum[i], max2+sum[i]); } maxoooooo = Math.max(maxoooooo, Math.max(max1, max2)); } maxoooooo = Math.max(maxoooooo, Math.max(max1, max2)); System.out.println(maxoooooo); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
// package codeforces.cf4xx.cf407.div1; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class A { public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n = in.nextInt(); long[] a = in.nextLongs(n); long[] diff = new long[n-1]; for (int i = 0; i < n-1; i++) { diff[i] = Math.abs(a[i+1]-a[i]) * ((i % 2 == 0) ? -1 : 1); } long[] diffM = diff.clone(); for (int i = 0; i < n-1; i++) { diffM[i] *= -1; } out.println(Math.max(solve(diff), solve(diffM))); out.flush(); } private static long solve(long[] a) { int n = a.length; long max = 0; long partial = 0; for (int i = 0; i < n ; i++) { partial += a[i]; if (partial < 0) { partial = 0; } max = Math.max(max, partial); } return max; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int[] nextInts(int n) { int[] ret = new int[n]; for (int i = 0; i < n; i++) { ret[i] = nextInt(); } return ret; } private int[][] nextIntTable(int n, int m) { int[][] ret = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[i][j] = nextInt(); } } return ret; } private long[] nextLongs(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = nextLong(); } return ret; } private long[][] nextLongTable(int n, int m) { long[][] ret = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ret[i][j] = nextLong(); } } return ret; } private double[] nextDoubles(int n) { double[] ret = new double[n]; for (int i = 0; i < n; i++) { ret[i] = nextDouble(); } return ret; } private int next() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public char nextChar() { int c = next(); while (isSpaceChar(c)) c = next(); if ('a' <= c && c <= 'z') { return (char) c; } if ('A' <= c && c <= 'Z') { return (char) c; } throw new InputMismatchException(); } public String nextToken() { int c = next(); while (isSpaceChar(c)) c = next(); StringBuilder res = new StringBuilder(); do { res.append((char) c); c = next(); } while (!isSpaceChar(c)); return res.toString(); } public int nextInt() { int c = next(); while (isSpaceChar(c)) c = next(); int sgn = 1; if (c == '-') { sgn = -1; c = next(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c-'0'; c = next(); } while (!isSpaceChar(c)); return res*sgn; } public long nextLong() { int c = next(); while (isSpaceChar(c)) c = next(); long sgn = 1; if (c == '-') { sgn = -1; c = next(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c-'0'; c = next(); } while (!isSpaceChar(c)); return res*sgn; } public double nextDouble() { return Double.valueOf(nextToken()); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static void debug(Object... o) { System.err.println(Arrays.deepToString(o)); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int main() { long long int n, N[100005]; long long int A[100005]; long long int B[100005]; cin >> n; long long int i; for (i = 1; i <= n; i++) { cin >> N[i]; } for (i = 1; i < n; i++) { long long int k; k = abs(N[i + 1] - N[i]); if (i % 2 == 1) { A[i] = k; B[i] = -k; } else { k = -k; A[i] = k; B[i] = -k; } } long long int lrge = A[1]; long long int suprlrge = A[1]; for (i = 2; i < n; i++) { lrge = max(A[i], lrge + A[i]); suprlrge = max(lrge, suprlrge); } long long int lrge1 = B[0]; for (i = 2; i < n; i++) { lrge1 = max(B[i], lrge1 + B[i]); suprlrge = max(lrge1, suprlrge); } cout << suprlrge; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(raw_input()) arr = map(int,raw_input().split()) newarr1 = [] newarr2 = [] one = 1 for i in xrange(n-1): newarr1.append(one*abs(arr[i+1]-arr[i])) newarr2.append(-1*one*abs(arr[i+1]-arr[i])) one*=-1 val = 0 temp = [] for i in xrange(len(newarr1)): if(val+newarr1[i]>0): val += newarr1[i] temp.append(val) else: temp.append(val) val = 0 ans1 = max(temp) temp = [] val = 0 for i in xrange(len(newarr2)): if(val+newarr2[i]>0): val += newarr2[i] temp.append(val) else: temp.append(val) val = 0 ans2 = max(temp) ans = max(ans1,ans2) print ans
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
def main(): n = int(input()) arr = list(map(int,input().split())) dp = [] ans = -float('inf') total = 0 for i in range(n-1): if i%2 == 0: total += abs(arr[i]-arr[i+1]) else: total -= abs(arr[i]-arr[i+1]) ans = max(ans,abs(arr[i]-arr[i+1])) dp.append(total) dp2 = [] dp.reverse() for i in dp: if not dp2: dp2.append([i,i]) else: dp2.append([min(dp2[-1][0],i),max(dp2[-1][1],i)]) dp.reverse() dp2.reverse() #print(dp) #print(dp2) for i in range(len(dp)-1): if i%2 == 0: total = dp2[i+1][1] if i-1 >= 0: total -= dp[i-1] else: total = abs(dp2[i+1][0]-dp[i-1]) #print(total) ans = max(ans,total) if n == 2: print(dp[-1]) return print(ans) main()
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int ar[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; } int b[n]; for (int i = 0; i < n - 1; i++) { b[i] = abs(ar[i] - ar[i + 1]); if (i % 2 == 0) { b[i] *= -1; } } int c[n]; for (int i = 0; i < n - 1; i++) { c[i] = abs(ar[i] - ar[i + 1]); if (i % 2 != 0) { c[i] *= -1; } } long long int mx1 = 0; long long int mxsofar = 0; for (int i = 0; i < n - 1; i++) { mxsofar += b[i]; if (mxsofar < 0) { mxsofar = 0; } if (mxsofar > mx1) { mx1 = mxsofar; } } long long int mx2 = 0; mxsofar = 0; for (int i = 0; i < n - 1; i++) { mxsofar += c[i]; if (mxsofar < 0) { mxsofar = 0; } if (mxsofar > mx2) { mx2 = mxsofar; } } cout << max(mx1, mx2); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.util.*; public class Functional { public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); long[] arr = new long[n-1]; long last = in.nextLong(); for (int i = 1; i < n; i++) { long current = in.nextLong(); arr[i-1] = Math.abs(last - current); last = current; } long max = 0; long sum = 0; for (int i = 0; i < n-1; i++) { if (sum < 0) sum = 0; if (i%2 == 0) sum += arr[i]; else sum -= arr[i]; if (sum > max) max = sum; } sum = 0; for (int i = 0; i < n-1; i++) { if (sum < 0) sum = 0; if (i%2 == 1) sum += arr[i]; else sum -= arr[i]; if (sum > max) max = sum; } System.out.println(max); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().split()) def msi(): return map(str,input().split()) def li(): return list(mi()) n=ii() a=li() dp=[[0,0] for i in range(n)] for i in range(1,n): curr=abs(a[i]-a[i-1]) if dp[i-1][0]>0: dp[i][1]=curr dp[i][0]=curr-dp[i-1][1] else: dp[i][1]=curr-dp[i-1][0] if dp[i-1][1]<0: dp[i][0]=curr else: dp[i][0]=curr-dp[i-1][1] ans=0 for i in range(n): ans=max(ans,dp[i][1]) print(ans)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int main() { vector<long long int> arr, arr2, x, y; int n; long long int temp; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &temp); arr.push_back(temp); } for (int i = 0; i < n - 1; i++) { arr2.push_back(abs(arr[i] - arr[i + 1])); } for (int i = 0; i < n - 1; i++) { if (i & 1) { x.push_back(arr2[i]); y.push_back(-arr2[i]); } else { x.push_back(-arr2[i]); y.push_back(arr2[i]); } } long long int p = 0, s = 0; for (int k = 0; k < n - 1; k++) { s = max(x[k], s + x[k]); p = max(p, s); } long long int p2 = 0, s2 = 0; for (int k = 0; k < n - 1; k++) { s2 = max(y[k], s2 + y[k]); p2 = max(p2, s2); } cout << max(p2, p) << "\n"; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) d = [] for i in range(n-1): if i%2==0: d.append(abs(a[i+1]-a[i])) else: d.append(-abs(a[i+1]-a[i])) ans = -10**18 m = 0 acc = 0 for i in range(n-1): acc += d[i] ans = max(ans, acc-m) if i%2==1: m = min(m, acc) d = list(map(lambda x: -x, d)) m = 0 acc = 0 for i in range(1, n-1): acc += d[i] ans = max(ans, acc-m) if i%2==0: m = min(m, acc) print(ans)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
read = lambda: map(int, input().split()) n = int(input()) a = list(read()) d = [0] * n b = [0] * n c = [0] * n sgn = 1 for i in range(1, n): d[i] = abs(a[i - 1] - a[i]) b[i] = d[i] * sgn c[i] = -b[i] sgn *= -1 s = [0] * n for i in range(1, n): s[i] = s[i - 1] + b[i] mn = ans = 0 for i in range(1, n): if i % 2: ans = max(ans, s[i] - mn) else: mn = min(mn, s[i]) for i in range(1, n): s[i] = s[i - 1] + c[i] mn = 0 for i in range(1, n): if i % 2: mn = min(mn, s[i]) else: ans = max(ans, s[i] - mn) print(ans)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:2000000") #pragma comment(linker, "/HEAP:2000000") using namespace std; void print_width(long long x) { std::cout << std::fixed; std::cout << std::setprecision(x); } long long power(long long x, long long y, long long p = 1000000007) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } void printArr(long long a[], long long n) { for (long long i = 0; i < n; i++) cout << a[i] << " "; cout << '\n'; } void printVector(std::vector<long long> v) { for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; cout << '\n'; } void printVectorPair(std::vector<pair<long long, long long>> v) { for (long long i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << '\n'; ; cout << '\n'; } long long Min(long long a, long long b, long long c) { if (a < b and a < c) return a; if (b < c) return b; return c; } void initialize(long long arr[], long long n) { for (long long i = 0; i <= n; i++) arr[i] = i; } long long root(long long arr[], long long i) { while (arr[i] != i) { arr[i] = arr[arr[i]]; i = arr[i]; } return i; } void Union(long long arr[], long long a, long long b) { long long root_a = root(arr, a); long long root_b = root(arr, b); arr[root_a] = root_b; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long power_wm(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } long double dpower(long double x, long long y) { long double res = 1; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } std::vector<long long> vsum(std::vector<long long> a) { std::vector<long long> s(a.size()); s[0] = a[0]; for (long long i = 1; i <= a.size() - 1; i++) { s[i] = s[i - 1] + a[i]; } return s; } bool comp(pair<long long, long long> a, pair<long long, long long> b) { long long x = a.second * b.first; long long y = a.first * b.second; if (x <= y) return false; else return true; } bool is_cap(char a) { if (a <= 'Z' and a >= 'A') return true; else return false; } bool is_small(char a) { if (a <= 'z' and a >= 'a') return true; else return false; } string findSum(string str1, string str2) { string str = ""; if (str1.size() > str2.size()) { swap(str1, str2); } int n1 = str1.size(), n2 = str2.size(); reverse(str1.begin(), str1.end()); reverse(str2.begin(), str2.end()); int carry; carry = 0; for (int i = 0; i < n1; i++) { int sum; sum = ((str1[i] - '0') + (str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } for (int i = n1; i < n2; i++) { int sum; sum = ((str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } if (carry) str.push_back(carry + '0'); reverse(str.begin(), str.end()); return str; } bool mcomp(long long a, long long b, long long c, long long d) { if (a == b and b == c and c == d) return true; else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; std::vector<long long> a(n, 0); for (long long i = 0; i < n; i++) cin >> a[i]; std::vector<long long> diff; long long m = -(long long)1e19; for (long long i = 0; i < n - 1; i++) { diff.push_back(abs(a[i + 1] - a[i])); } std::vector<long long> d1, d2; d1.push_back(0); d2.push_back(0); long long t = 1; for (long long i = 0; i < n - 1; i++) { d1.push_back(t * diff[i]); d2.push_back(t * -1 * diff[i]); t *= -1; m = max(m, diff[i]); } d1 = vsum(d1); d2 = vsum(d2); long long mi = 0; for (long long i = 0; i < n; i++) { if (d1[i] < d1[mi]) mi = i; m = max(m, d1[i] - d1[mi]); } mi = 0; for (long long i = 0; i < n; i++) { if (d2[i] < d2[mi]) mi = i; m = max(m, d2[i] - d2[mi]); } cout << m; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.*; import java.util.StringTokenizer; import static java.lang.Math.max; /** * 788A * ΞΈ(n) time * ΞΈ(n) space * * @author artyom */ public class _788A implements Runnable { private BufferedReader in; private StringTokenizer tok; private long solve() throws IOException { int n = nextInt(), l = n - 1; int[] a = readIntArray(n), d = new int[l]; for (int i = 0; i < l; i++) { d[i] = Math.abs(a[i + 1] - a[i]); if ((i & 1) == 1) { d[i] = -d[i]; } } long max = 0, s = 0; for (int i = 0; i < l; i++) { s = max(0, s + d[i]); max = max(max, s); } s = 0; for (int i = 1; i < l; i++) { s = max(0, s - d[i]); max = max(max, s); } return max; } //-------------------------------------------------------------- public static void main(String[] args) { new _788A().run(); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); tok = null; System.out.print(solve()); in.close(); } catch (IOException e) { System.exit(0); } } private String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } private int nextInt() throws IOException { return Integer.parseInt(nextToken()); } private int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Random; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; import java.util.Vector; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CF407A { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; static class Sort implements Comparable<Sort> { int ind, a; @Override public int compareTo(Sort o) { return a - o.a; } public Sort(int i, int an) { ind = i; a = an; } } public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); solve(); pw.close(); } static int n; static long[] a; private static void solve() throws IOException { n = nextInt(); a = new long [n]; for (int i = 0; i < n; ++i) { a[i] = nextLong(); } for (int i = 0; i < n - 1; ++i) { a[i] = Math.abs(a[i] - a[i + 1]); if (i % 2 == 1) a[i] = -a[i]; } long ans = a[0]; long min = 0; long max = a[0]; long[] sum = new long [n]; sum[0] = a[0]; long c; for (int i = 1; i < n - 1; ++i) { sum[i] = a[i] + sum[i - 1]; c = sum[i] - max; if (c < 0) c = -c; ans = Math.max(c, ans); c = sum[i] - min; if (c < 0) c = -c; ans = Math.max(c, ans); if (sum[i] > max) max = sum[i]; if (sum[i] < min) min = sum[i]; } pw.println(ans); } private static int sumf(int[] fen, int id) { int summ = 0; for (; id >= 0; id = (id & (id + 1)) - 1) summ += fen[id]; return summ; } private static void addf(int[] fen, int id) { for (; id < fen.length; id |= id + 1) fen[id]++; } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) a = list(map(int,input().strip().split())) b = [0]*(n-1) c = [0]*(n-1) for i in range(n-1): if (i)%2 == 0: b[i] = (abs(a[i] - a[i+1])) else: b[i] = (abs(a[i] - a[i+1]))*(-1) for i in range(1,n-1): if (i-1)%2 == 0: c[i] = (abs(a[i] - a[i+1])) else: c[i] = (abs(a[i] - a[i+1]))*(-1) d1 = [-1000000001]*(n-1) d2 = [-1000000001]*(n-1) for i in range(n-1): if i > 0: d1[i] = max(b[i]+d1[i-1],max(0,b[i])) else: d1[i] = max(b[i],0) for i in range(n-1): if i > 0: d2[i] = max(c[i]+d2[i-1],max(0,c[i])) else: d2[i] = max(c[i],0) print(max(max(d1),max(d2))) #print(b) #print(d1) #print(c) #print(d2)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int n; long long dat[100002]; long long dif[100002]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> dat[i]; dif[i] = dat[i] - dat[i - 1]; if (dif[i] < 0) dif[i] *= -1; } long long may = INT_MIN; long long aux = 0; long long par = 0; long long impar = 0; long long sum1 = 0; long long sum2 = 0; for (int i = n; i >= 2; i--) { if (i & 1) { impar += dif[i]; } else par += dif[i]; if (i & 1) { may = max(may, impar - par - sum2); } else may = max(may, par - impar - sum1); sum1 = min(sum1, par - impar); sum2 = min(sum2, impar - par); } cout << may << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; long long num[maxn]; long long sum[maxn]; long long getmaxsum(long long *s, int n) { long long Max = 0; long long ssum = 0; for (int i = 0; i < n; ++i) { ssum += s[i]; Max = max(ssum, Max); if (ssum < 0) ssum = 0; } return Max; } int main() { int n; scanf("%d", &n); int flag = 1; scanf("%I64d", &num[0]); for (int i = 1; i < n; ++i) { scanf("%I64d", &num[i]); sum[i - 1] = flag * abs(num[i] - num[i - 1]); flag *= -1; } long long Max = getmaxsum(sum, n - 1); for (int i = 0; i < n - 1; ++i) sum[i] *= -1; Max = max(Max, getmaxsum(sum, n - 1)); printf("%I64d\n", Max); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) a = list(map(int, input().split())) b = [] for i in range(n - 1): b.append(abs(a[i] - a[i + 1])) mx = -float('inf') now = 0 for i in range(n - 1): now += b[i] * (-1) ** (i % 2) mx = max(mx, now) if now < 0: now = 0 now = 0 for i in range(1, n - 1): now += b[i] * (-1) ** (i % 2 + 1) mx = max(mx, now) if now < 0: now = 0 print(mx)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long int maxSubArraySum(vector<long long int> a, int size) { long long int max_so_far = 0, max_ending_here = 0, pos = 1; for (int i = 0; i < size; i++) { if ((max_ending_here + a[i]) < a[i]) { max_ending_here = a[i]; pos = 1; } else max_ending_here = (max_ending_here + a[i]); if (max_so_far < max_ending_here) max_so_far = max_ending_here; } return max_so_far; } int main() { int n; cin >> n; vector<long long int> a(n), diff1, diff2; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { diff1.push_back(abs(a[i] - a[i + 1])); diff2.push_back(abs(a[i] - a[i + 1])); } for (int i = 0; i < diff1.size(); i += 2) diff1[i] = -diff1[i]; for (int i = 1; i < diff2.size(); i += 2) diff2[i] = -diff2[i]; cout << max(maxSubArraySum(diff1, n - 1), maxSubArraySum(diff2, n - 1)) << endl; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.PrintWriter; public class C_407 { public static int mod = 1000000007; static FastReader scan = new FastReader(); static PrintWriter out = new PrintWriter(System.out); public static void main(String args[]) { /******************* code ***********************/ int n = scan.nextInt(); long arr[] = scan.nextLongArray(n); long diff[] = new long[n - 1]; for (int i = 0; i < n - 1; i++) { diff[i] = Math.abs(arr[i] - arr[i + 1]); } long a[] = new long[n - 1]; long b[] = new long[n - 1]; for (int i = 0; i < n - 1; i++) { if (i % 2 == 0) { a[i] = diff[i]; b[i] = -diff[i]; } else { a[i] = -diff[i]; b[i] = diff[i]; } } out.println(Math.max(max(a, a.length), max(b, b.length))); out.close(); } public static long max(long a[], int size) { long max_so_far = Integer.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } static class Pair implements Comparable<Pair> { int x; int y; Pair(int xx, int yy) { x = xx; y = yy; } public String toString() { return "[x=" + x + ", y=" + y + "]"; } @Override public int compareTo(Pair o) { if (Long.compare(this.y, o.y) != 0) return Integer.compare(this.y, o.y); else return Long.compare(this.x, o.x); } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static long pow(long x, long n, long mod) { long res = 1; x %= mod; while (n > 0) { if (n % 2 == 1) { res = (res * x) % mod; } x = (x * x) % mod; n /= 2; } return res; } /************** Scanner ********************/ static class FastReader { private byte[] buf = new byte[1024]; private int curChar; private int snumChars; public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = System.in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) a = list(map(int, input().split())) b = [] f = 1 for i in range(len(a) - 1): b.append(abs(a[i] - a[i + 1]) * f) f *= -1 max1, max2 = 0, 0 a1, a2 = 0, 0 for i in range(n - 1): if a1 + b[i] > 0: a1 += b[i] else: a1 = 0 max1 = max(a1, max1) if a2 - b[i] > 0: a2 -= b[i] else: a2 = 0 max2 = max(a2, max2) print(max(max1, max2))
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; using pii = pair<long long, long long>; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; vector<long long> b(n); for (long long i = 1; i < n; i++) { b[i] = b[i - 1]; long long cur = abs(a[i - 1] - a[i]); if (i % 2) b[i] += cur; else b[i] -= cur; } long long ev = 0, odd = 1; long long ans = 0; for (long long i = 1; i < n; i++) { ans = max(ans, max(b[i] - b[ev], -b[i] + b[odd])); if (i % 2 == 0 && b[i] - b[ev] <= 0) ev = i; else if (i % 2 == 1 && b[odd] - b[i] <= 0) odd = i; } cout << ans << "\n"; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
def f(l, r, p): if l > r: return 0 return p[r] - p[l - 1] if l % 2 == 1 else -f(l - 1, r, p) + p[l - 1] def main(): read = lambda: tuple(map(int, input().split())) n = read()[0] v = read() p = [0] pv = 0 for i in range(n - 1): cp = abs(v[i] - v[i + 1]) * (-1) ** i pv += cp p += [pv] mxc, mxn = 0, 0 mnc, mnn = 0, 0 for i in range(n): cc, cn = f(1, i, p), f(2, i, p) mxc, mxn = max(mxc, cc - mnc), max(mxn, cn - mnn) mnc, mnn = min(mnc, cc), min(mnn, cn) return max(mxc, mxn) print(main())
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner in=new Scanner(System.in); int n=in.nextInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=in.nextInt(); long diff1[]=new long[n-1]; long diff2[]=new long[n-1]; for(int i=0;i<n-1;i++) { long diff=Math.abs(arr[i]*1l-arr[i+1]*1l); if(i%2==0) { diff1[i]=diff*-1; diff2[i]=diff; } else { diff2[i]=diff*-1; diff1[i]=diff; } } long max=Math.max(max1(diff1),max1(diff2)); System.out.println(max); } static long max1(long a[]) { int size = a.length; long max_so_far = Long.MIN_VALUE, max_ending_here = 0; for (int i = 0; i < size; i++) { max_ending_here = max_ending_here + a[i]; if (max_so_far < max_ending_here) max_so_far = max_ending_here; if (max_ending_here < 0) max_ending_here = 0; } return max_so_far; } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10; const long long INF = numeric_limits<long long>::max(); enum { EVEN, ODD }; int A[MAXN]; long long dp[2][MAXN]; int main() { int N; cin >> N; for (int i = 1; i <= N; ++i) cin >> A[i]; dp[ODD][1] = dp[EVEN][1] = dp[ODD][2] = -INF; dp[EVEN][2] = abs(A[2] - A[1]); long long ans = dp[EVEN][2]; for (int i = 3; i <= N; ++i) { dp[ODD][i] = dp[EVEN][i - 1] - abs(A[i] - A[i - 1]); dp[EVEN][i] = max(dp[ODD][i - 1] + abs(A[i] - A[i - 1]), abs(A[i] - A[i - 1]) * 1ll); ans = max(ans, max(dp[ODD][i], dp[EVEN][i])); } cout << ans << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long int p1(int i) { if (i % 2 == 0) return 1; else return -1; } int main() { long long int n; cin >> n; vector<long long int> a(n + 1, 0), psum(n + 1, -10000000000); for (int i = 1; i < n + 1; i++) scanf("%lld", &a[i]); psum[2] = abs(a[1] - a[2]); for (int i = 3; i <= n; i++) psum[i] = psum[i - 1] + abs(a[i] - a[i - 1]) * p1(i); priority_queue<long long int, vector<long long int>, greater<long long int>> pq1; priority_queue<long long int> pq2; pq1.push(psum[3]); pq2.push(psum[2]); long long int ans = *max_element(psum.begin(), psum.end()); for (int i = 2; i <= n; i++) { long long int anst1 = psum[i] - pq1.top(); long long int anst2 = pq2.top() - psum[i]; ans = max(anst1, ans); ans = max(anst2, ans); if (i >= 4) { if (i % 2 == 1) pq1.push(psum[i]); else pq2.push(psum[i]); } } cout << ans; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#!/usr/bin/python # coding: utf-8 n=int(raw_input()) arr=map(long,raw_input().split(' ')) arr1=[] for i in xrange(n-1): tmp=abs(arr[i+1]-arr[i]) tmp=tmp*((-1)**i) arr1.append(tmp) arr2=[] for i in xrange(n-1): arr2.append(arr1[i]*(-1)) max=current=0 for i in xrange(n-1): current+=arr1[i] if(current<0): current=0 if(max<current): max=current current=0 for i in xrange(n-1): current+=arr2[i] if(current<0): current=0 if(max<current): max=current print max ''' 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: Functions_again.png In 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. Input The first line contains single integer n (2 ≀ n ≀ 10^5) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-10^9 ≀ ai ≀ 10^9) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array. '''
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; int a[maxn]; long long b[maxn], sum[maxn]; long long getMax(int n) { sum[0] = 0; for (int i = 0; i < n; i++) { sum[i + 1] = sum[i] + b[i]; } long long res = 0; int id = 0; for (int i = 1; i <= n; i++) { if (sum[i] - sum[id] <= 0) { id = i; continue; } res = max(res, sum[i] - sum[id]); } return res; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } n--; for (int i = 0; i < n; i++) { b[i] = (long long)abs(a[i + 1] - a[i]); } for (int i = 0; i <= n; i++) { if (i % 2) b[i] = -b[i]; } long long ans = getMax(n); for (int i = 0; i < n; i++) { b[i] = -b[i]; } ans = max(ans, getMax(n)); printf("%I64d\n", ans); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long a[100005]; long long dp[100005][2]; int main() { long long res, n, k; while (cin >> n) { for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n - 1; i++) { a[i] = abs(a[i] - a[i + 1]); } long long ans = 0; for (int i = n - 2; i >= 0; i--) { dp[i][0] = a[i] + dp[i + 1][1]; dp[i][1] = -a[i] + dp[i + 1][0]; dp[i][0] = max(dp[i][0], 0LL); dp[i][1] = max(dp[i][1], 0LL); ans = max(ans, dp[i][0]); ans = max(ans, dp[i][1]); } cout << ans << endl; } return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> const int MAXINT = 2147483640; const long long MAXLL = 9223372036854775800LL; const long long MAXN = 1000000; const double pi = 3.1415926535897932384626433832795; using namespace std; long long ans = -MAXINT, n; long long x1[300000]; long long x2[300000]; void check(long long xx) { long long sum = 0, i; for (long long i = xx; i < n; i++) { sum += x2[i]; ans = max(sum, ans); if (sum < 0) sum = 0; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(time(0)); long long i, j; cin >> n; for (i = 1; i <= n; i++) cin >> x1[i]; for (i = 1; i < n; i++) { x2[i] = abs(x1[i] - x1[i + 1]); if (!(i & 1)) x2[i] *= -1; } check(1); for (i = 2; i < n; i++) { x2[i] = abs(x1[i] - x1[i + 1]); if (i & 1) x2[i] *= -1; } check(2); cout << ans << endl; return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; long long kadane(const vector<long long> &v) { long long ans = 0; long long curr = 0; for (long long n : v) { curr += n; ans = max(ans, curr); if (curr < 0) curr = 0; } return ans; } int main() { ios_base::sync_with_stdio(false); int n; scanf("%d", &n); vector<long long> v(n); for (int i = 0; i < n; i++) scanf("%lld", &v[i]); vector<long long> v2(n - 1), v3(n - 1); int x = -1; for (int i = 0; i + 1 < n; i++) { v2[i] = abs(v[i] - v[i + 1]) * x; v3[i] = v2[i] * -1; x = (x == -1) ? 1 : -1; } printf("%lld\n", max(kadane(v2), kadane(v3))); return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.util.*; import java.io.*; public class C1{ static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]){ FastReader fr=new FastReader(); int n,i; n=fr.nextInt(); long arr[]=new long[n]; for(i=0;i<n;i++){ arr[i]=fr.nextLong(); } long a[]=new long[n-1]; for(i=0;i<n-1;i++){ a[i]=Math.abs(arr[i]-arr[i+1]); } long dp[]=new long[n-1]; if(n==2){ System.out.println(a[0]); return; } dp[n-2]=a[n-2]; dp[n-3]=a[n-3]; for(i=n-4;i>=0;i--){ long first=a[i]; long second=a[i]-a[i+1]+dp[i+2]; dp[i]=Math.max(first, second); } long ans=Long.MIN_VALUE; for(i=0;i<=n-2;i++){ ans=Math.max(ans,dp[i]); } System.out.println(ans); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(input()) a = [*map(int, input().split())] a = [abs(a[i] - a[i + 1]) for i in range(n - 1)] ans = t1 = t2 = 0 for i in a: t1, t2 = max(t2 + i, 0), max(t1 - i, 0) ans = max(ans, t1, t2) print(ans)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import math def getMax(a, n): MAX = -1*pow(10, 9) memoize = [[0 for x in range(2)] for y in range(n)] difference = [0 for x in range(n)] for i in range(n): if i > 0: difference[i] = abs(a[i]-a[i-1]) for i in range(1, n): memoize[i][0] = max(difference[i], memoize[i-1][1] + difference[i]) memoize[i][1] = max(-difference[i], memoize[i-1][0] - difference[i]) MAX = max(memoize[i][1], MAX) MAX = max(memoize[i][0], MAX) return MAX n = int(input()) a = [int(x) for x in input().split()] print(getMax(a, n))
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.Arrays; public class Main{ static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } boolean neg = (c == '-'); if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) { return; } din.close(); } } public static void main(String[] args) throws IOException { // TODO Auto-generated method stub Reader r=new Reader(); int n=r.nextInt(); long ar[]=new long[n]; long a[]=new long[n]; for(int i=0;i<n;i++){ ar[i]=r.nextLong(); } for(int i=0;i<n-1;i++){ a[i]=Math.abs(ar[i]-ar[i+1]); } long maxtill=-1,maxhere=0; int sign=0; for(int i=0;i<n-1;i++){ if(sign==0){ maxhere+=a[i]; if(maxhere>maxtill){maxtill=maxhere;} sign=1; } else {maxhere-=a[i]; if(maxhere<0)maxhere=0; sign=0; } } sign=1; //System.out.println(maxtill); maxhere=0; for(int i=0;i<n-1;i++){ if(sign==0){ maxhere+=a[i]; if(maxhere>maxtill){maxtill=maxhere;} sign=1; } else {maxhere-=a[i]; if(maxhere<0)maxhere=0; sign=0; } } //System.out.println(Arrays.toString(a)); System.out.println(maxtill); } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; public class Main2 { public static void main(String[] args) { FastScanner scanner = new FastScanner(); int n = scanner.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = scanner.nextInt(); int[] diffs = new int[n-1]; for (int i = 0; i < n - 1; i++) diffs[i] = Math.abs(a[i+1] - a[i]); long[][] dp = new long[n][2]; dp[0][0] = diffs[0]; int current = 1; long max = diffs[0]; for (int i = 1; i < diffs.length; i++) { int nw = diffs[i]; dp[i][1] = dp[i - 1][0] - diffs[i]; dp[i][0] = Math.max(dp[i - 1][1] + diffs[i], diffs[i]); max = Math.max(max, Math.max(dp[i][0], dp[i][1])); } System.out.println(max); } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
JAVA
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(raw_input()) ll = list(map(int, raw_input().split())) l = [abs(ll[i] - ll[i + 1]) for i in xrange(n - 1)] d = [[0, 0] for i in xrange(n - 1)] d[n - 2] = [l[n - 2], l[n - 2]] ans = d[n - 2][1] for i in xrange(n - 3, -1, -1): d[i][0] = l[i] - max(0, d[i + 1][1]) d[i][1] = l[i] - min(0, d[i + 1][0]) if d[i][1] > ans: ans = d[i][1] print(ans)
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
# In this template you are not required to write code in main import sys inf = float("inf") sys.setrecursionlimit(1000000) #from cmath import sqrt #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd #from bisect import bisect_left,bisect_right #import numpy as np abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod,MOD=1000000007,998244353 vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def maxSubArraySum(a, size): max_so_far = -inf - 1 max_ending_here = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here): max_so_far = max_ending_here if max_ending_here < 0: max_ending_here = 0 return max_so_far def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() n=int(input()) Arr=get_array() b=[];c=[];d=-1;e=1 for i in range(n-1): b.append(abs(Arr[i]-Arr[i+1])*d) c.append(abs(Arr[i]-Arr[i+1])*e) d=-d;e=-e print(max(maxSubArraySum(b,n-1),maxSubArraySum(c,n-1)))
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n=int(input()) *a,=map(int,input().split()) l=[abs(a[i]-a[i-1]) for i in range(1,n)] ans=0 x=y=0 for i in range(n-1): t=[l[i],-l[i]][i%2] x+=t y-=t ans=max(ans,x,y) x=max(x,0) y=max(y,0) print(ans)
PYTHON3
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
#include <bits/stdc++.h> using namespace std; int n; long long a[110000]; long long b[110000]; long long dp[110000][2]; long long myabs(long long x) { if (x < 0) return -x; else return x; } int main() { while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); for (int i = 1; i < n; i++) b[i] = myabs(a[i - 1] - a[i]); dp[1][0] = -b[1]; dp[1][1] = b[1]; for (int i = 2; i < n; i++) { dp[i][0] = max(-b[i], dp[i - 1][1] - b[i]); dp[i][1] = max(b[i], dp[i - 1][0] + b[i]); } long long ans = max(dp[1][0], dp[1][1]); for (int i = 2; i < n; i++) { ans = max(ans, dp[i][0]); ans = max(ans, dp[i][1]); } printf("%I64d\n", ans); } return 0; }
CPP
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
n = int(raw_input()) l = [int(x) for x in raw_input().split()] l1 = [] l2 = [] def best(ll): ans = ll[0] sm = 0 min_sum = 0 for a in ll: sm += a ans = max(ans, sm - min_sum) min_sum = min(min_sum, sm) return ans for i in xrange(n-1): val = abs(l[i+1]-l[i]) if i % 2 == 0: val = -val l1.append(val) l2.append(-val) ans = best(l1) ans = max(ans, best(l2)) print ans
PYTHON
788_A. Functions again
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: <image> In 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. Input The first line contains single integer n (2 ≀ n ≀ 105) β€” the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≀ ai ≀ 109) β€” the array elements. Output Print the only integer β€” the maximum value of f. Examples Input 5 1 4 2 3 1 Output 3 Input 4 1 5 4 7 Output 6 Note In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5]. In the second case maximal value of f is reachable only on the whole array.
2
7
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; public class Main { public static void main(String[] args) { FastScanner sc = new FastScanner(); int n = sc.nextInt(); long[] a = sc.nextLongList(n); long[] b = new long[n]; b[0] = a[0]; long min = b[0]; long max = b[0]; for (int i = 1; i < n; i ++) { b[i] = b[i - 1] + Math.abs(a[i] - a[i - 1]) * (i % 2 == 1 ? 1 : -1); min = Math.min(min, b[i]); max = Math.max(max, b[i]); } System.out.println(max - min); // f(l, r + 1) = f(l, r) + |a[r - 1] - a[r]|(-1)^(r-l) } } class FastScanner { public static String debug = null; private final InputStream in = System.in; private int ptr = 0; private int buflen = 0; private byte[] buffer = new byte[1024]; private boolean eos = false; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { if (debug != null) { buflen = debug.length(); buffer = debug.getBytes(); debug = ""; eos = true; } else { buflen = in.read(buffer); } } catch (IOException e) { e.printStackTrace(); } if (buflen < 0) { eos = true; return false; } else if (buflen == 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } private void skipUnprintable() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; } public boolean isEOS() { return this.eos; } public boolean hasNext() { skipUnprintable(); return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { return (int) nextLong(); } public long[] nextLongList(int n) { return nextLongTable(1, n)[0]; } public int[] nextIntList(int n) { return nextIntTable(1, n)[0]; } public long[][] nextLongTable(int n, int m) { long[][] ret = new long[n][m]; for (int i = 0; i < n; i ++) { for (int j = 0; j < m; j ++) { ret[i][j] = nextLong(); } } return ret; } public int[][] nextIntTable(int n, int m) { int[][] ret = new int[n][m]; for (int i = 0; i < n; i ++) { for (int j = 0; j < m; j ++) { ret[i][j] = nextInt(); } } return ret; } }
JAVA