description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
d = int(input()) l = list(map(int, input().split())) res = 0 cr = 0 w = l[0] // 2 b = l[0] - w for i in range(1, d): if (i + 1) % 2 != 0: w = w + l[i] // 2 b = b + l[i] - l[i] // 2 else: b = b + l[i] // 2 w = w + l[i] - l[i] // 2 print(min(b, w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) L = list(map(int, input().split())) col = "white" w = 0 b = 0 for i in range(n): if col == "white": w += (L[i] + 1) // 2 b += L[i] // 2 col = "black" else: b += (L[i] + 1) // 2 w += L[i] // 2 col = "white" print(min(w, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
a = int(input()) A = list(map(int, input().split())) w = 0 b = 0 for i in range(a): A[i] += i % 2 b += A[i] // 2 w += A[i] - A[i] // 2 - i % 2 print(min(b, w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) l = list(map(int, input().split())) r = 0 b = 0 for i in range(len(l)): if l[i] % 2 == 1: if i % 2 == 1: r += int(l[i] / 2) + 1 b += int(l[i] / 2) else: r += int(l[i] / 2) b += int(l[i] / 2) + 1 else: r += int(l[i] / 2) b += int(l[i] / 2) print(min(r, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) data = list(map(int, input().split())) b, w = 0, 0 for i in range(n): n_even = int(data[i] / 2) n_odd = data[i] - n_even if i % 2 == 0: b += n_even w += n_odd else: b += n_odd w += n_even print(min(b, w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) vals = list(map(int, input().split(" "))) black, white = 0, 0 even = False for v in vals: vdiv2 = v // 2 black += vdiv2 white += vdiv2 if v & 1: if even: black += 1 else: white += 1 even = not even print(min(black, white))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split())) ans = 0 for q in range(n): if q % 2 == 0: ans += (a[q] + 1) // 2 else: ans += a[q] // 2 print(min(ans, sum(a) - ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split())) w = 0 b = 0 for i in range(n): if i % 2 == 0: if a[i] % 2 == 1: b += 1 b += a[i] // 2 w += a[i] // 2 else: if a[i] % 2 == 1: w += 1 b += a[i] // 2 w += a[i] // 2 print(min(w, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split(" "))) A, B = 0, 0 for i, v in enumerate(a): g = (v + 1) // 2 l = v - g if i % 2: g, l = l, g A, B = A + g, B + l print(min(A, B))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = input() L = map(int, input().split()) base_is_white = True white = 0 black = 0 for x in L: if base_is_white: white += x // 2 + x % 2 black += x // 2 base_is_white = False else: white += x // 2 black += x // 2 + x % 2 base_is_white = True print(min(white, black))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = input().split(" ") for i in range(n): a[i] = int(a[i]) w = a[0] // 2 b = a[0] - w c = 1 def no(x): if x == 1: return 0 return 1 for i in range(1, len(a)): if a[i - 1] % 2 == 0: if a[i] % 2 == 0: w += a[i] // 2 b += a[i] // 2 c = no(c) elif c == 1: w += a[i] // 2 b += a[i] - a[i] // 2 else: b += a[i] // 2 w += a[i] - a[i] // 2 elif a[i] % 2 == 0: w += a[i] // 2 b += a[i] // 2 else: if c == 1: b += a[i] // 2 w += a[i] - a[i] // 2 else: w += a[i] // 2 b += a[i] - a[i] // 2 c = no(c) print(min(b, w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
import sys input = sys.stdin.readline N = int(input()) A = list(map(int, input().split())) white = 0 black = 0 for i, a in enumerate(A): if a % 2 == 0: white += a // 2 black += a // 2 elif i % 2 == 0: black += a // 2 white += a // 2 + 1 else: white += a // 2 black += a // 2 + 1 print(min(black, white))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
import sys input = sys.stdin.readline lastodd = [] n = int(input()) a = [int(i) for i in input().split()] ans = 0 for i in range(n): if not a[i] & 1: ans += a[i] // 2 else: if lastodd: if i - lastodd[-1] & 1: ans += 1 lastodd.pop() else: lastodd.append(i) else: lastodd.append(i) ans += a[i] // 2 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) array = list(map(int, input().split())) c1, c2 = 0, 0 for i in range(n): a = array[i] if a % 2 == 0: c1 += a // 2 c2 += a // 2 else: c1 += a // 2 c2 += a // 2 if i % 2 == 0: c1 += 1 else: c2 += 1 print(min(c1, c2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) hist = [int(i) for i in input().split()] total = sum(hist) white = 0 black = 0 for i in range(n): if i % 2 == 0: white += hist[i] // 2 else: white += (hist[i] + 1) // 2 black = total - white print(min(white, black))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split())) b = w = 0 key = True for j in range(len(a)): if key: if a[j] % 2 == 0: b += a[j] // 2 w += a[j] // 2 else: b += a[j] // 2 + 1 w += a[j] // 2 key = False else: if a[j] % 2 == 0: w += a[j] // 2 b += a[j] // 2 else: w += a[j] // 2 + 1 b += a[j] // 2 key = True print(min(w, b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split())) ans = [0, 0] color = 0 for x in a: ans[color] += x // 2 color ^= 1 ans[color] += x - x // 2 print(min(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split())) black = 0 white = 0 j = 0 while j < n: black += (a[j] + 1) // 2 white += a[j] // 2 black, white = white, black j += 1 print(min(black, white))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) b, w = 0, 0 cols = [int(x) for x in input().split()] for c in range(n): if cols[c] % 2 == 1: b += c % 2 w += (c + 1) % 2 print((sum(cols) - abs(b - w)) // 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) A = list(map(int, input().split())) x, y = 0, 0 for a in A: x += a // 2 y += (a + 1) // 2 x, y = y, x print(min(x, y))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = list(map(int, input().split())) dp = [(0) for _ in range(2)] pre = -1 for i in range(n): if i % 2 == 0: dp[0] += a[i] // 2 dp[1] += a[i] - a[i] // 2 else: dp[0] += a[i] - a[i] // 2 dp[1] += a[i] // 2 print(min(dp[0], dp[1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
from sys import stdin, stdout def scal(typ=int): return typ(stdin.readline()) def vec(typ=int): if isinstance(typ, list): inp = stdin.readline().split() return [typ[i](inp[i]) for i in range(len(inp))] else: return list(map(typ, stdin.readline().split())) n = scal() A = vec() i, b, w = 0, 0, 0 for a in A: if i: w += a - a // 2 b += a // 2 else: w += a // 2 b += a - a // 2 i = 1 - i print(min(b, w))
FUNC_DEF VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) arr = [int(x) for x in input().split()] ans = 0 for i in range(len(arr)): ans += arr[i] // 2 arr[i] %= 2 for i in range(2, len(arr)): arr[i % 2] += arr[i] if n > 1: ans += min(arr[0], arr[1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a Young diagram. Given diagram is a histogram with n columns of lengths a_1, a_2, …, a_n (a_1 ≥ a_2 ≥ … ≥ a_n ≥ 1). <image> Young diagram for a=[3,2,2,2,1]. Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1 × 2 or 2 × 1 rectangle. Input The first line of input contain one integer n (1 ≤ n ≤ 300 000): the number of columns in the given histogram. The next line of input contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 300 000, a_i ≥ a_{i+1}): the lengths of columns. Output Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram. Example Input 5 3 2 2 2 1 Output 4 Note Some of the possible solutions for the example: <image> <image>
n = int(input()) a = [int(i) for i in input().split()] m = [0, 0] for i in range(n): m[0] += a[i] // 2 m[1] += a[i] // 2 if a[i] % 2 != 0: m[i % 2] += 1 print(int(min(m[0], m[1])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Morning desert sun horizon Rise above the sands of time... Fates Warning, "Exodus" After crossing the Windswept Wastes, Ori has finally reached the Windtorn Ruins to find the Heart of the Forest! However, the ancient repository containing this priceless Willow light did not want to open! Ori was taken aback, but the Voice of the Forest explained to him that the cunning Gorleks had decided to add protection to the repository. The Gorleks were very fond of the "string expansion" operation. They were also very fond of increasing subsequences. Suppose a string s_1s_2s_3 … s_n is given. Then its "expansion" is defined as the sequence of strings s_1, s_1 s_2, ..., s_1 s_2 … s_n, s_2, s_2 s_3, ..., s_2 s_3 … s_n, s_3, s_3 s_4, ..., s_{n-1} s_n, s_n. For example, the "expansion" the string 'abcd' will be the following sequence of strings: 'a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'. To open the ancient repository, Ori must find the size of the largest increasing subsequence of the "expansion" of the string s. Here, strings are compared lexicographically. Help Ori with this task! A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a ≠ b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input Each test contains multiple test cases. The first line contains one positive integer t (1 ≤ t ≤ 10^3), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer n (1 ≤ n ≤ 5000) — length of the string. The second line of each test case contains a non-empty string of length n, which consists of lowercase latin letters. It is guaranteed that the sum of n over all test cases does not exceed 10^4. Output For every test case print one non-negative integer — the answer to the problem. Example Input 7 5 acbac 8 acabacba 12 aaaaaaaaaaaa 10 abacabadac 8 dcbaabcd 3 cba 6 sparky Output 9 17 12 29 14 3 9 Note In first test case the "expansion" of the string is: 'a', 'ac', 'acb', 'acba', 'acbac', 'c', 'cb', 'cba', 'cbac', 'b', 'ba', 'bac', 'a', 'ac', 'c'. The answer can be, for example, 'a', 'ac', 'acb', 'acba', 'acbac', 'b', 'ba', 'bac', 'c'.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) s = input()[:-1] lcp = [([0] * (n + 1)) for _ in range(n + 1)] for i in range(n - 1, -1, -1): for j in range(n - 1, -1, -1): if s[i] == s[j]: lcp[i][j] = lcp[i + 1][j + 1] + 1 def cal(x, y): t = lcp[x][y] if y + t >= n: return -1 xc, yc = s[x + t], s[y + t] if xc > yc: return -1 return t dp = [0] * n for i in range(n): dp[i] = n - i for j in range(i): x = cal(j, i) if x != -1: dp[i] = max(dp[i], dp[j] + n - i - x) print(max(dp))
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Morning desert sun horizon Rise above the sands of time... Fates Warning, "Exodus" After crossing the Windswept Wastes, Ori has finally reached the Windtorn Ruins to find the Heart of the Forest! However, the ancient repository containing this priceless Willow light did not want to open! Ori was taken aback, but the Voice of the Forest explained to him that the cunning Gorleks had decided to add protection to the repository. The Gorleks were very fond of the "string expansion" operation. They were also very fond of increasing subsequences. Suppose a string s_1s_2s_3 … s_n is given. Then its "expansion" is defined as the sequence of strings s_1, s_1 s_2, ..., s_1 s_2 … s_n, s_2, s_2 s_3, ..., s_2 s_3 … s_n, s_3, s_3 s_4, ..., s_{n-1} s_n, s_n. For example, the "expansion" the string 'abcd' will be the following sequence of strings: 'a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'. To open the ancient repository, Ori must find the size of the largest increasing subsequence of the "expansion" of the string s. Here, strings are compared lexicographically. Help Ori with this task! A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a ≠ b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input Each test contains multiple test cases. The first line contains one positive integer t (1 ≤ t ≤ 10^3), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer n (1 ≤ n ≤ 5000) — length of the string. The second line of each test case contains a non-empty string of length n, which consists of lowercase latin letters. It is guaranteed that the sum of n over all test cases does not exceed 10^4. Output For every test case print one non-negative integer — the answer to the problem. Example Input 7 5 acbac 8 acabacba 12 aaaaaaaaaaaa 10 abacabadac 8 dcbaabcd 3 cba 6 sparky Output 9 17 12 29 14 3 9 Note In first test case the "expansion" of the string is: 'a', 'ac', 'acb', 'acba', 'acbac', 'c', 'cb', 'cba', 'cbac', 'b', 'ba', 'bac', 'a', 'ac', 'c'. The answer can be, for example, 'a', 'ac', 'acb', 'acba', 'acbac', 'b', 'ba', 'bac', 'c'.
import sys for _ in range(int(sys.stdin.readline())): n = int(sys.stdin.readline()) s = sys.stdin.readline().strip() d = [0] * n l = [0] * (n + 1) for i in range(n - 1, -1, -1): for j in range(i + 1, n): if s[i] < s[j]: d[i] = max(d[i], d[j]) if s[i] == s[j]: l[j] = l[j + 1] + 1 if j + l[j] < n and s[i + l[j]] < s[j + l[j]]: d[i] = max(d[i], d[j] - l[j]) else: l[j] = 0 d[i] += n - i sys.stdout.write("%d\n" % max(d))
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR
Morning desert sun horizon Rise above the sands of time... Fates Warning, "Exodus" After crossing the Windswept Wastes, Ori has finally reached the Windtorn Ruins to find the Heart of the Forest! However, the ancient repository containing this priceless Willow light did not want to open! Ori was taken aback, but the Voice of the Forest explained to him that the cunning Gorleks had decided to add protection to the repository. The Gorleks were very fond of the "string expansion" operation. They were also very fond of increasing subsequences. Suppose a string s_1s_2s_3 … s_n is given. Then its "expansion" is defined as the sequence of strings s_1, s_1 s_2, ..., s_1 s_2 … s_n, s_2, s_2 s_3, ..., s_2 s_3 … s_n, s_3, s_3 s_4, ..., s_{n-1} s_n, s_n. For example, the "expansion" the string 'abcd' will be the following sequence of strings: 'a', 'ab', 'abc', 'abcd', 'b', 'bc', 'bcd', 'c', 'cd', 'd'. To open the ancient repository, Ori must find the size of the largest increasing subsequence of the "expansion" of the string s. Here, strings are compared lexicographically. Help Ori with this task! A string a is lexicographically smaller than a string b if and only if one of the following holds: * a is a prefix of b, but a ≠ b; * in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. Input Each test contains multiple test cases. The first line contains one positive integer t (1 ≤ t ≤ 10^3), denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer n (1 ≤ n ≤ 5000) — length of the string. The second line of each test case contains a non-empty string of length n, which consists of lowercase latin letters. It is guaranteed that the sum of n over all test cases does not exceed 10^4. Output For every test case print one non-negative integer — the answer to the problem. Example Input 7 5 acbac 8 acabacba 12 aaaaaaaaaaaa 10 abacabadac 8 dcbaabcd 3 cba 6 sparky Output 9 17 12 29 14 3 9 Note In first test case the "expansion" of the string is: 'a', 'ac', 'acb', 'acba', 'acbac', 'c', 'cb', 'cba', 'cbac', 'b', 'ba', 'bac', 'a', 'ac', 'c'. The answer can be, for example, 'a', 'ac', 'acb', 'acba', 'acbac', 'b', 'ba', 'bac', 'c'.
import sys input = sys.stdin.readline def SA_IS(a): a += [0] k = max(a) + 1 n = len(a) def induce_l(sa, a, n, k, stype): bucket = get_buckets(a, k, 1) for i in range(n): j = sa[i] - 1 if j >= 0 and not stype[j]: sa[bucket[a[j]]] = j bucket[a[j]] += 1 def induce_s(sa, a, n, k, stype): bucket = get_buckets(a, k, 0) for i in range(n)[::-1]: j = sa[i] - 1 if j >= 0 and stype[j]: bucket[a[j]] -= 1 sa[bucket[a[j]]] = j def get_buckets(a, k, start=0): bucket = [0] * k for item in a: bucket[item] += 1 s = 0 for i in range(k): s += bucket[i] bucket[i] = s - (bucket[i] if start else 0) return bucket def set_lms(a, n, k, default_order): bucket = get_buckets(a, k) sa = [-1] * n for i in default_order[::-1]: bucket[a[i]] -= 1 sa[bucket[a[i]]] = i return sa def induce(a, n, k, stype, default_order): sa = set_lms(a, n, k, default_order) induce_l(sa, a, n, k, stype) induce_s(sa, a, n, k, stype) return sa def rename_LMS_substring(sa, a, n, stype, LMS, l): sa = [_s for _s in sa if LMS[_s]] tmp = [-1] * (n // 2) + [0] dupl = 0 for _ in range(1, l): i, j = sa[_ - 1], sa[_] for ii in range(n): if a[i + ii] != a[j + ii] or stype[i + ii] != stype[j + ii]: break if ii and (LMS[i + ii] or LMS[j + ii]): dupl += 1 break tmp[j // 2] = _ - dupl tmp = [t for t in tmp if t >= 0] return tmp, dupl def calc(a, n, k): stype = [1] * n for i in range(n - 1)[::-1]: if a[i] > a[i + 1] or a[i] == a[i + 1] and stype[i + 1] == 0: stype[i] = 0 LMS = [(1 if stype[i] and not stype[i - 1] else 0) for i in range(n - 1)] + [1] l = sum(LMS) lms = [i for i in range(n) if LMS[i]] sa = induce(a, n, k, stype, lms) renamed_LMS, dupl = rename_LMS_substring(sa, a, n, stype, LMS, l) if dupl: sub_sa = calc(renamed_LMS, l, l - dupl) else: sub_sa = [0] * l for i in range(l): sub_sa[renamed_LMS[i]] = i lms = [lms[sub_sa[i]] for i in range(l)] sa = induce(a, n, k, stype, lms) return sa sa = calc(a, n, k) return sa def LCP(s, n, sa): lcp = [-1] * (n + 1) rank = [0] * (n + 1) for i in range(n + 1): rank[sa[i]] = i h = 0 lcp[0] = 0 for i in range(n): j = sa[rank[i] - 1] if h > 0: h -= 1 while j + h < n and i + h < n and s[j + h] == s[i + h]: h += 1 lcp[rank[i] - 1] = h return lcp def main(): n = int(input()) S = [(ord(a) - 96) for a in input().strip()] SA = SA_IS(S) lcp = LCP(S, n, SA) dp = [0] * n for i in range(1, n + 1): pos = SA[i] plus = n - pos min_ = 10**10 for j in range(i - 1, 0, -1): p2 = SA[j] min_ = min(min_, lcp[j]) if p2 < pos: dp[pos] = max(dp[pos], dp[p2] - min_) dp[pos] += plus print(max(dp)) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR FUNC_DEF VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) q = d = 0 p = False ar = list(map(int, input().split())) min = 10000000000 r = max(ar) for i in ar: if i >= 0: if i < min: min = i q += i else: p = True d += -i if r >= 0: if p: max = q + d else: max = q - min - min else: max = d + r + r if len(ar) == 1: print(ar[0]) else: print(max)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) a.sort() c = a[n - 1] f = a[0] if n > 1: for i in range(1, n - 1): if a[i] >= 0: f -= a[i] elif a[i] < 0: f += a[i] print(c - f) else: print(a[0])
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) l1 = list(map(int, input().split())) x = l1[0] flag1 = 0 flag2 = 0 ans = 0 for item in l1: ans += abs(item) if item < 0: flag1 = 1 else: flag2 = 1 for i in range(0, n): l1[i] = abs(l1[i]) if flag1 == 1 and flag2 == 1: print(ans) elif n != 1: print(sum(l1) - 2 * min(l1)) else: print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) all_positive = True all_negative = True min_positive_index = None max_negative_index = None min_abs_value = abs(a[0]) total = 0 if n == 1: print(a[0]) else: for i in range(n): value = a[i] all_positive &= value > 0 all_negative &= value < 0 min_abs_value = min(min_abs_value, abs(value)) total += abs(value) if all_positive or all_negative: print(total - 2 * min_abs_value) else: print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
I = lambda: map(int, input().split()) n = int(input()) a = list(I()) s = 0 if n == 1: exit(print(a[0])) if all(i > 0 for i in a): print(sum(a) - 2 * min(a)) elif all(i < 0 for i in a): print(-(sum(a) - 2 * max(a))) else: for i in a: s += abs(i) print(s)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys input = sys.stdin.readline n = int(input()) ar = list(map(int, input().split())) if n == 1: print(ar[0]) elif n == 2: print(abs(ar[0] - ar[1])) else: flag = False for i in range(1, n): if ar[i] * ar[i - 1] <= 0: flag = True break if flag: ans = 0 for i in range(n): ans += abs(ar[i]) print(ans) else: mi = float("inf") ans = 0 for i in range(n): mi = min(mi, abs(ar[i])) ans += abs(ar[i]) print(abs(ans - mi * 2))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) l = list(map(int, input().split())) pos = 0 neg = 0 w = [] for i in range(n): if l[i] >= 0: pos += 1 else: neg += 1 w.append(abs(l[i])) if n == 1: print(l[0]) elif neg != 0 and pos == 0: print(sum(w) - min(w) * 2) elif pos != 0 and neg == 0: print(sum(w) - min(w) * 2) else: print(sum(w))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = input().split() for i in range(n): a[i] = int(a[i]) if n == 1: print(a[0]) else: sm = 0 havePositive = False haveNegative = False for c in a: if c == 0: haveNegative = True havePositive = True elif c > 0: havePositive = True sm += c else: haveNegative = True sm -= c if haveNegative and havePositive: print(sm) else: for i in range(n): a[i] = abs(a[i]) ans = sum(a) low = a[0] for c in a: low = min(low, c) print(ans - 2 * low)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = [int(i) for i in input().split()] if n == 1: print(a[0]) exit() mx, mxi = -(10**10), -1 mn, mni = 10**10, -1 for i in range(n): if a[i] > mx: mx, mxi = a[i], i if a[i] < mn: mn, mni = a[i], i if mn < 0: if mx < 0: print(-sum(a) + 2 * mx) else: print(sum([abs(i) for i in a])) else: print(sum(a) - 2 * mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys input = sys.stdin.readline n = int(input()) v = list(sorted(map(int, input().split()))) if n == 1: print(v[0]) else: print(v[n - 1] - v[0] + sum(map(abs, v)) - (abs(v[0]) + abs(v[n - 1])))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
def f(arr, N): if N == 1: return arr[0] a = 0 a_min = 10**9 + 1 b = 0 c = False d = False b_min = -1 * a_min for i in arr: if i >= 0: c = True a += i a_min = min(a_min, i) else: d = True b += i b_min = max(i, b_min) if c: if d: return a - b return a - 2 * a_min return 2 * b_min - b N = int(input()) arr = list(map(int, input().strip().split())) print(f(arr, N))
FUNC_DEF IF VAR NUMBER RETURN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR IF VAR RETURN BIN_OP VAR VAR RETURN BIN_OP VAR BIN_OP NUMBER VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
from sys import stdin n = int(input()) a = [int(x) for x in stdin.readline().split()] if n == 1: print(a[0]) else: cnt = 0 b = [0] * n li1 = [0] * n li2 = [0] * n for i in range(n): b[i] = abs(a[i]) for i in range(n): if b[i] == a[i]: cnt += 1 else: cnt = cnt ans = float("-inf") if cnt == n or cnt == 0: for i in range(n): if i == 0: li1[i] = b[i] else: li1[i] = li1[i - 1] + b[i] for i in range(n - 1, -1, -1): if i == n - 1: li2[i] = b[i] else: li2[i] = li2[i + 1] + b[i] for i in range(n - 1): su = 0 s = a[i] - a[i + 1] su += abs(s) if i != 0: su += li1[i - 1] if i != n - 2: su += li2[i + 2] if su > ans: ans = su pos = i print(ans) else: ans = sum(b) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) lis = list(map(int, input().split())) if len(lis) == 1: print(lis[0]) else: lis.sort() a = -lis[0] + lis[-1] lis = list(map(abs, lis)) print(a + sum(lis[1:-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) l = list(map(int, input().split())) if n == 1: print(l[0]) exit(0) pos = 0 neg = 0 for i in l: if i >= 0: pos -= -1 else: neg -= -1 if neg > 0 and pos > 0: sm = 0 for i in l: sm += abs(i) print(sm) elif neg == 0: print(sum(l) - 2 * min(l)) else: print(abs(sum(l)) - 2 * abs(max(l)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) arr = list(map(int, input().split())) arr.sort() arr1 = [] arr2 = [] val1 = 0 for i in range(n - 1): val1 += arr[i] arr1.append(val1) val1 = 0 for i in range(n - 1, 0, -1): val1 += arr[i] arr2.append(val1) fans = -10000000000000000000000 for i in range(n - 1): fans = max(fans, arr2[n - 2 - i] - arr1[i]) if n == 1: print(arr[0]) else: print(fans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) else: prod_minus = False for i in range(n - 1): if a[i] * a[i + 1] <= 0: prod_minus = True break Min_abs = float("inf") Sum = 0 for num in a: Sum += abs(num) if abs(num) < Min_abs: Min_abs = abs(num) if prod_minus: print(Sum) else: print(Sum - 2 * Min_abs)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) l = list(map(int, input().split())) if n == 1: print(l[0]) exit() l = sorted(l) s = abs(l[0] - l[-1]) for i in range(1, n - 1): s += abs(l[i]) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = [int(s) for s in input().split()] if n == 1: print(a[0]) exit() mi, po, sum, mix = 0, 0, 0, 1000000000.0 + 1 for i in a: if i < 0: mi += 1 if i >= 0: po += 1 sum += abs(i) mix = min(mix, abs(i)) if mi and po: print(sum) else: print(sum - mix * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) lis = [int(x) for x in input().split()] total = 0 l_negative = -1000000000000 s_positive = 1000000000000 for i in lis: if i <= 0: l_negative = max(l_negative, i) if i >= 0: s_positive = min(s_positive, i) total += abs(i) if l_negative == -1000000000000: total -= 2 * s_positive if s_positive == 1000000000000: total += 2 * l_negative if n == 1: print(lis[0]) else: print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) lis = list(map(int, input().split())) pos = [] neg = [] for i in lis: if i >= 0: pos.append(i) else: neg.append(i) pos.sort(reverse=True) neg.sort(reverse=True) if n == 1: print(lis[0]) exit() if len(pos) > 0: if len(neg) > 0: print(sum(pos) - sum(neg)) else: print(sum(pos) - 2 * min(pos)) else: print(2 * max(neg) - sum(neg))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) mina = abs(a[0]) summ = 0 okeye = False mosbat = 0 for adad in a: if abs(adad) < mina: mina = abs(adad) summ += abs(adad) if adad == 0: okeye = True elif adad > 0: mosbat += 1 if n == 1: print(a[0]) elif okeye or 0 < mosbat < n: print(summ) else: print(summ - 2 * mina)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) arr = list(map(int, input().split())) sum = 0 for i in range(n): sum += abs(arr[i]) ans = 0 if n == 1: print(arr[0]) else: for i in range(n - 1): ans = max(ans, sum - abs(arr[i]) - abs(arr[i + 1]) + abs(arr[i] - arr[i + 1])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) H = list(map(int, input().split())) d = 0 f = 0 m = 10**9 + 7 if n > 1: for i in range(n): if H[i] < 0: f += 1 d += abs(H[i]) if abs(H[i]) < m: m = abs(H[i]) if f == 0 or f == n: print(d - m * 2) else: print(d) else: print(H[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) s = list(map(int, input().split())) s.sort() ans = 0 if n == 1: ans = s[0] elif s[0] >= 0: ans = sum(s[1:]) - s[0] elif s[-1] < 0: ans = -sum(s[0 : n - 1]) + s[-1] else: for i in range(n): s[i] = abs(s[i]) ans = sum(s) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) exit(0) min_idx = 0 min_res = a[0] max_idx = 0 max_res = a[0] for i in range(1, n): if a[i] < min_res: min_idx, min_res = i, a[i] if a[i] > max_res: max_idx, max_res = i, a[i] res = 0 for i in range(n): if i != min_idx and i != max_idx: res += abs(a[i]) if min_res == max_res: res -= abs(min_res) print(max_res + res - min_res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) exit() if max(a) > 1 and min(a) < 1: print(sum(abs(x) for x in a)) else: print(sum(abs(x) for x in a) - 2 * min(abs(x) for x in a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: exit(print(*a)) pos, neg = False, False mn = 10**9 + 5 ans = 0 for i in a: pos |= i >= 0 neg |= i <= 0 ans += abs(i) mn = min(abs(i), mn) if pos and neg: print(ans) else: print(ans - 2 * mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
for _ in range(1): n = int(input()) arr = list(map(int, input().split())) s = 0 for i in range(n): s += abs(arr[i]) if min(arr) < 0 and max(arr) >= 0: print(s) continue if n == 1: print(arr[0]) continue d = 999999999999999 for i in range(1, n): d = min(d, abs(arr[i]) + abs(arr[i - 1]) - abs(arr[i] - arr[i - 1])) print(s - d)
FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) xs = [int(x) for x in input().split()] prefix = [(-1) for i in range(n)] suffix = [(-1) for i in range(n)] prefix[0] = 0 pre_has_neg = [(False) for i in range(n)] suffix[-1] = 0 suf_has_neg = [(False) for i in range(n)] for i in range(n): if i == 0: prefix[i] = xs[i] else: prefix[i] = prefix[i - 1] + xs[i] for i in reversed(range(n)): if i == n - 1: suffix[i] = xs[i] else: suffix[i] = suffix[i + 1] + xs[i] for i in range(n): if i == 0: pre_has_neg[i] = xs[i] <= 0 else: pre_has_neg[i] = pre_has_neg[i - 1] or xs[i] <= 0 for i in reversed(range(n)): if i == n - 1: suf_has_neg[i] = xs[i] <= 0 else: suf_has_neg[i] = suf_has_neg[i + 1] or xs[i] <= 0 prebignum = [None for i in range(n)] sufbignum = [None for i in range(n)] for i in range(n): if i == 0: prebignum[i] = xs[i] else: prebignum[i] = min(prebignum[i - 1], xs[i]) for i in reversed(range(n)): if i == n - 1: sufbignum[i] = xs[i] else: sufbignum[i] = min(sufbignum[i + 1], xs[i]) neg_pre = [(100000) for i in range(n)] neg_suf = [(100000) for i in range(n)] for i in range(n): if i == 0: neg_pre[i] = min(xs[i], -xs[i]) else: neg_pre[i] = neg_pre[i - 1] + min(xs[i], -xs[i]) for i in reversed(range(n)): if i == n - 1: neg_suf[i] = min(xs[i], -xs[i]) else: neg_suf[i] = neg_suf[i + 1] + min(xs[i], -xs[i]) ans = -100000000000000000 for i in range(n): tans = xs[i] if i == 0: pass elif pre_has_neg[i - 1]: tans -= neg_pre[i - 1] else: tans += prefix[i - 1] tans -= prebignum[i - 1] * 2 if i == n - 1: pass elif suf_has_neg[i + 1]: tans -= neg_suf[i + 1] else: tans += suffix[i + 1] tans -= sufbignum[i + 1] * 2 ans = max(ans, tans) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys n = int(input()) A = list(map(int, input().split())) if n == 1: print(A[0]) sys.exit(0) suma = 0 mine = 1000000001 d = False for i in range(0, n): mine = min(mine, abs(A[i])) suma += abs(A[i]) if i > 0 and (A[i] >= 0 and A[i - 1] < 0 or A[i] < 0 and A[i - 1] >= 0): d = True if d: print(suma) else: print(suma - 2 * mine)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) n = ii() a = li() b = [abs(x) for x in a] if n == 1: ans = a[0] elif all(x > 0 for x in a) or all(x < 0 for x in a): b.sort() ans = sum(b) - 2 * b[0] else: ans = sum(b) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) exit() m, M = min(a), max(a) if m >= 0: print(sum(a) - 2 * m) elif M < 0: print(-sum(a) + 2 * M) else: print(sum(abs(ai) for ai in a))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) if n == 1: print(input()) else: a = [int(x) for x in input().split()] b = [abs(x) for x in a] if min(a) >= 0: print(sum(b) - 2 * min(a)) elif max(a) <= 0: print(sum(b) + 2 * max(a)) else: print(sum(b))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) a.sort() ans = 0 ck = 0 d1 = -10000000000 d2 = 10000000000 for i in a: ans += abs(i) d1 = max(d1, i) d2 = min(d2, i) if i == 0: ck = 1 if ck == 1: print(ans) elif n == 1: print(a[0]) else: ans -= abs(d1) + abs(d2) ans += abs(d1 - d2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) a.sort() x = 0 y = 0 s = 0 if n == 1: print(a[0]) exit(0) for i in a: if i > 0: x += 1 if i < 0: y += 1 s = s + abs(i) if x == n or y == n: if x == n: print(s - 2 * a[0]) if y == n: print(s + 2 * a[n - 1]) else: print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
from sys import stdin input = stdin.readline n = int(input()) lst = sorted(map(int, input().split())) if n == 1: print(lst[0]) exit() elif lst[-1] <= 0: s = abs(sum(lst)) + 2 * lst[-1] elif lst[0] >= 0: s = sum(lst) - 2 * lst[0] else: s = 0 for i in lst: s += abs(i) print(s)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) minval = abs(a[0]) havepos = 0 haveneg = 0 ans = 0 if n == 1: print(a[0]) else: for i in range(n): minval = min(abs(a[i]), minval) havepos = havepos or a[i] >= 0 haveneg = haveneg or a[i] <= 0 ans += abs(a[i]) if haveneg and havepos: print(ans) else: print(ans - 2 * minval)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) l = list(map(int, input().split())) fl = 0 f = 0 s = 0 for i in l: if i <= 0: fl = 1 if i >= 0: f = 1 s = s + abs(i) x = min(l) y = max(l) if n == 1: print(l[0]) elif f == 1 and fl == 1: print(s) elif fl == 1: print(s - 2 * abs(y)) else: print(s - 2 * x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n, minus = int(input()), 100000000000000000000 x = [int(x) for x in input().split()] if n == 1: print(x[0]) exit(0) if min(x) > 0: for i in range(1, n): if x[i] - x[i - 1] <= 0 or x[i - 1] - x[i] <= 0: minus = min(minus, abs(x[i]) + abs(x[i - 1]) - abs(x[i] - x[i - 1])) if max(x) < 0: for i in range(1, n): if x[i] - x[i - 1] >= 0 or x[i - 1] - x[i] >= 0: minus = min(minus, abs(x[i]) + abs(x[i - 1]) - abs(x[i] - x[i - 1])) if min(x) <= 0 and max(x) >= 0: minus = 0 print(sum(map(abs, x)) - minus)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) ans = 0 i = 0 l = list(map(int, input().split())) t = 0 if n == 1: print(l[0]) else: while i < n: if l[i] >= 0: c = 0 t += 1 while l[i] >= 0: c += l[i] i += 1 if i == n: break ans += c else: c = 0 t += 1 while l[i] < 0: c -= l[i] i += 1 if i == n: break ans += c if t > 1: print(ans) elif l[0] >= 0: print(ans - 2 * min(l)) else: print(ans + 2 * max(l))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) x = [int(x) for x in input().split()] result = 0 i = 1 j = n - 2 k = 0 l = 0 x.sort() def check(x): a = 0 for i in range(0, n): if x[i] >= 0: a = x[i] return a if x[0] < 0 and x[n - 1] <= 0: result = x[n - 1] while j >= 0: result = result - x[j] j -= 1 elif x[0] < 0 and x[n - 1] >= 0: result = check(x) k = x.index(result) while k != 0: result = result - x[k - 1] k -= 1 x.append(result) k = x.index(check(x)) while k != n: l = l - x[k + 1] k += 1 result = abs(l) else: result = x[0] while i <= n - 1: result = result - x[i] i += 1 result = abs(result) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = [int(x) for x in input().split(" ")] if len(a) == 1: print(a[0]) exit() a.sort() if all(x > 0 for x in a): print(sum(a) - a[0] * 2) exit() if all(x < 0 for x in a): print(-sum(a) + a[-1] * 2) exit() print(sum(abs(x) for x in a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) x = 0 cp = 0 cn = 0 a = [int(i) for i in input().split()] sum1 = 0 for i in range(n): sum1 += abs(a[i]) if i == 0: x = abs(a[i]) else: x = min(x, abs(a[i])) if a[i] > 0: cp = 1 elif a[i] < 0: cn = 1 else: cp = 1 cn = 1 if n == 1: print(a[0]) elif cp == 1 and cn == 1: print(sum1) else: print(sum1 - 2 * x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = [int(w) for w in input().split()] s = sum(abs(x) for x in a) if len(a) == 1: print(a[0]) elif all(x > 0 for x in a) or all(x < 0 for x in a): print(max(abs(s - 2 * abs(x)) for x in a)) else: print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) if n == 1: print(int(input())) exit(0) a, b = [], [] for i in map(int, input().split()): if i > 0: a.append(i) elif i < 0: b.append(i) if len(a) != 0 and len(b) != 0: print(sum(a) - sum(b)) elif len(a) + len(b) != n: print(sum(a) - sum(b)) elif len(a) == 0: print(max(b) * 2 - sum(b)) else: print(sum(a) - min(a) * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) a.sort() if n == 1: print(a[0]) else: i = 0 while i < n and a[i] <= 0: i += 1 if i == 0: print(sum(a) - 2 * a[0]) elif i == n: print(2 * a[-1] - sum(a)) else: print(sum(a[i:]) - sum(a[:i]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) exit(0) hasp, hasn = False, False for aa in a: hasp = hasp or aa > 0 hasn = hasn or aa < 0 if hasn and hasp: print(sum(list(map(abs, a)))) elif hasn or hasp: print(sum(list(map(abs, a))) - 2 * min(list(map(abs, a)))) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input().strip()) arr = [int(__) for __ in input().strip().split()] if n == 1: print(arr[0]) else: ne = 0 for i in arr: if i < 0: ne += 1 if ne == len(arr): arr = [abs(i) for i in arr] print(sum(arr) - 2 * min(arr)) else: mi = min(arr) arr = [abs(i) for i in arr] if mi < 0: print(sum(arr)) else: print(sum(arr) - 2 * mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
def abs(a): if a < 0: return -a return a def min(a, b): if a < b: return a return b def max(a, b): if b > a: return b return a n = int(input()) a = input() a = list(map(int, a.split(" "))) if n == 1: print(a[0]) exit() pos = int(0) neg = int(0) mn = int(10000000010) mx = int(-1000000010) s = int(0) for i in range(n): if a[i] >= 0: pos += 1 elif a[i] <= 0: neg += 1 mn = min(mn, abs(a[i])) mx = max(mx, abs(a[i])) s += abs(a[i]) if pos == n or n == neg: print(s - 2 * mn) else: print(s)
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
N = int(input()) A = [int(x) for x in input().split()] B = [abs(x) for x in A] if N == 1: print(A[0]) exit(0) s = sum(B) if any(x >= 0 for x in A) and any(x <= 0 for x in A): print(s) else: print(s - 2 * min(B))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) val = sorted(map(int, input().split())) sval = sum(map(abs, val)) if n == 1: print(val[0]) elif val[0] > 0: print(sval - val[0] * 2) elif val[-1] < 0: print(val[-1] * 2 + sval) else: print(sval)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(sum(a)) return mn = min(a) mx = max(a) s = 0 for i in a: s += abs(i) print(s - abs(mn) - abs(mx) + mx - mn)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) s = list(map(int, input().split())) ans = 0 smax = -(10**9) smin = 10**9 for i in s: if i > smax: smax = i if i < smin: smin = i f1 = False f2 = False for i in s: if not f1 and i == smax: ans += smax f1 = True continue if not f2 and i == smin: ans -= smin f2 = True continue if i > 0: ans += i else: ans -= i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) s = list(map(int, input().split())) if n == 1: exit(print(s[0])) f = [] if any(i <= 0 for i in s) and any(i >= 0 for i in s): exit(print(sum(map(abs, s)))) s.sort(key=abs) cs = s[0] for i in s[1:]: cs -= i print(abs(cs))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys def abs_sum(lis): ans = 0 for item in lis: ans += abs(item) return ans n = int(input()) lis = [int(x) for x in input().split()] if n == 1: print(lis[0]) sys.exit() a = abs_sum(lis) b = sum(lis) if a != b and a != -b: print(a) elif b >= 0: minn = min(lis) print(a - 2 * minn) else: maxx = max(lis) print(a + 2 * maxx)
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) s = 0 if max(a) <= 0: print(abs(sum(a)) + 2 * max(a)) elif min(a) >= 0: print(abs(abs(sum(a)) - 2 * min(a))) else: for i in a: s += abs(i) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
tot = int(input()) arr = [int(x) for x in input().split()] n, p, z = 0, 0, 0 for i in arr: if i > 0: p += 1 elif i < 0: n += 1 else: z += 1 ans = 0 for i in arr: ans += abs(i) if tot == 1: print(arr[0]) elif p == 0 or n == 0: mi = min(arr) if mi < 0: mi = max(arr) if mi > 0: print(ans - 2 * mi) else: print(ans + 2 * mi) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) p = 0 if n <= 1: print(a[0]) else: for j in range(n): if a[j] < 0: p += 1 if p == 0: print(sum(a) - 2 * min(a)) elif p == n: print(abs(sum(a)) - 2 * abs(max(a))) else: s = 0 for j in range(n): s += abs(a[j]) print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) b = list(map(int, input().split())) if n == 1: print(b[0]) exit(0) min1 = min(b) max1 = max(b) s = 0 for i in range(n): s = s + abs(b[i]) print(s + max1 - min1 - abs(max1) - abs(min1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) arr = [int(x) for x in input().split()] if n == 1: print(arr[0]) exit() sum = sum(abs(x) for x in arr) if not any(x < 0 for x in arr): sum = sum - 2 * min(arr) if not any(x >= 0 for x in arr): sum = sum - 2 * abs(max(arr)) print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
N = int(input()) A = list(map(int, input().split())) p = 0 n = 0 for i in A: if i >= 0: p += 1 elif i < 0: n += 1 if N == 0: print(0) elif N == 1: print(A[0]) elif p > 0 and n > 0: for i in range(N): if A[i] < 0: A[i] = -1 * A[i] print(sum(A)) elif p == len(A): x = A.index(min(A)) if x == 0: print(sum(A) - 2 * A[0]) elif x == N - 1: print(sum(A) - 2 * A[-1]) else: print(sum(A) - 2 * min(A)) elif n == len(A): for i in range(N): A[i] = -1 * A[i] x = A.index(min(A)) if x == 0: print(sum(A) - 2 * A[0]) elif x == N - 1: print(sum(A) - 2 * A[-1]) else: print(sum(A) - 2 * min(A))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) x = list(map(int, input().split())) ans, mi = 0, 1234567654321 for i in range(n): ans += abs(x[i]) if i > 0: mi = min(mi, abs(x[i]) + abs(x[i - 1]) - abs(x[i] - x[i - 1])) if n == 1: print(x[i]) else: print(ans - mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) elif n == 2: print(max(a[0] - a[1], a[1] - a[0])) else: x = min(a) y = max(a) a = list(map(abs, a)) print(sum(a) - (2 * min(a) if x * y > 0 else 0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) A = list(map(int, input().split())) if n == 1: print(A[0]) elif n == 2: print(abs(A[0] - A[1])) else: SUM = 0 for i in range(n): SUM += abs(A[i]) ANS = 0 for i in range(n - 1): if ANS < SUM - abs(A[i]) - abs(A[i + 1]) + abs(A[i] - A[i + 1]): ANS = SUM - abs(A[i]) - abs(A[i + 1]) + abs(A[i] - A[i + 1]) print(ANS)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
def solve(): n = int(input()) worms = list(map(int, input().split())) if n == 1: return worms[0] if len(list(filter(lambda x: x >= 0, worms))) == 0: worms = list(map(abs, worms)) minimum = min(worms) return sum(worms) - 2 * minimum minimum = min(worms) worms_sum = abs(sum(list(map(abs, worms)))) - abs(minimum) return abs(worms_sum - minimum) print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) exit() if min(a) > 0: print(sum(a) - 2 * min(a)) elif max(a) < 0: print(-sum(a) + 2 * max(a)) else: x = 0 for i in range(n): x += abs(a[i]) print(x)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
def solve(a): if len(a) == 1: return a[0] if max(a) < 0: return 2 * max(a) - sum(a) elif min(a) >= 0: return sum(a) - 2 * min(a) else: return sum(abs(x) for x in a) n = int(input()) a = list(map(int, input().split())) print(solve(a))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) elif all(ele > 0 for ele in a): s = sum(a) - 2 * min(a) print(s) elif all(ele < 0 for ele in a): s = abs(sum(a)) - 2 * abs(max(a)) print(s) else: ans = 0 for ele in a: ans += abs(ele) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) arr = list(map(int, input().split())) arr.sort() s = 0 if n == 1: s = arr[0] elif arr[-1] < 0: s = abs(sum(arr)) - abs(arr[-1]) * 2 elif arr[0] < 0: for i in range(len(arr)): s += abs(arr[i]) else: s = sum(arr) - 2 * arr[0] print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
def mi(): return map(int, input().split()) n = int(input()) a = list(mi()) cp, cn = 0, 0 for i in a: if i >= 0: cp += 1 if i <= 0: cn += 1 s = 0 if n == 1: print(a[0]) exit(0) for i in a: s += abs(i) if cn and cp: print(s) else: m = -1e100 for i in a: m = max(m, s - 2 * abs(i)) print(m)
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
from sys import stdin input = stdin.buffer.readline n = int(input()) (*a,) = map(int, input().split()) x = max(a) ans = x a.remove(x) if n > 1: x = min(a) ans -= x a.remove(x) for i in a: ans += abs(i) print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
n = int(input()) a = list(map(int, input().split())) if n == 1: print(a[0]) else: s = 0 a.sort() pos = 0 neg = 0 k = 1 for i in range(n): if a[i] > 0: pos += 1 if a[i] < 0: neg += 1 s += abs(a[i]) if pos == n: print(s - 2 * a[0]) elif neg == n: print(s + 2 * a[n - 1]) else: print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ slimes in a row. Each slime has an integer value (possibly negative or zero) associated with it. Any slime can eat its adjacent slime (the closest slime to its left or to its right, assuming that this slime exists). When a slime with a value $x$ eats a slime with a value $y$, the eaten slime disappears, and the value of the remaining slime changes to $x - y$. The slimes will eat each other until there is only one slime left. Find the maximum possible value of the last slime. -----Input----- The first line of the input contains an integer $n$ ($1 \le n \le 500\,000$) denoting the number of slimes. The next line contains $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$), where $a_i$ is the value of $i$-th slime. -----Output----- Print an only integer — the maximum possible value of the last slime. -----Examples----- Input 4 2 1 2 1 Output 4 Input 5 0 -1 -1 -1 -1 Output 4 -----Note----- In the first example, a possible way of getting the last slime with value $4$ is: Second slime eats the third slime, the row now contains slimes $2, -1, 1$ Second slime eats the third slime, the row now contains slimes $2, -2$ First slime eats the second slime, the row now contains $4$ In the second example, the first slime can keep eating slimes to its right to end up with a value of $4$.
l = int(input()) a = [*map(int, input().split())] p = n = 0 for i in a: if i > 0: p += 1 else: n += 1 ans = 0 if p and n: a.sort() for i in a: if i < 0: ans -= i else: ans += i elif p: a.sort() ans -= a[0] for i in a[1:]: ans += i else: a.sort(reverse=True) ans += a[0] for i in a[1:]: ans -= i print(ans if l > 1 else a[0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER