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>
using namespace std;
const long long MAXN = 1e5 + 10;
long long s[MAXN], p[MAXN], a[MAXN];
int32_t main() {
ios::sync_with_stdio(false);
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long i = 1; i + 1 <= n; i++) p[i] = abs(a[i] - a[i + 1]);
for (long long i = 1; i + 1 <= n; i++) {
if (i % 2)
s[i] = s[i - 1] + p[i];
else
s[i] = s[i - 1] - p[i];
}
long long Min = 0, ans = 0, Max = 0;
for (long long i = 1; i + 1 <= n; i++) {
ans = max(ans, s[i] - Min);
ans = max(ans, Max - s[i]);
if (i % 2 == 0)
Min = min(Min, s[i]);
else
Max = max(Max, s[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 | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
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[256]; // 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{
Reader in = new Reader();
int n = in.nextInt();
long a[]=new long[n];
for(int i=0;i<n;i++)
a[i]=in.nextLong();
for(int i=0;i<n-1;i++){
a[i]=(long)Math.abs(a[i+1]-a[i]);
if(i%2==1)
a[i]=-a[i];
}
long max=-1;
long max_so_far=0;
for(int i=0;i<n-1;i++){
max_so_far+=a[i];
if(max_so_far<0)
max_so_far=0;
if(max_so_far>max)
max=max_so_far;
}
for(int i=0;i<n-1;i++)
a[i]=-a[i];
max_so_far=0;
for(int i=0;i<n-1;i++){
max_so_far+=a[i];
if(max_so_far<0)
max_so_far=0;
if(max_so_far>max)
max=max_so_far;
}
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 N = 1e5 + 5;
long long arr[N], odd[N], even[N];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &arr[i]);
for (int i = 1; i < n; i++) {
odd[i] = abs(arr[i] - arr[i + 1]) * pow(-1, i - 1);
even[i] = odd[i] * -1;
}
long long mx = 0;
long long cur = 0;
for (int i = 1; i < n; i++) {
cur = max(even[i], even[i] + cur);
mx = max(mx, cur);
}
cur = 0;
for (int i = 1; i < n; i++) {
cur = max(odd[i], odd[i] + cur);
mx = max(mx, cur);
}
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.io.*;
import java.math.*;
import java.util.*;
public class C {
final static int MOD = 1000000007;
public static void main(String[] args) throws Exception {
FastReader in = new FastReader(System.in);
int n = in.nextInt();
long[] a = new long[n], D = new long[n - 1];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
for (int i = 0; i < n - 1; i++) {
D[i] = Math.abs(a[i + 1] - a[i]);
}
if (n == 2) {
System.out.println(D[0]);
return;
}
long max = D[0];
long cur = D[0];
for (int i = 2; i < n - 1; i += 2) {
if (cur - D[i - 1] < 0) {
cur = 0;
} else {
cur -= D[i - 1];
}
cur += D[i];
max = Math.max(max, cur);
}
cur = D[1];
max = Math.max(max, cur);
for (int i = 3; i < n - 1; i += 2) {
if (cur - D[i - 1] < 0) {
cur = 0;
} else {
cur -= D[i - 1];
}
cur += D[i];
max = Math.max(max, cur);
}
System.out.println(max);
}
static class FastReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private 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 peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
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 == ',') {
c = read();
}
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 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 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;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String nextLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String nextLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return nextLine();
} else {
return readLine0();
}
}
public BigInteger nextBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
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;
const bool print = false;
const int MAXN = 1000111;
long long dp[MAXN][2];
long long t[MAXN];
long long ab(long long x) { return x > 0 ? x : -x; }
int main() {
int n;
scanf("%d", &n);
for (int i = (1); i <= (n); i++) scanf("%lld", &t[i]);
long long wyn = 0;
for (int i = (n - 1); i >= (1); i--) {
dp[i][0] = max(dp[i][0], dp[i + 1][1] + ab(t[i + 1] - t[i]));
dp[i][1] = max(dp[i][1], dp[i + 1][0] - ab(t[i + 1] - t[i]));
wyn = max(wyn, dp[i][0]);
}
printf("%lld\n", wyn);
}
| 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.Scanner;
public class Three {
public static void main(String[] args) {
// Scanner in = new Scanner("5\n" + "1 4 2 3 1"); //3
// Scanner in = new Scanner("4\n" + "1 5 4 7"); //6
// Scanner in = new Scanner("2\n" + "1 2");
// Scanner in = new Scanner("50\n" +
// "-5 -9 0 44 -10 37 34 -49 11 -22 -26 44 8 -13 23 -46 34 12 -24 2 -40 -15 -28 38 -40 -42 -42 7 -43 5 2 -11 10 43 9 49 -13 36 2 24 46 50 -15 -26 -6 -6 8 4 -44 -3\n"); // 208
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextLong();
}
long[] first = new long[n - 1];
int sign = 1;
for (int i = 0; i < a.length - 1; i++) {
first[i] = Math.abs(a[i] - a[i + 1]) * sign;
sign *= -1;
}
long[] second = new long[a.length - 2];
sign = 1;
for (int i = 0; i < a.length - 2; i++) {
second[i] = Math.abs(a[i + 1] - a[i + 2]) * sign;
sign *= -1;
}
System.out.println(Math.max(findMax(first), findMax(second)));
}
private static long findMax(long[] array) {
if (array.length == 0) {
return Long.MIN_VALUE;
}
long answer = array[0];
long currentSum = 0;
long minSum = 0;
for (long value : array) {
currentSum += value;
answer = Math.max(answer, currentSum - minSum);
minSum = Math.min(minSum, currentSum);
}
return answer;
}
private static long findMaxNaive(long[] array){
long max = Long.MIN_VALUE;
for (int i = 0; i < array.length; i++) {
for (int j = i; j < array.length; j++) {
max = Math.max(max, Arrays.stream(Arrays.copyOfRange(array, i, j +1)).sum());
}
}
return 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 | // created by ζδΈη₯ιζζ―θ°
import java.io.*;
import java.util.*;
public class scratch_18{
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
// out.append("Case #"+(tt+1)+": ");
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int n= Reader.nextInt();
long arr[]= new long[n];
for (int i = 0; i <n ; i++) {
arr[i]= Reader.nextLong();
}
long dp[][][]=new long[n][3][2]; // dp(i)(j)(k) , j will denote wheather before maximal subarray,during or after , k will denote wheather mult by +1 or -1.
dp[1][1][0]= Math.abs(arr[0]-arr[1]);
dp[1][1][1]= 0;
for (int i = 1; i <n-1 ; i++) {
dp[i+1][0][0]= dp[i][0][0]; // maximal subarray not started
dp[i+1][1][0]= Math.max(dp[i][0][0],dp[i][1][1])+Math.abs(arr[i]-arr[i+1]); // in maximal subarray, and multipled by +1, if previous was there, it had to be negative
dp[i+1][1][1]=dp[i][1][0]+ Math.abs(arr[i]-arr[i+1])*-1; // if it is getting multipled by -1, then the previous must have been in maximal subarray
dp[i+1][2][0]= Math.max(dp[i][1][1],dp[i][1][0]);
}
long max=0;
for (int i = 0; i <n ; i++) {
for (int j = 0; j <3 ; j++) {
for (int k = 0; k <2 ; k++) {
max= Math.max(max,dp[i][j][k]);
}
}
}
out.append(max+"\n");
out.flush();
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 | //package CF;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Stack;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int [] in = new int[n-1];
int f = sc.nextInt();
for (int i = 0; i < n-1; i++) {
int tmp = sc.nextInt();
in[i] = Math.abs(tmp-f);
f = tmp;
}
long ans = -INF;
for (int j = 0; j < 2; j++) {
Stack<In> q = new Stack<In>();
q.add(new In(0, null));
for (int i = j, k = 0; i < in.length; i++, k = 1-k) {
q.add(new In(((k==0?1:-1)*in[i]), q.peek()));
ans = Math.max(ans, q.peek().val-q.peek().min);
}
}
out.println(ans);
out.flush();
out.close();
}
static long INF = (long)1e18;
static class In{
long val, min;
public In(long v, In m){
val = v + (m == null?0:m.val);
min = Math.min((m == null?INF:m.min), val);
}
public String toString(){
return val + " " + min;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader fileReader) {
br = new BufferedReader(fileReader);
}
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 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 | n = int(input())
a = list(map(int, input().split(' ')))
def sign(n):
return [1, -1][n % 2]
b1 = [abs(a[i]-a[i-1]) for i in range(1, len(a))]
b1 = [sign(i)*b1[i] for i in range(len(b1))]
b2 = [-x for x in b1]
max_ending_here = b1[0]
max_so_far = b1[0]
for i, x in enumerate(b1):
if i == 0:
continue
if i % 2 == 1:
max_ending_here += x
else:
if x > max_ending_here + x:
max_ending_here = x
else:
max_ending_here += x
max_so_far = max(max_so_far, max_ending_here)
b1 = b2
max_ending_here = b1[0]
max_so_far2 = b1[0]
for i, x in enumerate(b1):
if i == 0:
continue
if i % 2 == 0:
max_ending_here += x
else:
if x > max_ending_here + x:
max_ending_here = x
else:
max_ending_here += x
max_so_far2 = max(max_so_far2, max_ending_here)
print(max(max_so_far, max_so_far2))
| 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 N = 1e5 * 2 + 10;
long long dp[N][2], n, a[N], s[N], ans = 0;
int32_t main() {
cin >> n;
for (long long i = (1); i <= (n); ++i) cin >> a[i];
for (long long i = (1); i <= (n - 1); ++i) s[i] = abs(a[i] - a[i + 1]);
for (long long i = (1); i <= (n - 1); ++i) {
dp[i][0] = max(dp[i - 1][1] + s[i], s[i]);
dp[i][1] = dp[i - 1][0] - s[i];
ans = max({ans, dp[i][0], dp[i][1]});
}
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 | #include <bits/stdc++.h>
using namespace std;
long long A[100005], diff[100005];
int main() {
int n, i;
long long cur, maxi;
cin >> n;
for (i = 0; i < n; i++) cin >> A[i];
for (i = 0; i < n - 1; i++) {
diff[i] = abs(A[i] - A[i + 1]);
if (i % 2) diff[i] = -diff[i];
}
cur = maxi = diff[0];
for (i = 1; i < n - 1; i++) {
cur = max(cur + diff[i], diff[i]);
maxi = max(maxi, cur);
}
for (i = 0; i < n - 1; i++) diff[i] = -diff[i];
cur = diff[0];
for (i = 1; i < n - 1; i++) {
cur = max(cur + diff[i], diff[i]);
maxi = max(maxi, cur);
}
cout << maxi;
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.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
public class contest1 {
public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int []a=new int[n];
for(int i=0; i<n; i++)
a[i]=scn.nextInt();
ArrayList<Integer> p=new ArrayList<>();
ArrayList<Integer> q=new ArrayList<>();
for(int i=0; i<n-1; i++){
if(i%2 ==1)
p.add(Math.abs(a[i]-a[i+1]));
else
p.add(-Math.abs(a[i]-a[i+1]));
}
for(int i=0; i<n-1; i++){
if(i%2 ==0)
q.add(Math.abs(a[i]-a[i+1]));
else
q.add(-Math.abs(a[i]-a[i+1]));
}
System.out.println(Math.max(kadans(p), kadans(q)));
}
private static long kadans(ArrayList<Integer> p) {
long sum=p.get(0), osum=p.get(0);
for(int i=1; i<p.size(); i++){
if(sum>0)
sum+=p.get(i);
else
sum=p.get(i);
if(sum>osum)
osum=sum;
}
return osum;
}
}
| 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 javax.swing.plaf.synth.SynthOptionPaneUI;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main
{
static int N = 200100;
public static void main(String[] args) throws IOException {
Reader in = new Reader();
int n; n = in.nextInt();
long []f = new long[n];
long []a = new long[n-1];
for(int i=0; i<n; ++i) f[i]=in.nextLong();
for(int i=1; i<n; ++i){
a[i-1]=Math.abs(f[i]-f[i-1]);
}
long []b = new long[n-1];
long []c = new long[n-1];
for(int i=0; i<n-1; ++i){
if(i%2==0){
b[i]=a[i];
c[i]=-a[i];
}
else{
b[i]=-a[i];
c[i]=a[i];
}
}
long MAX=0, sum1=0, sum2=0;
for(int i=0; i<n-1; ++i){
sum1 = Math.max(0, sum1+b[i]);
sum2 = Math.max(0, sum2+c[i]);
MAX = Math.max(MAX, Math.max(sum1, sum2));
}
System.out.println(MAX);
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
final private DataInputStream din;
final 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();
}
}
} | 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())
arr = list(map(int, input().split()))
diff = [abs(arr[i]-arr[i+1]) for i in range(n-1)]
dp = [[0, 0, 0] for _ in range(n-1)]
dp[0][1] = diff[0]
for i in range(1, n-1):
dp[i][0] = max(dp[i-1][0], dp[i-1][1], dp[i-1][2])
dp[i][1] = max(dp[i-1][2]+diff[i], diff[i])
dp[i][2] = dp[i-1][1]-diff[i]
print(max(dp[n-2][0], dp[n-2][1], dp[n-2][2]))
| 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.util.*;
public final class Function {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int n=input.nextInt();
long a=input.nextInt();
long sum=0,min=0,max=0;
long b=0;
for (int i=1;i<n ;i++ ) {
b=input.nextInt();
if (i%2==0) {
sum+=Math.abs(a-b);
}
else{
sum+=Math.abs(a-b)*(-1);
}
max=Math.max(sum,max);
min=Math.min(sum,min);
a=b;
}
System.out.print(max-min);
}
} | 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())
arr = list(map(int,input().split(' ')))
arr.insert(0,0)
diff_arr = []
end_so_far = 0
max_so_far = 0
l = 1
for i in range(2,len(arr)-1):
temp = abs(arr[i]-arr[i+1])
diff_arr.append(temp*(pow((-1),i)))
end_so_far = end_so_far + diff_arr[-1]
if end_so_far < 0:
if i%2 == 0:
i = i + 1
end_so_far = 0
if max_so_far < end_so_far:
max_so_far = end_so_far
even_max = max_so_far
max_so_far = 0
end_so_far = 0
for i in range(1,len(arr)-1):
temp = abs(arr[i]-arr[i+1])
diff_arr.append(temp*(pow((-1),i-1)))
end_so_far = end_so_far + diff_arr[-1]
if end_so_far < 0:
if i%2 == 1:
i = i + 1
end_so_far = 0
if max_so_far < end_so_far:
max_so_far = end_so_far
odd_max = max_so_far
print(max(odd_max,even_max))
| 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.util.*;
import java.io.*;
public class Again{
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Entrada in = new Entrada(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC s = new TaskC();
s.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Entrada in, PrintWriter out) {
int n = in.nextInt();
long[] a = in.readLongArray(n);
long[] diff = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
diff[i] = Math.abs(a[i] - a[i + 1]);
}
long s1 = 0;
long s2 = 0;
long maxs1 = 0;
for (int i = 0; i < n - 1; i++) {
long current = diff[i];
if (i % 2 == 1) {
current = -current;
}
s1 += current;
s2 -= current;
if (s1 < 0) {
s1 = 0;
}
if (s2 < 0) {
s2 = 0;
}
maxs1 = Math.max(maxs1, s1);
maxs1 = Math.max(maxs1, s2);
}
out.println(maxs1);
}
}
static class Entrada {
private static BufferedReader in;
private static StringTokenizer tok;
public Entrada(InputStream in) {
this.in = new BufferedReader(new InputStreamReader(in));
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
public String next() {
try {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
} catch (IOException ex) {
System.err.println("An IOException was caught :" + ex.getMessage());
}
return tok.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 | #include <bits/stdc++.h>
using namespace std;
int n, a[100100];
long long dif[100100], sum1, sum2, maxs;
int main(int argc, char const *argv[]) {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
dif[i - 1] = abs(a[i - 1] - a[i]);
}
sum1 = sum2 = 0, maxs = dif[1];
for (int i = 1; i < n; i++) {
if (i % 2)
sum1 += dif[i], sum2 -= dif[i];
else
sum1 -= dif[i], sum2 += dif[i];
maxs = max(maxs, max(sum1, sum2));
if (sum1 < 0) sum1 = 0;
if (sum2 < 0) sum2 = 0;
}
printf("%lld\n", maxs);
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
n = int(input())
a= list(map(int,input().split()))
def maxSubArraySum(a):
max_so_far = 0
max_ending_here = 0
for i in range(len(a)):
max_ending_here = max_ending_here + a[i]
if max_ending_here < 0:
max_ending_here = 0
# Do not compare for all elements. Compare only
# when max_ending_here > 0
elif (max_so_far < max_ending_here):
max_so_far = max_ending_here
return max_so_far
b = [abs(a[i] - a[i + 1]) * (-1 if i & 1 else 1) for i in range(n - 1)]
res = maxSubArraySum(b)
c = [b[i] * -1 for i in range(n - 1)]
res = max(res, maxSubArraySum(c))
print(res)
# print(sum(dp[14:47]))
# j = 0
# for i in dp:
# s += i
# ans = max(ans,abs(s-smin),abs(s-mx))
# if abs(s-mx) == 208:
# print("max = ",j)
# smin = min(s,smin)
# mx = max(s,mx)
# if mx == 169:
# print(j)
# j+=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 | import java.io.*;
import java.util.*;
import java.lang.*;
public class A {
public static long solve(int[] arr) {
long ans = 0;
long maxCurr = 0;
for(int i:arr) {
maxCurr+=i;
if(maxCurr<0) maxCurr = 0;
ans = Math.max(ans, maxCurr);
}
return ans;
}
public static void main(String[] args) {
FastReader in = new FastReader();
int n = in.nextInt();
int[] arr = new int[n];
long ans = 0;
for(int i=0;i<n;i++) {
arr[i] =in.nextInt();
}
int[] diff = new int[n-1];
int[] odd = new int[n-1];
int[] even = new int[n-1];
for(int i=0;i<n-1;i++) {
diff[i] = Math.abs(arr[i]-arr[i+1]);
ans = Math.max(ans, diff[i]);
odd[i] = diff[i];
even[i] = diff[i];
}
for(int i=0;i<n-1;i++) {
if(i%2==0) even[i] = -even[i];
else odd[i] = -odd[i];
}
System.out.println(Math.max(solve(even), solve(odd)));
}
static final Random random=new Random();
// static void ruffleSort(Pair[] a) {
// int n=a.length;//shuffle, then sort
// for (int i=0; i<n; i++) {
// int oi=random.nextInt(n);
// Pair temp=a[oi];
// a[oi]=a[i]; a[i]=temp;
// }
// Arrays.sort(a);
// }
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(char[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
char temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
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;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
}
//class Pair implements Comparable<Pair>{
// int a;
// int b;
// public Pair(int a, int b) {
// this.a = a;
// this.b = b;
// }
// public int compareTo(Pair o) {
// if(this.a==o.a)
// return this.b - o.b;
// return this.a - o.a;
// }
//} | 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.*;
import java.math.*;
public class code2 {
InputStream is;
PrintWriter out;
void solve()
{
int n=ni();
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=ni();
long temp=0,fmax=Long.MIN_VALUE;
for(int i=1;i<n;i++)
{
if(i%2==1)
temp+=Math.abs(a[i+1]-a[i]);
else
temp-=Math.abs(a[i+1]-a[i]);
if(temp>fmax)
fmax=temp;
if(temp<0)
temp=0;
}
temp=0;
for(int i=1;i<n;i++)
{
if(i%2==0)
temp+=Math.abs(a[i+1]-a[i]);
else
temp-=Math.abs(a[i+1]-a[i]);
if(temp>fmax)
fmax=temp;
if(temp<0)
temp=0;
}
out.println(fmax);
}
static class Pair implements Comparable<Pair>{
int x,y,k,i;
Pair (int x,int y){
this.x=x;
this.y=y;
}
public int compareTo(Pair o) {
return Long.compare(this.i,o.i);
}
public boolean equals(Object o) {
if (o instanceof Pair) {
Pair p = (Pair)o;
return p.x == x && p.y == y && p.k==k;
}
return false;
}
@Override
public String toString() {
return "("+x + " " + y +" "+k+" "+i+" )";
}
}
public static boolean isPal(String s){
for(int i=0, j=s.length()-1;i<=j;i++,j--){
if(s.charAt(i)!=s.charAt(j)) return false;
}
return true;
}
public static String rev(String s){
StringBuilder sb=new StringBuilder(s);
sb.reverse();
return sb.toString();
}
public static long gcd(long x,long y){
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static int gcd(int x,int y){
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static long gcdExtended(long a,long b,long[] x){
if(a==0){
x[0]=0;
x[1]=1;
return b;
}
long[] y=new long[2];
long gcd=gcdExtended(b%a, a, y);
x[0]=y[1]-(b/a)*y[0];
x[1]=y[0];
return gcd;
}
public static int abs(int a,int b){
return (int)Math.abs(a-b);
}
public static long abs(long a,long b){
return (long)Math.abs(a-b);
}
public static int max(int a,int b){
if(a>b)
return a;
else
return b;
}
public static int min(int a,int b){
if(a>b)
return b;
else
return a;
}
public static long max(long a,long b){
if(a>b)
return a;
else
return b;
}
public static long min(long a,long b){
if(a>b)
return b;
else
return a;
}
public static long pow(long n,long p,long m){
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
if(result>=m)
result%=m;
p >>=1;
n*=n;
if(n>=m)
n%=m;
}
return result;
}
public static long pow(long n,long p){
long result = 1;
if(p==0)
return 1;
if (p==1)
return n;
while(p!=0)
{
if(p%2==1)
result *= n;
p >>=1;
n*=n;
}
return result;
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
void run() throws Exception {
is = System.in;
out = new PrintWriter(System.out);
solve();
out.flush();
}
public static void main(String[] args) throws Exception {
new code2().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
} | 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.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=1;
//t=sc.nextInt();
//int t=Integer.parseInt(br.readLine());
while(--t>=0){
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)a[i]=sc.nextInt();
long dp[]=new long[n];
long max=-10000000000000000l;
long min=10000000000000000l;
int minI=0;
int maxI=0;
for(int i=1;i<n;i++){
long abs=Math.abs(a[i]-a[i-1]);
if(i%2!=0)
dp[i]=dp[i-1]+abs;
else
dp[i]=dp[i-1]-abs;
if(dp[i]<=min){
min=dp[i];minI=i;
}
if(dp[i]>=max){
max=dp[i];maxI=i;
}
}
long ans=-10000000000000000l;
for(int i=0;i<maxI;i=i+2){
ans=Math.max(ans,dp[maxI]-dp[i]);
}
for(int i=1;i<minI;i=i+2){
ans=Math.max(ans,-dp[minI]+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 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 9, MOD = 1e9 + 7;
int n;
long long arr[maxN], ot[maxN];
long long maxsub(long long *arr, int n) {
long long cr = LLONG_MIN / 4, maxi = LLONG_MIN / 4;
for (int i = 0; i < n; i++) {
cr = max(arr[i], cr + arr[i]);
maxi = max(maxi, cr);
}
return maxi;
}
long long trr(long long *arr, int sta) {
int ct = 0;
for (int i = sta, cnd = 1; i + 1 < n; i++, cnd *= -1) {
ot[ct++] = abs(arr[i] - arr[i + 1]) * cnd;
}
return maxsub(ot, ct);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cout << max(trr(arr, 0), trr(arr, 1)) << '\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 | #include <bits/stdc++.h>
using namespace std;
int n, a[100005];
long long f[100005][2];
long long ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i < n; i++) {
f[i][0] = max(f[i - 1][1] + abs(a[i] - a[i + 1]),
(long long)abs(a[i] - a[i + 1]));
f[i][1] = max(f[i - 1][0] - abs(a[i] - a[i + 1]), 0ll);
ans = max(ans, max(f[i][0], f[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;
long long maxSubArraySum(vector<long long> a) {
long long siz = a.size();
long long max_so_far = a[0];
long long curr_max = a[0];
for (long long i = 1; i < siz; i++) {
curr_max = max(a[i], curr_max + a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main() {
long long n;
cin >> n;
long long ar[n];
for (long long i = 0; i < n; i++) {
cin >> ar[i];
}
bool t = true;
vector<long long> v1, v2;
for (long long i = 0; i < n - 1; i++) {
if (t) {
v1.push_back(abs(ar[i] - ar[i + 1]));
v2.push_back(-abs(ar[i] - ar[i + 1]));
} else {
v1.push_back(-abs(ar[i] - ar[i + 1]));
v2.push_back(abs(ar[i] - ar[i + 1]));
}
t = !t;
}
cout << max(maxSubArraySum(v1), maxSubArraySum(v2));
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.*;
public class Solution
{
public static void main(String ag[])
{
Scanner sc=new Scanner(System.in);
int i,j,k;
int N=sc.nextInt();
int A[]=new int[N+1];
for(i=1;i<=N;i++)
A[i]=sc.nextInt();
int diff[]=new int[N+1];
for(i=1;i<N;i++)
{
diff[i]=Math.abs(A[i]-A[i+1]);
}
int dpOfOdd[]=new int[N+1];
int dpOfEven[]=new int[N+1];
for(i=1;i<=N;i++)
{
if(i%2==0)
{
dpOfOdd[i]=-diff[i];
dpOfEven[i]=diff[i];
}
else
{
dpOfOdd[i]=diff[i];
dpOfEven[i]=-diff[i];
}
}
long max=0;
long DP1[]=new long[N+1];
long DP2[]=new long[N+1];
for(i=1;i<=N;i++)
{
DP1[i]=Math.max(DP1[i-1]+dpOfOdd[i],dpOfOdd[i]);
DP2[i]=Math.max(DP2[i-1]+dpOfEven[i],dpOfEven[i]);
max=Math.max(max,Math.max(DP1[i],DP2[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 | import java.io.*;
import java.util.*;
public class A {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
long slow(long[] a) {
long ret = 0;
for (int i = 0; i < a.length; i++) {
long sum = 0;
for (int j = i + 1; j < a.length; j++) {
long diff = a[j] - a[j - 1];
if ((j - i) % 2 == 0) {
diff = -diff;
}
sum += diff;
ret = Math.max(ret, sum);
}
}
return ret;
}
long go(long[] a) {
long ret = 0;
long minEven = 0;
long pref = 0;
for (int i = 0; i < a.length; i++) {
pref += (i % 2 == 0 ? a[i] : -a[i]);
ret = Math.max(ret, pref - minEven);
if (i % 2 == 1) {
minEven = Math.min(minEven, pref);
}
}
return ret;
}
long solve(long[] a) throws IOException {
int n = a.length;
long[] b = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
b[i] = Math.abs(a[i] - a[i + 1]);
}
return Math.max(go(b), go(Arrays.copyOfRange(b, 1, n - 1)));
}
void submit() throws IOException {
int n = nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
out.println(solve(a));
}
A() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
submit();
out.close();
}
public static void main(String[] args) throws IOException {
new A();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
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 | import java.util.*;
import java.io.*;
public class FunctAg{
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 s = new TaskC();
s.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] a = in.readLongArray(n);
long[] diff = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
diff[i] = Math.abs(a[i] - a[i + 1]);
}
long s1 = 0;
long s2 = 0;
long maxs1 = 0;
for (int i = 0; i < n - 1; i++) {
long current = diff[i];
if (i % 2 == 1) {
current = -current;
}
s1 += current;
s2 -= current;
if (s1 < 0) {
s1 = 0;
}
if (s2 < 0) {
s2 = 0;
}
maxs1 = Math.max(maxs1, s1);
maxs1 = Math.max(maxs1, s2);
}
out.println(maxs1);
}
}
static class InputReader {
private static BufferedReader in;
private static StringTokenizer tok;
public InputReader(InputStream in) {
this.in = new BufferedReader(new InputStreamReader(in));
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
public String next() {
try {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
} catch (IOException ex) {
System.err.println("An IOException was caught :" + ex.getMessage());
}
return tok.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 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
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;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int[] a = IOUtils.readIntArray(in, n);
long res = Long.MIN_VALUE;
long[] prefix = new long[n];
for (int i = 0; i < n - 1; i++)
prefix[i + 1] = prefix[i] + (i % 2 == 0 ? 1 : -1) * Math.abs(a[i] - a[i + 1]);
long minValue = 0, maxValue = 0;
for (int i = 1; i < prefix.length; i++) {
res = Math.max(res, prefix[i] - minValue);
res = Math.max(res, maxValue - prefix[i]);
minValue = Math.min(minValue, prefix[i]);
maxValue = Math.max(maxValue, prefix[i]);
}
out.printLine(res);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(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 readInt() {
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 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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.readInt();
}
return array;
}
}
}
| 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 f(a):
c, v, m = 0, 0, 1
for i in range(len(a) - 1):
x = m * abs(a[i] - a[i + 1])
c = max(c + x, x)
v = max(v, c)
m *= -1
return v
n, a = int(input()), list(map(int, input().split()))
print(max(f(a), f(a[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 | #include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y, long long int m) {
if (y == 0) return 1;
long long int p = power(x, y / 2, m) % m;
p = (p * p) % m;
return (y % 2 == 0) ? p : (x * p) % m;
}
long long int nCr(long long int n, long long int r, long long int m) {
if (r > n) return 0;
long long int a = 1, b = 1, i;
for (i = 0; i < r; i++) {
a = (a * n) % m;
--n;
}
while (r) {
b = (b * r) % m;
--r;
}
return (a * power(b, m - 2, m)) % m;
}
void solve() {
int n;
cin >> n;
vector<long long int> v(n);
for (int i = 0; i < n; ++i) cin >> v[i];
vector<long long int> ans1, ans2;
for (int i = 0; i < n - 1; ++i) {
if (i % 2 == 0)
ans1.push_back(abs(v[i] - v[i + 1]));
else
ans1.push_back(-1 * (abs(v[i] - v[i + 1])));
if (i > 0) {
if (i % 2)
ans2.push_back(abs(v[i] - v[i + 1]));
else
ans2.push_back(-1 * (abs(v[i] - v[i + 1])));
}
}
long long int ans = 0, temp = 0;
for (int i = 0; i < (int)ans1.size(); ++i) {
temp += ans1[i];
if (temp < 0) temp = 0;
ans = max(temp, ans);
}
temp = 0;
for (int i = 0; i < (int)ans2.size(); ++i) {
temp += ans2[i];
if (temp < 0) temp = 0;
ans = max(temp, ans);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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;
long long v[100005], dp[100005], Max;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> v[i];
for (int i = 1; i <= n - 1; i++) v[i] = abs(v[i] - v[i + 1]);
n--;
for (int i = 1; i <= n; i++) {
dp[i] = v[i];
if (i + 2 >= 3) dp[i] = max(dp[i], v[i] - v[i - 1] + dp[i - 2]);
Max = max(Max, dp[i]);
}
cout << Max;
}
| 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
def solve():
n = int(input())
a = [int(i) for i in input().split()]
a_dif = [abs(a[i + 1] - a[i]) for i in range(n - 1)]
a1 = [a_dif[i] * (-1)**i for i in range(n - 1)]
md1 = 0
m1 = 0
v = 0
for i in range(n - 1):
v += a1[i]
if v < m1:
m1 = v
else:
md1 = max(v - m1, md1)
a2 = [a1[i]*(-1) for i in range(n -1)]
md2 = 0
m2 = 0
v = 0
for i in range(n - 1):
v += a2[i]
if v < m2:
m2 = v
else:
md2 = max(v - m2, md2)
ans = max(md1, md2)
print(ans)
def debug(x, table):
for name, val in table.items():
if x is val:
print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
return None
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;
long long p[100010];
long long Abs(long long n) {
if (n < 0) return -n;
return n;
}
int main() {
int n, fu = 1;
long long now = 0, Max = 0, ans = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%I64d", &p[i]);
for (int i = 1; i < n; i++) {
if (now + Abs(p[i] - p[i + 1]) * fu < 0)
now = 0;
else {
now += Abs(p[i] - p[i + 1]) * fu;
ans = max(ans, now);
}
if (fu == 1)
fu = -1;
else
fu = 1;
}
now = 0, fu = 1;
for (int i = 2; i < n; i++) {
if (now + Abs(p[i] - p[i + 1]) * fu < 0)
now = 0;
else {
now += Abs(p[i] - p[i + 1]) * fu;
ans = max(ans, now);
}
if (fu == 1)
fu = -1;
else
fu = 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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class FunctionsAgain
{
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
int[] diff = new int[n-1];
for (int i = 0; i < arr.length; i++)
arr[i] = sc.nextInt();
for (int i = 1; i < arr.length; i++)
diff[i-1] = Math.abs(arr[i]-arr[i-1]);
for (int i = 1; i < diff.length; i+=2)
diff[i] = -diff[i];
long maxSum = 0;
long sum = 0;
for (int i = 0; i < diff.length; i++)
{
sum = sum + diff[i];
maxSum = Math.max(sum, maxSum);
if(sum < 0)
sum = 0;
}
for (int i = 0; i < diff.length; i++)
diff[i] = -diff[i];
long maxSum2 = 0;
long sum2 = 0;
for (int i = 0; i < diff.length; i++)
{
sum2 = sum2 + diff[i];
maxSum2 = Math.max(sum2, maxSum2);
if(sum2 < 0)
sum2 = 0;
}
System.out.println(Math.max(maxSum2, maxSum));
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(System.in));}
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 | n = input()
arr = map(int,raw_input().strip().split(' '))
a = []
for i in range(1,n):
a.append(abs(arr[i] - arr[i-1]))
dpeven = []
dpodd = []
#dpeven
answer = 0
index = 0
while index < n -1 :
if index % 2 == 0:
answer += a[index]
else:
dpeven.append(answer)
if answer >= a[index]:
answer -= a[index]
else:
answer = 0
dpeven.append(answer)
index += 1
answer = 0
index = 1
while index < n-1:
if index % 2 == 1:
answer += a[index]
else:
dpodd.append(answer)
if answer >= a[index]:
answer -= a[index]
else:
answer = 0
dpodd.append(answer)
index += 1
m = 0
if len(dpodd) > 0:
m = max(dpodd)
#print dpeven
#print dpodd
print max(max(dpeven),m)
| 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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Nguyen Trung Hieu - [email protected]
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int count = in.readInt();
int[] A = IOUtils.readIntArray(in, count);
int[] B = new int[count - 1];
for (int i = 0; i < count - 1; i++) {
B[i] = Math.abs(A[i] - A[i + 1]);
}
long answer = 0;
int sign = 1;
long sum = 0;
for (int i = 0; i < count - 1; i++) {
sum += B[i] * sign;
if (sum < 0) {
sum = 0;
sign = 1;
} else {
answer = Math.max(answer, sum);
sign *= -1;
}
}
sign = 1;
sum = 0;
for (int i = 1; i < count - 1; i++) {
sum += B[i] * sign;
if (sum < 0) {
sum = 0;
sign = 1;
} else {
answer = Math.max(answer, sum);
sign *= -1;
}
}
out.printLine(answer);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(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 readInt() {
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 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 interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
static class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = in.readInt();
}
return array;
}
}
}
| 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=input()
a=map(int,raw_input().split())
d=[]
for i in range(n-1):
x=abs(a[i]-a[i+1])
if i%2==0:
d.append(x)
else:
d.append(-x)
x=0
m=0
for i in range(n-1):
x+=d[i]
if x<0:
x=0
else:
m=max(m,x)
x=0
p=0
for i in range(n-1):
x+=d[i]
if x>0:
x=0
else:
p=min(p,x)
p=-p
print max(p,m)
| 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>
const int MAX = 100005;
int main() {
long long a[MAX], dp[MAX][2];
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n; i++) {
a[i] = abs(a[i] - a[i + 1]);
}
dp[1][1] = a[1];
dp[1][0] = 0;
long long ans = a[1];
for (int i = 2; i < n; i++) {
if (i % 2) {
dp[i][0] = dp[i - 1][0] - a[i];
dp[i][1] =
(((a[i]) > (dp[i - 1][1] + a[i])) ? (a[i]) : (dp[i - 1][1] + a[i]));
ans = (((ans) > ((((dp[i][0]) > (dp[i][1])) ? (dp[i][0]) : (dp[i][1]))))
? (ans)
: ((((dp[i][0]) > (dp[i][1])) ? (dp[i][0]) : (dp[i][1]))));
} else {
dp[i][0] =
(((a[i]) > (dp[i - 1][0] + a[i])) ? (a[i]) : (dp[i - 1][0] + a[i]));
dp[i][1] = dp[i - 1][1] - a[i];
ans = (((ans) > ((((dp[i][0]) > (dp[i][1])) ? (dp[i][0]) : (dp[i][1]))))
? (ans)
: ((((dp[i][0]) > (dp[i][1])) ? (dp[i][0]) : (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 | def main():
n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(len(a) - 1):
b.append(abs(a[i] - a[i + 1]))
max_odd = b[0]
max_even = 0
all_values = [max_odd, max_even]
for bi in b[1:]:
new_odd = max(max_even + bi, bi)
new_even = max(max_odd - bi, 0)
max_odd = new_odd
max_even = new_even
all_values += [max_odd, max_even]
print(max(all_values))
if __name__ == '__main__':
# import sys
# sys.stdin = open("C.txt")
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 n;
long long a[100002], f[100002], g[100002], p[100002], q[100002];
int main() {
long long h, fm = 0, gm = 0, pm = 0, qm = 0;
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int(i) = (0); (i) < (n); i++) {
cin >> a[i];
if (i % 2 == 1 and i > 0) {
f[i - 1] = abs(a[i - 1] - a[i]) * (1);
} else if (i % 2 == 0 and i > 0) {
f[i - 1] = abs(a[i - 1] - a[i]) * (-1);
}
if (i % 2 == 1 and i > 0) {
g[i - 1] = abs(a[i - 1] - a[i]) * (-1);
} else if (i % 2 == 0 and i > 0) {
g[i - 1] = abs(a[i - 1] - a[i]) * (1);
}
}
h = 0;
for (int(i) = (0); (i) < (n - 1); i++) {
if (i % 2 == 0) {
if (h < 0) h = 0;
}
h += f[i];
if (h > fm) fm = h;
}
h = 0;
for (int(i) = (1); (i) < (n - 1); i++) {
if (i % 2 == 1) {
if (h < 0) h = 0;
}
h += g[i];
if (h > gm) gm = h;
}
cout << max(fm, gm);
cout << 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 maxs(vector<long long int> a) {
long long int size = a.size();
long long int max_so_far = 0, max_ending_here = 0;
for (long long int i = 0; i < size; i++) {
max_ending_here = max_ending_here + a[i];
if (max_ending_here < 0)
max_ending_here = 0;
else if (max_so_far < max_ending_here)
max_so_far = max_ending_here;
}
return max_so_far;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n;
cin >> n;
long long int arr[n];
for (long long int i = 0; i < n; i++) cin >> arr[i];
long long int temp[n - 1];
for (long long int i = 0; i < n - 1; i++) temp[i] = abs(arr[i] - arr[i + 1]);
vector<long long int> first, second;
for (long long int i = 0; i < n - 1; i++) {
if (i % 2)
first.push_back(-temp[i]);
else
first.push_back(temp[i]);
}
for (long long int i = 1; i < n - 1; i++) {
if (i % 2)
second.push_back(temp[i]);
else
second.push_back(-temp[i]);
}
long long int ans = max(maxs(first), maxs(second));
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())
nums = list(map(int,raw_input().split()))
arr = [0]
arr2 = [0]
for i in range(n-1):
arr.append(abs(nums[i]-nums[i+1]))
arr2.append(abs(nums[i]-nums[i+1]))
for i in range(1,n):
if i%2:
arr[i] *= -1
else:
arr2[i] *= -1
ans = 0
for i in range(n-1):
arr[i+1] += arr[i]
arr2[i+1] += arr2[i]
last = 0
for i in range(n):
ans = max(ans,arr[i]-last)
if i%2:
last = min(arr[i],last)
last = 0
for i in range(n):
ans = max(ans,arr2[i]-last)
if i%2==0:
last = min(arr2[i],last)
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 | #include <bits/stdc++.h>
using namespace std;
long long n, t, sum1, sum2, ans, a[100001], x, y, b[100001], c[100001];
bool f;
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (long long i = 0; i < n; ++i) cin >> a[i];
for (long long i = 0; i < n - 1; ++i) {
t = abs(a[i + 1] - a[i]);
b[i] = c[i] = t;
if (i & 1)
b[i] *= -1;
else
c[i] *= -1;
}
sum1 = sum2 = -1e18 + 1;
x = y = 0;
for (long long i = 0; i < n - 1; i++) {
x = x + b[i];
y = y + c[i];
if (sum1 < x) sum1 = x;
if (sum2 < y) sum2 = y;
if (x < 0) x = 0;
if (y < 0) y = 0;
}
ans = max(sum1, sum2);
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=input()
arr=map(int,raw_input().strip().split(" "))
diff=[]
for i in range(n-1):
diff.append(abs(arr[i]-arr[i+1]))
ar1,ar2=[],[]
for i in range(len(diff)):
if(i%2==0):
ar1.append(diff[i])
ar2.append(-diff[i])
else:
ar1.append(-diff[i])
ar2.append(diff[i])
m1,maxend,m2=0,0,0
for i in range(len(ar1)):
maxend+=ar1[i]
if(maxend<0):
maxend=0
elif(m1<maxend):
m1=maxend
maxend=0
for i in range(len(ar2)):
maxend+=ar2[i]
if(maxend<0):
maxend=0
elif(m2<maxend):
m2=maxend
print max(m1,m2)
#print diff,ar1,ar2
#print m1,m2
| 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 math
maxi = (10 ** 5) + 5
Xb = [0 for i in range(maxi)]
Xc = [0 for i in range(maxi)]
n = input()
lista = map(int, raw_input().split())
A = [0 for i in range(n+1)]
for i in range(1, n+1):
A[i] = lista[i-1]
a = [0 for i in range(n+1)]
for i in range(1, n):
a[i] = abs(A[i]-A[i+1])
Xc[1] = a[1]
Xb[1] = 0
mirespuesta = a[1]
for i in range(2, n):
if(i % 2):
Xb[i] = Xb[i-1] - a[i]
Xc[i] = max(a[i], Xc[i-1]+a[i])
mirespuesta = max(mirespuesta, max(Xb[i], Xc[i]))
else:
Xb[i] = max(a[i], Xb[i-1]+a[i])
Xc[i] = Xc[i-1] - a[i]
mirespuesta = max(mirespuesta, max(Xb[i], Xc[i]))
print mirespuesta
| 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.util.*;
import java.io.*;
public class a
{
public static void main(String[] arg) throws IOException
{
new a();
}
long[][] memo;
long oo = Long.MIN_VALUE/10;
int n;
long[] vs;
public a() throws IOException
{
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
n = in.nextInt();
long[] temp = new long[n];
for(int i = 0; i < n; i++) temp[i] = in.nextInt();
n--;
vs = new long[n];
for(int i = 0; i < n; i++)
{
vs[i] = Math.abs(temp[i]-temp[i+1]);
}
memo = new long[4][n];
for(int i = 0; i < 4; i++) Arrays.fill(memo[i], oo);
out.println(solve(0, 0));
in.close(); out.close();
}
long solve(int f, int idx)
{
if(idx == n)
{
if(f == 0) return oo+1;
return 0;
}
long ret = memo[f][idx];
if(ret != oo) return ret;
ret = oo+1;
if(f == 0)
{
ret = Math.max(ret, solve(0, idx+1));
ret = Math.max(ret, solve(1, idx+1)+vs[idx]);
}
if(f == 1)
{
ret = Math.max(ret, solve(3, idx+1));
ret = Math.max(ret, solve(2, idx+1)-vs[idx]);
}
if(f == 2)
{
ret = Math.max(ret, solve(3, idx+1));
ret = Math.max(ret, solve(1, idx+1)+vs[idx]);
}
if(f == 3)
{
ret = Math.max(ret, solve(3, idx+1));
}
return memo[f][idx] = ret;
}
class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
public String next() throws IOException
{
while(!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(next());
}
public void close() throws IOException
{
br.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.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
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(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, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = in.readintArray(n), c = new int[n];
for (int i = 0; i < n - 1; i++) {
c[i] = Math.abs(a[i] - a[i + 1]);
}
int[] res1 = new int[n], res2 = new int[n];
for (int i = 0; i < n - 1; i++) {
res1[i] += c[i] * Math.pow(-1, i + 1);
res2[i] += c[i] * Math.pow(-1, i);
}
long res = res1[0], sum = res1[0], ress = res2[0], summ = res2[0];
// out.println();
for (int i = 1; i < n; i++) {
sum = Math.max(sum + res1[i], res1[i]);
res = Math.max(res, sum);
summ = Math.max(summ + res2[i], res2[i]);
ress = Math.max(ress, summ);
}
//out.println();
out.println(Math.max(res, ress));
}
}
static class InputReader {
public byte[] buf = new byte[8000];
public int index;
public int total;
public InputStream in;
public InputReader(InputStream stream) {
in = stream;
}
public int scan() {
if (total == -1)
throw new InputMismatchException();
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (total <= 0)
return -1;
}
return buf[index++];
}
public int nextInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
public int[] readintArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
}
}
| 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.util.stream.*;
import java.io.*;
import java.math.*;
public class Main {
static boolean FROM_FILE = false;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
if (FROM_FILE) {
try {
br = new BufferedReader(new FileReader("input.txt"));
} catch (IOException error) {
}
} else {
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;
}
}
static int max(int... nums) {
int res = Integer.MIN_VALUE;
for (int num: nums) res = Math.max(res, num);
return res;
}
static int min(int... nums) {
int res = Integer.MAX_VALUE;
for (int num: nums) res = Math.min(res, num);
return res;
}
static long max(long... nums) {
long res = Long.MIN_VALUE;
for (long num: nums) res = Math.max(res, num);
return res;
}
static long min(long... nums) {
long res = Long.MAX_VALUE;
for (long num: nums) res = Math.min(res, num);
return res;
}
public static void main(String[] args) {
FastReader fr = new FastReader();
PrintWriter out = null;
if (FROM_FILE) {
try {
out = new PrintWriter(new FileWriter("output.txt"));
} catch (IOException error) {
}
} else {
out = new PrintWriter(new OutputStreamWriter(System.out));
}
new Main().run(fr, out);
out.flush();
out.close();
}
void run(FastReader fr, PrintWriter out) {
int n = fr.nextInt();
long[] nums = new long[n];
for (int i = 0; i < n; i += 1) nums[i] = fr.nextLong();
long[] diff = new long[n - 1];
for (int i = 0; i < n - 1; i += 1) diff[i] = Math.abs(nums[i + 1] - nums[i]);
long res = 0, pos = 0, neg = 0;
for (int i = 0; i < n - 1; i += 1) {
long next_pos = diff[i] + neg, next_neg = -diff[i] + pos;
pos = next_pos; neg = max(0, next_neg);
res = max(res, pos);
}
out.println(res);
}
} | 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 maxSubArraySum(long long a[], long long size) {
long long max_so_far = 0, max_ending_here = 0;
for (long long 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;
}
int main() {
long long n;
cin >> n;
long long temp, l0, r0;
vector<long long> myvect(n, 0);
long long memo[n - 1];
for (long long i = 0; i < n; ++i) {
cin >> myvect[i];
}
long long a = 1;
for (long long i = 0; i < n - 1; ++i) {
memo[i] = abs(myvect[i] - myvect[i + 1]) * a;
a *= -1;
}
long long maxValue = maxSubArraySum(memo, n - 1);
for (long long i = 0; i < n - 1; ++i) {
memo[i] = memo[i] * (-1);
}
long long maxValue2 = maxSubArraySum(memo, n - 1);
cout << max(maxValue, maxValue2);
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;
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[] A = new int[N];
for (int i = 0; i < N; i++) A[i] = in.nextInt();
int[] B = new int[N - 1];
for (int i = 0; i < N - 1; i++) {
B[i] = Math.abs(A[i] - A[i + 1]);
}
long ans = Math.max(find(B, 0), find(B, 1));
out.println(ans);
}
private long find(int[] b, int start) {
if (start >= b.length) return 0;
long sign = -1L;
long cur = b[start];
long ans = cur;
for (int i = start + 1; i < b.length; i++) {
cur = cur + sign * b[i];
if (cur < 0) {
cur = 0;
}
ans = Math.max(ans, cur);
sign *= -1L;
}
return ans;
}
}
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 | from sys import stdin
from math import fabs, copysign
a = map(int, stdin.read().split())
n = a[0]; a = a[1:]
b = list()
for i in range(1, n):
a[i - 1] = int(copysign(a[i] - a[i - 1], ((i & 1) << 1)- 1))
a[n - 1] = 0
ans, tmp, l, r = a[0], a[0], 0, 0
while r < n - 1:
r += 1
tmp += a[r]
while tmp < 0 and l <= r:
tmp -= a[l]
l += 1
ans = max(ans, tmp)
a = map(lambda x: -x, a)
tmp, l, r = a[1], 1, 1
while r < n - 1:
r += 1
tmp += a[r]
while tmp < 0 and l <= r:
tmp -= a[l]
l += 1
ans = max(ans, tmp)
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 | #include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535;
const int MI = 2147483647;
const int mod = 1e9 + 7;
const int Max = 2e5 + 25;
long long a[Max];
long long dp[Max][2];
int main() {
cin.sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
long long MAX = -MI;
for (int i = 2; i <= n; i++) {
dp[i][0] = max(dp[i - 1][1], 0ll) + abs(a[i] - a[i - 1]);
dp[i][1] = max(dp[i - 1][0], 0ll) - abs(a[i] - a[i - 1]);
MAX = max(max(dp[i][0], dp[i][1]), MAX);
}
cout << MAX << 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;
int main() {
long long i, n;
cin >> n;
long long a[n], b[n - 1], c[n - 1];
for (i = 0; i < n; i++) {
cin >> a[i];
if (i > 0) {
if (i % 2 != 0) {
b[i - 1] = abs(a[i] - a[i - 1]);
c[i - 1] = -b[i - 1];
} else {
b[i - 1] = -abs(a[i] - a[i - 1]);
c[i - 1] = -b[i - 1];
}
}
}
n--;
long long mx = 0, cur = 0;
for (i = 0; i < n; i++) {
cur = cur + b[i];
if (cur < 0) {
cur = 0;
} else {
mx = max(mx, cur);
}
}
cur = 0;
for (i = 1; i < n; i++) {
cur = cur + c[i];
if (cur < 0) {
cur = 0;
} else {
mx = max(mx, cur);
}
}
cout << mx;
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 A[500005];
int main() {
int n, a;
scanf("%d", &n);
for (int i = 1; i < n + 1; i++) {
scanf("%d", &A[i]);
}
priority_queue<long long> pq1;
priority_queue<long long> pq2;
long long sum1 = 0;
long long sum2 = 0;
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
sum2 += abs(A[i] - A[i + 1]);
sum1 -= abs(A[i] - A[i + 1]);
} else {
sum2 -= abs(A[i] - A[i + 1]);
sum1 += abs(A[i] - A[i + 1]);
}
pq1.push(sum1);
pq2.push(sum2);
}
long long add1 = 0;
long long add2 = 0;
long long ans = -LLONG_MAX;
for (int i = 1; i < n; i++) {
if (i % 2) {
ans = max(ans, pq1.top() + add1);
add1 -= abs(A[i] - A[i + 1]);
add2 += abs(A[i] - A[i + 1]);
} else {
ans = max(ans, pq2.top() + add2);
add1 += abs(A[i] - A[i + 1]);
add2 -= abs(A[i] - A[i + 1]);
}
}
while (!pq1.empty()) pq1.pop();
while (!pq2.empty()) pq2.pop();
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 int N = 1e6;
long long dp1[N], dp2[N];
int main() {
int n;
scanf("%d", &n);
long long a[n];
for (int i = 0; i < n; i++) scanf("%lld", &a[i]);
dp1[n - 1] = dp1[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) dp1[i] = abs(a[i] - a[i + 1]);
for (int i = 0; i < n - 1; i += 2) dp1[i] = -dp1[i];
for (int i = 0; i < n - 1; i++) dp2[i] = -dp1[i];
long long ans = -INT_MAX;
long long sum1 = 0, sum2 = 0;
for (int i = 0; i < n - 1; i++) {
sum1 = max(sum1 + dp1[i], dp1[i]);
sum2 = max(sum2 + dp2[i], dp2[i]);
ans = max(ans, max(sum1, sum2));
}
printf("%lld\n", 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>
using namespace std;
int main() {
int n, input[100000];
long long int a = 0, b = 0, maxnum = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input[i];
if (i > 0) {
if (i % 2 == 1) {
a = max(a + abs(input[i - 1] - input[i]), (long long int)0);
b = max(b + abs(input[i - 1] - input[i]) * (-1), (long long int)0);
maxnum = max(a, maxnum);
} else {
a = max(a + abs(input[i - 1] - input[i]) * (-1), (long long int)0);
b = max(b + abs(input[i - 1] - input[i]), (long long int)0);
maxnum = max(b, maxnum);
}
}
}
cout << maxnum << 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 A{
public static void main(String args[]){
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();
}
int cum[] = new int[n-1];
for(int i=0;i<n-1;i++){
cum[i] = Math.abs(arr[i]-arr[i+1]);
}
long sum=0;
long max=0;
for(int i=0;i<n-1;i++){
if(i%2==0)
sum+=cum[i];
else
sum-=cum[i];
if(sum<0)
sum=0;
if(max<sum)
max = sum;
}
sum=0;
for(int i=1;i<n-1;i++){
if(i%2==1)
sum+=cum[i];
else
sum-=cum[i];
if(sum<0)
sum=0;
if(max<sum)
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 |
import java.util.*;
import java.io.*;
public class A
{
public static void main(String[] args) throws Exception
{
PrintWriter out = new PrintWriter(System.out);
new A(new FastScanner(System.in), out);
out.close();
}
public A(FastScanner in, PrintWriter out)
{
int N = in.nextInt();
int[] vs = new int[N];
for (int i=0; i<N; i++)
vs[i] = in.nextInt();
int[] delta = new int[N-1];
for (int i=1; i<N; i++)
delta[i-1] = Math.abs(vs[i]-vs[i-1]);
N--;
for (int i=0; i<N; i+=2)
delta[i] = -delta[i];
long res = Long.MIN_VALUE;
long bestSoFar = Integer.MIN_VALUE;
for (int i=0; i<N; i++)
{
if (bestSoFar < 0)
bestSoFar = 0;
bestSoFar += delta[i];
res = Math.max(res, bestSoFar);
}
for (int i=0; i<N; i++)
delta[i] = -delta[i];
bestSoFar = Integer.MIN_VALUE;
for (int i=0; i<N; i++)
{
if (bestSoFar < 0)
bestSoFar = 0;
bestSoFar += delta[i];
res = Math.max(res, bestSoFar);
}
out.println(res);
}
}
class FastScanner{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream)
{
this.stream = stream;
}
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++];
}
boolean isSpaceChar(int c)
{
return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
}
boolean isEndline(int c)
{
return c=='\n'||c=='\r'||c==-1;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String next(){
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do{
res.appendCodePoint(c);
c = read();
}while(!isSpaceChar(c));
return res.toString();
}
String nextLine(){
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do{
res.appendCodePoint(c);
c = read();
}while(!isEndline(c));
return res.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 | 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;
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];
for (int i = 0; i < n; i++)
arr[i] = in.nextInt();
long[] first = new long[n - 1];
first[0] = Math.abs(arr[1] - arr[0]);
for (int i = 1; i < n - 1; i++) {
if (i % 2 == 1)
first[i] = -Math.abs(arr[i] - arr[i + 1]);
else
first[i] = Math.abs(arr[i] - arr[i + 1]);
}
if (n <= 2) {
long answer = check(first);
out.println(answer);
return;
}
long[] second = new long[n - 2];
second[0] = Math.abs(arr[2] - arr[1]);
for (int i = 1; i < n - 2; i++) {
if (i % 2 == 1)
second[i] = -Math.abs(arr[i + 2] - arr[i + 1]);
else
second[i] = Math.abs(arr[i + 2] - arr[i + 1]);
}
long fs = check(first);
long se = check(second);
out.println(Math.max(fs, se));
}
static long check(long[] arr) {
long max = Long.MIN_VALUE;
long here = 0;
int len = arr.length;
for (int i = 0; i < len; i++) {
here += arr[i];
if (here > max)
max = here;
if (here < 0)
here = 0;
}
return max;
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException();
}
}
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 po(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) {
res = (res * a) % 1000000007;
}
a = (a * a) % 1000000007;
b = b / 2;
}
return res % 1000000007;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
{
long long n;
cin >> n;
long long arr[n];
long long ans = 0;
vector<long long> res1, res2;
for (long long i = 0; i < n; i++) cin >> arr[i];
for (long long i = 0; i < n - 1; i++)
res1.push_back(abs(arr[i] - arr[i + 1]));
res2 = res1;
for (long long i = 0; i < n - 1; i++) {
if (i % 2)
res1[i] = -res1[i];
else
res2[i] = -res2[i];
}
long long sum = 0;
for (auto i : res1) {
sum += i;
if (sum < 0) sum = 0;
ans = max(ans, sum);
}
sum = 0;
for (auto i : res2) {
sum += i;
if (sum < 0) sum = 0;
ans = max(ans, sum);
}
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.*;
import java.math.*;
public class cb {
InputStream is;
PrintWriter out;
static long mod=(long)(1e7+9);
static long mx=Integer.MIN_VALUE;
static long maxSubArraySum(long a[])
{
int size = a.length;
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;
}
void solve()
{
int n=ni();long a[]=new long[n];
long pre[]=new long[n-1];
long pr[]=new long[n-1];
for(int i=0;i<n;i++)
a[i]=nl();
for(int i=0;i<n-1;i++){
pre[i]=Math.abs(a[i]-a[i+1]);
if(i%2!=0)
pre[i]=-pre[i];
pr[i]=-pre[i];
}
out.println(Math.max(maxSubArraySum(pre),maxSubArraySum(pr)));
}
static class pair
{
int k,v;
public pair(int k,int v)
{
this.k=k;
this.v=v;
}
}
public static long [][] multiply(long a[][],long b[][])
{
long mul[][]=new long[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
mul[i][j]=0;
for(int k=0;k<2;k++)
mul[i][j]=(mul[i][j]+(a[i][k]*b[k][j]))%mod;
}
}
return mul;
}
/* Matrix Exponention*/
public static long [][] power(long [][] mat,int n)
{
long res[][]=new long[2][2];
res[0][0]=1;res[1][1]=1;
while(n>0)
{
if(n%2==1)
res=multiply(res,mat);
mat=multiply(mat,mat);
n/=2;
}
return res;
}
//---------- I/O Template ----------
public static void main(String[] args) { new cb().run(); }
void run() {
is = System.in;
out = new PrintWriter(System.out);
solve();
out.flush();
}
byte input[] = new byte[1024];
int len = 0, ptr = 0;
int readByte() {
if(ptr >= len) { ptr = 0;
try { len = is.read(input); }
catch(IOException e) { throw new InputMismatchException(); }
if(len <= 0) { return -1; }
} return input[ptr++];
}
boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
int skip() {
int b = readByte();
while(b != -1 && isSpaceChar(b)) { b = readByte(); }
return b;
}
char nc() { return (char)skip(); }
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
String nLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
int ni() {
int n = 0, b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
if(b == -1) { return -1; } //no input
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
long nl() {
long n = 0L; int b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
double nd() { return Double.parseDouble(ns()); }
float nf() { return Float.parseFloat(ns()); }
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) { a[i] = ni(); }
return a;
}
char[] ns(int n) {
char c[] = new char[n];
int i, b = skip();
for(i = 0; i < n; i++) {
if(isSpaceChar(b)) { break; }
c[i] = (char)b; b = readByte();
} return i == n ? c : Arrays.copyOf(c,i);
}
} | 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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n;
cin >> n;
long long int* arr = new long long int[n];
for (int i = 0; i < n; i++) cin >> arr[i];
long long mx = 0, mn = 0, res = 0, a, s = 0;
for (int i = 1; i < n; i++) {
a = abs(arr[i] - arr[i - 1]);
if (i % 2 == 1) {
s += a;
mx = max(mx, s);
} else {
s -= a;
mn = min(mn, s);
}
res = max(res, mx - mn);
}
cout << 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 | import java.io.*;
import java.util.*;
public class C
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
long[] nums = new long[n];
for(int i = 0; i < n; i++)
nums[i] = Integer.parseInt(st.nextToken());
long[] dif = new long[n-1];
for(int i = 0; i < n-1; i++)
dif[i] = Math.abs(nums[i] - nums[i+1]);
long[] eNeg = new long[n-1], oNeg = new long[n-1];
for(int i = 0; i < n-1; i++)
eNeg[i] = dif[i] * (i%2 == 0 ? -1 : 1);
for(int i = 0; i < n-1; i++)
oNeg[i] = dif[i] * (i%2 == 1 ? -1 : 1);
long ans1 = 0, run = 0;
for(int i = 0; i < n-1; i++)
{
run = Math.max(run + eNeg[i], 0);
ans1 = Math.max(ans1, run);
}
run = 0;
long ans2 = 0;
for(int i = 0; i < n-1; i++)
{
run = Math.max(run + oNeg[i], 0);
ans2 = Math.max(ans2, run);
}
pw.println(Math.max(ans1, ans2));
br.close();
pw.close();
System.exit(0);
}
} | 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.util.StringTokenizer;
public class Main {
static int INF = (int) 2e9;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
int[] in = sc.nextIntArray(n);
n--;
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = Math.abs(in[i] - in[i + 1]) * (i % 2 == 0 ? 1 : -1);
long ans = Kodane(a);
for (int i = 0; i < n; i++)
a[i] = -1 * a[i];
ans = Math.max(ans, Kodane(a));
out.println(ans);
out.flush();
out.close();
}
static long Kodane(int[] a) {
long ans = a[0];
long sum = a[0];
for (int i = 1; i < a.length; i++) {
sum = Math.max(a[i], sum + a[i]);
ans = Math.max(ans, sum);
}
return ans;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] ans = new double[n];
for (int i = 0; i < n; i++)
ans[i] = nextDouble();
return ans;
}
public short nextShort() throws IOException {
return Short.parseShort(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 | /*
* Author- Priyam Vora
* BTech 2nd Year DAIICT
*/
import java.awt.Point;
import java.io.*;
import java.math.*;
import java.util.*;
import javax.print.attribute.SetOfIntegerSyntax;
public class Main{
private static InputStream stream;
private static byte[] buf = new byte[1024];
private static int curChar;
private static int numChars;
private static SpaceCharFilter filter;
private static PrintWriter pw;
private static long count = 0,mod=1000000007;
// private static TreeSet<Integer> ts=new TreeSet[200000];
public static void main(String[] args) {
InputReader(System.in);
pw = new PrintWriter(System.out);
new Thread(null ,new Runnable(){
public void run(){
try{
soln2();
pw.close();
} catch(Exception e){
e.printStackTrace();
}
}
},"1",1<<26).start();
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd( y % x,x);
} public static boolean isPrime(int n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static LinkedList<Integer> adj[];
static boolean Visited[];
static long color[];
static long dist[][];
static long no_sp_no_sub[];
static long ans=0;
private static void soln2(){
int n=nextInt();
long a[]=nextLongArray(n);
long diff[]=new long [n-1];
long diff2[]=new long [n-1];
for(int i=1;i<n;i++){
diff[i-1]=Math.abs(a[i]-a[i-1]);
diff2[i-1]=diff[i-1];
}
for(int i=0;i<n-1;i++){
if(i%2==0){
diff2[i]=-diff2[i];
}else{
diff[i]=-diff[i];
}
}
pw.println(Math.max(maxSubarraySum(diff), maxSubarraySum(diff2)));
}
private static long maxSubarraySum(long a[]){
long max_so_far=Long.MIN_VALUE;
long max_ending_here=0;
for(int i=0;i<a.length;i++){
max_ending_here+=a[i];
if(max_ending_here>max_so_far)
max_so_far=max_ending_here;
if(max_ending_here<0)
max_ending_here=0;
}
return max_so_far;
}
private static int dfs(int x){
Visited[x]=true;
for(int i:adj[x]){
if(!Visited[i]){
dfs(i);
}
}
return x;
}
private static void buildgraph(int n){
adj=new LinkedList[n+1];
Visited=new boolean[n+1];
color=new long[n+1];
no_sp_no_sub=new long[n+1];
ans=0;
Arrays.fill(color, 0);
for(int i=0;i<=n;i++){
adj[i]=new LinkedList<Integer>();
}
}
// To Get Input
// Some Buffer Methods
public static void InputReader(InputStream stream1) {
stream = stream1;
}
private static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private static boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
private static 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++];
}
private static 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;
}
private static 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;
}
private static String nextToken() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private static 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();
}
private static int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
private static int[][] next2dArray(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
private static char[][] nextCharArray(int n,int m){
char [][]c=new char[n][m];
for(int i=0;i<n;i++){
String s=nextLine();
for(int j=0;j<s.length();j++){
c[i][j]=s.charAt(j);
}
}
return c;
}
private static long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private static void pArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
private static void pArray(long[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
private static void pArray(boolean[] arr) {
for (int i = 0; i < arr.length; i++) {
pw.print(arr[i] + " ");
}
pw.println();
return;
}
private static boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class Node{
int to;
long dist;
Node(int to,long dist){
this.to=to;
this.dist=dist;
}
}
class Pair implements Comparable<Pair>{
int city,pop;
Pair(int city,int pop){
this.city=city;
this.pop=pop;
}
@Override
public int compareTo(Pair o) {
return pop-this.pop;
}
public int hashCode() {
int hu = (int) (city ^ (city >>> 32));
int hv = (int) (pop ^ (pop >>> 32));
//int hw = (int) (mass ^ (mass >>> 32));
return 31 * hu + hv ;
//return 0;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return city == other.city && pop == other.pop;
}
} class Dsu{
private int rank[], parent[] ,n;
private static int[] parent1;
Dsu(int size){
this.n=size+1;
rank=new int[n];
//parent=new int[n];
parent=new int[n];
makeSet();
}
void makeSet(){
for(int i=0;i<n;i++){
parent[i]=i;
}
}
int find(int x){
if(parent[x]!=x){
parent[x]=find(parent[x]);
}
return parent[x];
}
boolean union(int x,int y){
int xRoot=find(x);
int yRoot=find(y);
if(xRoot==yRoot)
return false;
if(rank[xRoot]<rank[yRoot]){
parent[xRoot]=yRoot;
}else if(rank[yRoot]<rank[xRoot]){
parent[yRoot]=xRoot;
}else{
parent[yRoot]=xRoot;
rank[xRoot]++;
}
return true;
}
} | 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.lang.*;
import java.math.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class codeforces implements Runnable
{
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(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 String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
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 double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
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 boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new codeforces(),"codeforces",1<<26).start();
}
public void run() {
InputReader s = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = s.nextInt();
long[] temp = new long[n];
long[] a = new long[n - 1];
long[] b = new long[n - 1];
for(int i = 0; i < n; i++)
temp[i] = s.nextInt();
int f = 1;
for(int i = 0; i < n - 1; i++) {
a[i] = f * Math.abs(temp[i + 1] - temp[i]); f = -f;
}
f = -1;
for(int i = 0; i < n - 1; i++) {
b[i] = f * Math.abs(temp[i + 1] - temp[i]); f = -f;
}
long max1 = 0;
long maxEndingHere = 0;
for(int i = 0; i < n - 1; i++) {
maxEndingHere = Math.max(maxEndingHere + a[i], a[i]);
max1 = Math.max(maxEndingHere, max1);
}
long max2 = 0;
maxEndingHere = 0;
for(int i = 0; i < n - 1; i++) {
maxEndingHere = Math.max(maxEndingHere + b[i], b[i]);
max2 = Math.max(maxEndingHere, max2);
}
w.println(Math.max(max1, max2));
w.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.io.*;
import java.util.*;
public class again
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().trim().split(" ");
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=Integer.parseInt(s[i]);
}
int arr1[]=new int[n-1];
int arr2[]=new int[n-1];
long curr1=arr1[0],curr2=arr2[0],max1=0,max2=0;
for(int i=0;i<n-1;i++)
{
if(i%2==0)
{
arr1[i]=Math.abs(arr[i]-arr[i+1]);
arr2[i]=-Math.abs(arr[i]-arr[i+1]);
}
else
{
arr1[i]=-Math.abs(arr[i]-arr[i+1]);
arr2[i]=Math.abs(arr[i]-arr[i+1]);
}
curr1=(long)Math.max(arr1[i],curr1+arr1[i]);
curr2=(long)Math.max(arr2[i],curr2+arr2[i]);
max1=(long)Math.max(max1,curr1);
max2=(long)Math.max(curr2,max2);
}
System.out.println((long)Math.max(max1,max2));
}
} | 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 ar1[100000 + 200], ar2[100000 + 200], ar3[100000 + 200];
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) cin >> ar1[i];
for (long long i = 1; i < n; i++) {
if (i % 2 == 1) {
ar2[i] = abs(ar1[i] - ar1[i + 1]);
ar3[i] = abs(ar1[i] - ar1[i + 1]) * (-1);
} else {
ar2[i] = abs(ar1[i] - ar1[i + 1]) * (-1);
ar3[i] = abs(ar1[i] - ar1[i + 1]);
}
}
long long sum1 = 0, p1 = 0;
for (long long j = 1; j < n; j++) {
sum1 = max(ar2[j], sum1 + ar2[j]);
p1 = max(sum1, p1);
}
long long sum2 = 0, p2 = 0;
for (long long j = 1; j < n; j++) {
sum2 = max(ar3[j], sum2 + ar3[j]);
p2 = max(sum2, p2);
}
cout << max(p1, p2) << 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 N = 2e5 + 5;
long long int a[N], diff[N], n, cache[N][2];
long long int dp(long long int i, long long int sign) {
if (i >= n) {
return 0;
}
long long int &ans = cache[i][sign];
if (ans != -1) {
return ans;
}
ans = diff[i];
if (sign == 1) {
ans *= -1;
}
if (sign == 0) {
ans = max(ans, diff[i] + dp(i + 1, 1));
} else {
ans = max(ans, -diff[i] + dp(i + 1, 0));
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(cache, -1, sizeof(cache));
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
n--;
for (int i = 0; i < n; i++) {
diff[i] = abs(a[i] - a[i + 1]);
}
long long int ans = 0;
for (int i = 0; i < n + 1; i++) {
ans = max(ans, dp(i, 0));
}
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 | #include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long res = 1;
while (b > 0) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
long long int binomial(long long int n, long long int k) {
long long int C[k + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (long long int i = 1; i <= n; i++) {
for (int j = min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1];
}
return C[k];
}
vector<int> pr;
bool prime[10000000];
void Sieve(long long int n) {
memset(prime, true, sizeof(prime));
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
pr.push_back(p);
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
pair<int, int> fib(int n) {
if (n == 0) return {0, 1};
auto p = fib(n >> 1);
int c = p.first * (2 * p.second - p.first);
int d = p.first * p.first + p.second * p.second;
if (n & 1)
return {d, c + d};
else
return {c, d};
}
void reverse(string& str) {
int n = str.length();
for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]);
}
int sum(int n) {
if (n == 0) return 0;
return (n % 10 + sum(n / 10));
}
int main() {
long long int n;
cin >> n;
long long int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
long long int s1 = 0, s2 = 0;
long long int st = 1;
long long int m1 = 0, m2 = 0;
for (int i = 0; i < n - 1; i++) {
s1 += (st) * (abs(a[i] - a[i + 1]));
s2 += (-st) * (abs(a[i] - a[i + 1]));
st *= -1;
if (s1 < 0) {
s1 = 0;
}
if (s2 < 0) {
s2 = 0;
}
m1 = max(m1, s1);
m2 = max(m2, s2);
}
cout << max(m1, m2) << 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 sys
import inspect
import re
import math
from collections import defaultdict
from pprint import pprint as pi
MOD = 10e9 + 7 #998244353
INF = 10**12
n = int(input())
a = list(map(int,input().split()))
d = []
for i in range(1,n):
d.append(abs(a[i]-a[i-1]))
n-=1
sum=0
cur=0
for i in range(n):
cur += (((i+1)%2) - (i%2))*d[i]
if (cur<0): cur=0
if (cur>sum): sum=cur
cur=0
for i in range(1,n):
cur += ((i%2) - ((i+1)%2))*d[i]
if (cur<0): cur=0
if (cur>sum): sum=cur
pi(sum) | 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 N, x[600000], y[600000], best, tmp, mins, idx;
int main() {
cin >> N;
for (int i = 1; i <= N; i++) cin >> y[i], x[i - 1] = abs(y[i - 1] - y[i]);
best = 0, idx = 1;
while (idx <= N - 1) {
tmp = x[idx];
best = max(best, tmp);
mins = 1;
while (idx <= N - 1) {
idx++;
if (mins & 1)
tmp -= x[idx];
else
tmp += x[idx], best = max(best, tmp);
if (tmp < 0) {
idx++;
break;
}
mins++;
}
}
idx = 2;
while (idx <= N - 1) {
tmp = x[idx];
best = max(best, tmp);
mins = 1;
while (idx <= N - 1) {
idx++;
if (mins & 1)
tmp -= x[idx];
else
tmp += x[idx], best = max(best, tmp);
if (tmp < 0) {
idx++;
break;
}
mins++;
}
}
cout << best << 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 binpow(long long base, long long exp, int mod) {
long long res = 1;
while (exp > 0) {
if (exp % 2 == 1) res = (res * base) % mod;
exp = exp >> 1;
base = (base * base) % mod;
}
return res;
}
long long mod(long long x) {
return ((x % 1000000007LL + 1000000007LL) % 1000000007LL);
}
long long add(long long a, long long b) { return mod(mod(a) + mod(b)); }
long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
long long int n;
cin >> n;
vector<long long int> v, ans;
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
v.push_back(x);
};
for (int i = 1; i < n; i++) {
ans.push_back(abs(v[i] - v[i - 1]));
}
long long int final = (-1) * (1e9 + 1);
long long int i = 0, j = 1;
final = ans[i];
long long int sum = ans[i];
for (; j < ans.size(); j++) {
((j - i) % 2 == 0) ? (sum += ans[j]) : (sum -= ans[j]);
if (sum > final) {
final = sum;
}
if (sum < 0) {
i = j + 1;
sum = 0;
}
}
if (n != 2) {
i = 1, j = 2;
long long int final2 = ans[i];
sum = ans[i];
for (; j < ans.size(); j++) {
((j - i) % 2 == 0) ? (sum += ans[j]) : (sum -= ans[j]);
if (sum > final2) {
final2 = sum;
}
if (sum < 0) {
i = j + 1;
sum = 0;
}
}
final = max(final, final2);
}
cout << final << "\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 | #include <bits/stdc++.h>
using namespace std;
long long maxSubArraySum(vector<long long> a) {
long long size = a.size();
if (size == 0) return -1000000000;
long long max_so_far = a[0];
long long curr_max = a[0];
for (long long i = 1; i < size; i++) {
curr_max = max(a[i], curr_max + a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main(void) {
long long i, n;
cin >> n;
long long arr[n];
for (i = 0; i < n; i++) cin >> arr[i];
vector<long long> a1, a2;
for (i = 0; i < n - 1; i++)
a1.push_back(abs(arr[i] - arr[i + 1]) * pow(-1, i));
for (i = 1; i < n - 1; i++)
a2.push_back(abs(arr[i] - arr[i + 1]) * pow(-1, i - 1));
cout << max(maxSubArraySum(a1), maxSubArraySum(a2)) << 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()))
ls=[abs(arr[i+1]-arr[i])*(-1)**(i%2) for i in range(n-1)]
ls1=[abs(arr[i+1]-arr[i])*(-1)**(1-i%2) for i in range(n-1)]
su=0
s=0
for i in ls:
su+=i
if su<0:
su=0
else:
s=max(s,su)
su=0
for j in ls1:
su+=j
if su<0:
su=0
else:
s=max(su,s)
print(s) | 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 p[100005], x[100005];
long long dp[100005];
int main() {
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= n - 1; i++) {
x[i] = abs(p[i] - p[i + 1]);
if (i % 2 == 0) x[i] *= -1;
}
memset(dp, 0, sizeof(dp));
long long ans = 0;
dp[1] = x[1];
ans = max(ans, dp[1]);
for (int i = 2; i <= n; i++) {
dp[i] = max(x[i], dp[i - 1] + x[i]);
ans = max(ans, dp[i]);
}
for (int i = 2; i <= n - 1; i++) {
x[i] = abs(p[i] - p[i + 1]);
if (i % 2 == 1) x[i] *= -1;
}
dp[2] = x[2];
ans = max(ans, dp[2]);
for (int i = 3; i <= n; i++) {
dp[i] = max(x[i], dp[i - 1] + x[i]);
ans = max(ans, dp[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.util.Scanner;
public class P5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int a[] = new int[n - 1];
int l = scan.nextInt();
for (int i = 0; i < n - 1; i++) {
int r = scan.nextInt();
a[i] = Math.abs(l - r);
l = r;
}
/*
System.out.println();
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
*/
long max = 0;
long sum = 0;
for (int i = 0; i < n - 1; i++) {
if (sum < 0) {
sum = 0;
}
if (i % 2 == 0) {
sum += a[i];
} else {
sum -= a[i];
}
if (sum > max) {
max = sum;
}
//System.out.println(i + " sum: " +sum+" max: "+max);
}
sum = 0;
for (int i = 0; i < n - 1; i++) {
if (sum < 0) {
sum = 0;
}
if (i % 2 == 1) {
sum += a[i];
} else {
sum -= a[i];
}
if (sum > max) {
max = sum;
}
//System.out.println(i + " sum: " +sum+" max: "+max);
}
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 | # n, m = [int(x) for x in input().split(" ")]
# s = set()
# ls = [0]*(n+1)
# in_C = input().split(" ")
#
# for x in range(len(in_C)-1, -1, -1):
# s.add(int(in_C[x]))
# ls[x+1] = len(s)
# [print(ls[int(input())]) for i in range(m)]
#
# in_x = input()
# n = list(in_x)
# length = len(n)
# counter = 0
# counter_2 = 0
# for _ in range(length):
# temp = n[:]
# if counter > 0:
# temp.pop(counter)
# counter += 1
# for j in range(0, len(temp)):
# if j > 0:
# temp.pop((length - 1) - j)
# num = int("".join(temp))
# if num % 8 == 0:
# print(f"YES\n{num}")
# exit()
# temp = n[:]
# if counter_2 > 0:
# temp.pop(counter_2)
# counter_2 += 1
# # print(temp)
# for j in range(len(temp), 1, -1):
# temp.pop(j - len(temp))
# num = (int("".join(temp)))
# # print(num)
# if num % 8 == 0:
# print(f"YES\n{num}")
# exit()
# print("NO")
n = int(input())
nums = input().split(" ")
sumas = [0] * (n+1)
dp = [[0] * 2 for _ in range(n+1)]
res = -1e11
for x in range(0, n):
if x > 0:
sumas[x] = abs(int(nums[x]) - int(nums[x - 1]))
for x in range(1, n+1):
dp[x][0] = max(sumas[x], dp[x - 1][1] + sumas[x])
dp[x][1] = max(-sumas[x], dp[x - 1][0] - sumas[x])
res = max(max(dp[x][0], dp[x][1]), 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 | import java.util.Scanner;
public class FunctionsAgain {
//static long[][] dp ;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
int n = input.nextInt()-1;
int[] aa = new int[n];
int a = input.nextInt();
for(int i=0;i<n;i++){
int b = input.nextInt();
aa[i]= Math.abs(a-b);
a= b;
}
if(n==1){
System.out.println(aa[0]);
return;
}
long a2 = aa[0],a1 = aa[1];
long max = Math.max(a1,a2);
for(int i=2; i<n;i++){
long tmp = a1;
a1 = Math.max(a2-aa[i-1]+aa[i],aa[i]);
a2 = tmp;
max = Math.max(max,a1);
}
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;
int n;
vector<long long> data;
long long a;
int main() {
cin >> n;
long long nn = 0;
for (int i = 0; i < n; i++) {
cin >> a;
data.push_back(a);
}
long long answer = -1;
long long now = 0;
long long mm = 0;
for (int i = 0; i < n - 1; i++) {
if (i % 2 == 0) {
now += abs(data[i] - data[i + 1]);
answer = max(answer, now + mm);
} else {
now -= abs(data[i] - data[i + 1]);
answer = max(answer, now + mm);
mm = max(mm + now, nn);
now = 0;
}
}
mm = 0;
now = 0;
for (int i = 1; i < n - 1; i++) {
if (i % 2 != 0) {
now += abs(data[i] - data[i + 1]);
answer = max(answer, now + mm);
} else {
now -= abs(data[i] - data[i + 1]);
answer = max(answer, now + mm);
mm = max(mm + now, nn);
now = 0;
}
}
cout << answer << 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 abso(long long int a) { return (a < 0) ? a * -1 : a; }
int main() {
int n;
long long int a[100001], ans = 0, maxi = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 1; i < n; i++) {
if (i % 2) {
ans += abso(a[i] - a[i - 1]);
if (ans > maxi) maxi = ans;
} else {
ans -= abso(a[i] - a[i - 1]);
if (ans < 0)
ans = 0;
else if (ans > maxi)
maxi = ans;
}
}
ans = 0;
for (int i = 2; i < n; i++) {
if (i % 2 == 0) {
ans += abso(a[i] - a[i - 1]);
if (ans > maxi) maxi = ans;
} else {
ans -= abso(a[i] - a[i - 1]);
if (ans < 0)
ans = 0;
else if (ans > maxi)
maxi = ans;
}
}
cout << maxi << 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())
f = []
arr = list(map(int, input().split()))
for i in range(0, len(arr) - 1):
f.append(abs(arr[i] - arr[i + 1]))
dp = [[0] * len(f), [0] * len(f)]
dp[0][0] = 0
dp[1][0] = f[0]
for i in range(1, len(f)):
dp[0][i] = dp[1][i-1]-f[i]
dp[1][i] = max(dp[0][i - 1] + f[i], f[i])
ans = max(max(dp[0]), max(dp[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 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
long[] arr1 = new long[n-1];
long[] arr2 = new long[n-1];
long[] arr = new long[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = Long.parseLong(st.nextToken());
}
for (int i = 0; i < arr.length-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;
if(i%2 == 1)arr2[i] *= -1;
}
long max1 = Long.MIN_VALUE;
long max2 = Long.MIN_VALUE;
long sum = 0;
long sum1 = 0;
for (int i = 0; i < arr1.length; i++) {
if(sum < 0){
sum = 0;
}
if(sum1 < 0){
sum1 = 0;
}
sum+=arr1[i];
max1 = Long.max(max1, sum);
sum1+=arr2[i];
max2 = Long.max(max2, sum1);
}
System.out.println(Long.max(max1, max2));
}
}
| 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 M = 1e5 + 10;
int n;
long long a[M];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%I64d", a + i);
}
for (int i = 1; i < n; ++i) {
a[i] = abs(a[i + 1] - a[i]);
if (i & 1) a[i] = -a[i];
}
long long ans = -1e17;
long long minn = 0;
long long sum = 0;
for (int i = 1; i < n; ++i) {
sum += a[i];
ans = max(ans, sum - minn);
minn = min(minn, sum);
}
for (int i = 1; i < n; ++i) {
a[i] = -a[i];
}
minn = 0;
sum = 0;
for (int i = 1; i < n; ++i) {
sum += a[i];
ans = max(ans, sum - minn);
minn = min(minn, sum);
}
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;
int main() {
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];
b[0] = 0;
c[0] = 0;
long long sum = -1e15, sum1 = -1e15, mb = 0, mc = 0;
for (long long i = 0; i < n - 1; i++) {
b[i + 1] = b[i] + abs(a[i + 1] - a[i]) * (i % 2 ? -1 : 1);
c[i + 1] = c[i] + abs(a[i + 1] - a[i]) * (i % 2 ? 1 : -1);
sum = max(sum, b[i + 1] - mb);
sum1 = max(sum1, c[i + 1] - mc);
mb = min(mb, b[i + 1]);
mc = min(mc, c[i + 1]);
}
cout << max(sum, sum1);
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 = 1e5 + 5;
using ll = long long;
ll a[maxn];
ll b[maxn];
ll c[maxn];
int main() {
std::ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i < n; ++i) {
a[i] = abs(a[i] - a[i + 1]);
}
int flag = 1;
for (int i = 1; i < n; ++i) {
b[i] = a[i] * flag;
flag *= -1;
}
flag = -1;
for (int i = 1; i < n; ++i) {
c[i] = a[i] * flag;
flag *= -1;
}
ll mmax = 0x3fffffff * -1;
ll tmpb = 0;
ll tmpc = 0;
for (int i = 1; i < n; ++i) {
tmpb += b[i];
tmpc += c[i];
if (tmpb < 0) tmpb = 0;
if (tmpc < 0) tmpc = 0;
mmax = max(mmax, tmpb);
mmax = max(mmax, tmpc);
}
cout << mmax << 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.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Created by Rakshit on 8/4/17.
* Java is Love.. :)
*/
public class FuncAg {
static int n;
static final int N=100005;
static int a[]=new int [N];
static int b[]=new int [N];
static int c[]=new int [N];
static SortedSet<Long> rec=new TreeSet<>();
static SortedSet<Long> res=new TreeSet<>();
public static void main(String args[])
{
InputStream stream=System.in;
OutputStream out1=System.out;
InputReader in = new InputReader(stream);
PrintWriter out=new PrintWriter(out1);
Task solver=new Task();
solver.solve(in,out);
}
static class Task
{
public void solve(InputReader in , PrintWriter out)
{
n=in.nextInt();
a=in.nextIntArray(n);
for(int i=0;i<n-1;i++)
{
b[i]=Math.abs(a[i]-a[i+1])*((int)Math.pow(-1,i));
c[i]=Math.abs(a[i]-a[i+1])*((int)Math.pow(-1,i+1));
}
long result=0;
for(int i=0;i<n-1;i++)
{
result+=(long)b[i];
if(result<0)
result=0;
else
res.add(result);
}
result=0;
for(int i=1;i<n-1;i++)
{
result+=c[i];
if(result<0)
result=0;
else
rec.add(result);
}
long m=0,n=0;
if(res.size()>0)
m=res.last();
if(rec.size()>0)
n=rec.last();
out.println(Math.max(m,n));
out.flush();
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 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 {
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 {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
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 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 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 | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> v(n);
deque<ll> a(n + 10);
for (int i = 0; i < n; ++i) cin >> v[i];
for (int i = 1; i < n; ++i) a[i] = abs(v[i - 1] - v[i]);
a.pop_front();
ll ret = a[0], maxplus = a[0], minus = INT_MIN;
for (int i = 1; i < n; ++i) {
ll maxi = maxplus;
maxplus = max(a[i], a[i] + minus);
minus = maxi - a[i];
ret = max(ret, maxplus);
}
cout << ret;
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 int N = 200010;
const long long mod = 1000000007;
const long long inf = 1000000000000000000;
const long double pi = 3.14159265358979323846264338;
long long int kadane_algo(vector<long long int> a) {
long long int ans = a[0], curr = a[0];
for (long long int i = 1; i < a.size(); i++) {
curr = max(a[i], curr + a[i]);
ans = max(ans, curr);
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
vector<long long int> a(n);
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long int> v;
for (long long int i = 0; i < n - 1; i++) {
v.push_back(abs(a[i] - a[i + 1]));
}
vector<long long int> v1, v2;
for (long long int i = 0; i < v.size(); i++) {
if (i % 2)
v1.push_back(v[i]);
else
v1.push_back(-v[i]);
}
for (long long int i = 0; i < v.size(); i++) {
if (i % 2)
v2.push_back(-v[i]);
else
v2.push_back(v[i]);
}
cout << max(kadane_algo(v1), kadane_algo(v2));
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,os,io
import math,bisect,operator
inf,mod = float('inf'),10**9+7
# sys.setrecursionlimit(10 ** 6)
from itertools import groupby,accumulate
from heapq import heapify,heappop,heappush
from collections import deque,Counter,defaultdict
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
Neo = lambda : list(map(int,input().split()))
test, = Neo()
n = test
A = Neo()
def maxSubArray(A):
curSum = maxSum = A[0]
for num in A[1:]:
curSum = max(num, curSum + num)
maxSum = max(maxSum, curSum)
return maxSum
A1 = [-10**9]
A2 = [abs(A[0]-A[1])]
for i in range(1,n-1):
t = abs(A[i]-A[i+1])
if i%2:
A1.append(t)
A2.append(-t)
else:
A1.append(-t)
A2.append(t)
print(max(maxSubArray(A1),maxSubArray(A2)))
| 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 num[100005];
int main() {
long long n, mx = 0, mn = 0, sum = 0;
scanf("%lld %lld", &n, &num[1]);
for (long long i = 2; i <= n; i++) {
scanf("%lld", &num[i]);
if (i % 2)
sum += abs(num[i] - num[i - 1]);
else
sum -= abs(num[i] - num[i - 1]);
mx = max(sum, mx);
mn = min(mn, sum);
}
cout << mx - mn << 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 | n=int(input())
a=list(map(int,input().split()))
dp=[[0,0] for i in range(n)]
dp[0][1]=abs(a[1]-a[0])
maxi=abs(a[1]-a[0])
for i in range(1,n-1):
dp[i][0]=max(0,dp[i-1][1]-abs(a[i]-a[i+1]))
dp[i][1]=max(0,dp[i-1][0]+abs(a[i]-a[i+1]))
maxi=max(maxi,dp[i][0],dp[i][1])
print(maxi) | 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 | from sys import stdin
inf = float("inf")
N, = map(int, stdin.readline().split())
A = list(map(int, stdin.readline().split()))
diffodd = [0] * (N)
diffeven = [0] * (N)
for i in range(1, N):
v = abs(A[i - 1] - A[i])
if i % 2 == 1:
diffodd[i] = v
diffeven[i] = -v
else:
diffeven[i] = v
diffodd[i] = -v
# print(diffodd, diffeven)
for i in range(1, N):
diffodd[i] += diffodd[i - 1]
diffeven[i] += diffeven[i - 1]
# print(diffodd, diffeven)
best_startodd = 0
best_starteven = 0
best_sum = -inf
for end in range(1, N):
best_sum = max(best_sum, diffodd[end] - diffodd[best_startodd], diffeven[end] - diffeven[best_starteven])
if diffodd[end] < diffodd[best_startodd]:
best_startodd = end
if diffeven[end] < diffeven[best_starteven]:
best_starteven = end
print(best_sum)
| 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 cf407c {
static final int N=(int)1e5+10;
static final long inf=(long)1e10;
static int a[]=new int[N];
static long b[]=new long[N];
static long c[]=new long[N];
public static void main(String[] args){
Scanner cin=new Scanner(new InputStreamReader(System.in));
while(cin.hasNext()){
int n=cin.nextInt();
for(int i =0; i<n; i++){
a[i]=cin.nextInt();
}
b[n-1]=c[n-1]=0;
long ans=0;
for(int i=n-2;i>=0;i--){
b[i]=Math.max(-c[i+1],0)+Math.abs(a[i]-a[i+1]);
c[i]=Math.min(-b[i+1],0)+Math.abs(a[i]-a[i+1]);
ans=Math.max(ans,b[i]);
}
System.out.println(ans);
}
cin.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 | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
long long int sum = abs(v[0] - v[1]), ma = abs(v[0] - v[1]);
for (int i = 2; i < n; i++) {
if (i % 2 == 0) {
sum -= abs(v[i] - v[i - 1]);
} else {
sum += abs(v[i] - v[i - 1]);
}
if (sum < 0) {
sum = 0;
}
ma = max(ma, sum);
}
sum = 0;
for (int i = 2; i < n; i++) {
if (i % 2 == 0) {
sum += abs(v[i] - v[i - 1]);
} else {
sum -= abs(v[i] - v[i - 1]);
}
if (sum < 0) {
sum = 0;
}
ma = max(ma, sum);
}
cout << ma;
}
| 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 go(vector<long long> a) {
long long result = 0;
long long now = 0;
for (auto& item : a) {
now += item;
if (now < 0) {
now = 0;
}
result = max(result, now);
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> b(n), a(n - 1);
for (auto& item : b) {
cin >> item;
}
for (int i = 0; i < n - 1; ++i) {
a[i] = abs(b[i + 1] - b[i]);
if (i & 1) a[i] *= -1;
}
long long result = go(a);
for (auto& item : a) {
item *= -1;
}
result = max(result, go(a));
cout << result << 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.io.IOException;
import java.io.InputStream;
import java.util.InputMismatchException;
public class P788A
{
public static void main(String[] args)
{
FastScanner in = new FastScanner(System.in);
int n = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
long[] diff = new long[n - 1];
for (int i = 0; i < n - 1; i++)
diff[i] = Math.abs(a[i] - a[i + 1]);
long max = -1;
long curr = 0;
for (int i = 0; i < n - 1; i += 2) {
curr += diff[i];
if (curr > max)
max = curr;
if (i < n - 2)
curr -= diff[i + 1];
if (curr < 0)
curr = 0;
}
curr = 0;
for (int i = 1; i < n - 1; i += 2) {
curr += diff[i];
if (curr > max)
max = curr;
if (i < n - 2)
curr -= diff[i + 1];
if (curr < 0)
curr = 0;
}
System.out.println(max);
}
/**
* Source: Matt Fontaine
*/
public static class FastScanner
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream)
{
this.stream = stream;
}
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++];
}
boolean isSpaceChar(int c)
{
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
public String next()
{
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 String nextLine()
{
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.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 | import java.io.*;
import java.util.*;
public class cf407c {
static final int N=(int)1e5+10;
static final long inf=(long)1e10;
static int a[]=new int[N];
static int b[]=new int[N];
static int c[]=new int[N];
public static void main(String[] args){
Scanner cin=new Scanner(new InputStreamReader(System.in));
while(cin.hasNext()){
int n=cin.nextInt();
for(int i =0; i<n; i++){
a[i]=cin.nextInt();
}
int tmp;
for(int i=0;i<n-1;i++){
tmp=Math.abs(a[i]-a[i+1]);
if(i%2==0){
b[i]=tmp;
c[i]=-tmp;
}else{
b[i]=-tmp;
c[i]=tmp;
}
}
long ans1=-inf,ans2=-inf;
long sum=-inf;
for(int i=0;i<n-1;i++){
sum+=b[i];
ans1=Math.max(ans1, sum);
if(sum<0){
sum=0;
if(i%2==0) i--;
}
}
sum=-inf;
for(int i=0;i<n-1;i++){
sum+=c[i];
ans2=Math.max(ans2, sum);
if(sum<0){
sum=0;
if(i%2==1) i--;
}
}
System.out.println(Math.max(ans1, ans2));
}
cin.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 | #include <bits/stdc++.h>
using namespace std;
using std::cin;
using std::cout;
long long MOD = 998244353;
int main() {
int n;
cin >> n;
int tmp[n];
long long arr[n - 1];
for (int i = 0; i < n; i++) cin >> tmp[i];
for (int i = 0; i < n - 1; i++) {
arr[i] = abs(tmp[i + 1] - tmp[i]);
}
long long maxn = 0;
long long dpEven[n];
long long dpOdd[n];
for (int i = 0; i < n; i++) {
dpEven[i] = 0;
dpOdd[i] = 0;
}
for (int i = 1; i < n; i++) {
dpEven[i] = max(arr[i - 1], dpOdd[i - 1] + arr[i - 1]);
dpOdd[i] = dpEven[i - 1] - arr[i - 1];
maxn = max(maxn, dpEven[i]);
maxn = max(maxn, dpOdd[i]);
}
cout << maxn << "\n";
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.