description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
t = input() A = input().split() sum = 0 count = 0 found = False A.sort() for i in range(len(A)): A[i] = int(A[i]) sum += A[i] if A[0] != 0: print(str(-1)) exit() if sum % 3 == 0: if sum != 0: for i in reversed(A): print(i, end="") exit() else: print(0) exit() else: target = sum % 3 for i in range(len(A)): if A[i] % 3 == target: A.pop(i) found = True break if found == False: k = 0 while k < len(A): if A[k] % 3 == 1 or A[k] % 3 == 2: A.pop(k) count += 1 k -= 1 found = True if count == 2: break k += 1 if found == True: sum = 0 for i in range(len(A)): sum += A[i] if sum == 0: print(0) else: for j in reversed(A): print(j, end="") exit() else: print(str(-1))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, input().split())) for _ in range(1): n = nmbr() a = sorted(lst(), reverse=True) sm = sum(a) ans = [] if a[-1] != 0: print(-1) continue r0 = [] r1 = [] r2 = [] for v in a: if v % 3 == 0: r0 += [v] elif v % 3 == 1: r1 += [v] elif v % 3 == 2: r2 += [v] if sm % 3 == 0: for v in a: ans += [v] elif sm % 3 == 1: time = 0 rem = rem1 = rem2 = -1 if r1: rem = r1[-1] time = 1 elif len(r2) >= 2: rem1 = r2[-1] rem2 = r2[-2] time = 2 if time == 0: print(-1) continue if time == 1: r1.pop() else: r2.pop() r2.pop() for v in r0: ans += [v] for v in r1: ans += [v] for v in r2: ans += [v] elif sm % 3 == 2: time = 0 rem = rem1 = rem2 = -1 if r2: rem = r2[-1] time = 1 elif len(r1) >= 2: rem1 = r1[-1] rem2 = r1[-2] time = 2 if time == 0: print(-1) continue if time == 1: r2.pop() else: r1.pop() r1.pop() for v in r0: ans += [v] for v in r1: ans += [v] for v in r2: ans += [v] if ans[0] == ans[-1] == 0: print(0) else: for v in sorted(ans, reverse=True): stdout.write(str(v))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR LIST VAR FOR VAR VAR VAR LIST VAR FOR VAR VAR VAR LIST VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR LIST VAR FOR VAR VAR VAR LIST VAR FOR VAR VAR VAR LIST VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) x = list(map(int, input().strip().split())) x.sort(reverse=True) y = x[::-1] if 0 not in x: print(-1) elif max(x) == 0: print(0) else: if sum(x) % 3 == 1: flag_found_1 = False for i in y: if i % 3 == 1: x.remove(i) flag_found_1 = True break flag_found_2 = 0 if not flag_found_1: for i in y: if i % 3 == 2 and flag_found_2 == 1: x.remove(i) flag_found_2 = True break elif i % 3 == 2 and flag_found_2 == 0: x.remove(i) flag_found_2 = 1 if not flag_found_2 and not flag_found_1: print(-1) if sum(x) % 3 == 2: flag_found_1 = False for i in y: if i % 3 == 2: x.remove(i) flag_found_1 = True break flag_found_2 = 0 if not flag_found_1: for i in y: if i % 3 == 1 and flag_found_2 == 1: x.remove(i) flag_found_2 = True break elif i % 3 == 1 and flag_found_2 == 0: x.remove(i) flag_found_2 = 1 if not flag_found_2 and not flag_found_1: print(-1) if max(x) == 0: print(0) else: print("".join(map(str, x)))
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 NUMBER ASSIGN VAR VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def vyvod(sp): if set(sp) == {0}: print(0) exit() sp.sort(reverse=True) for i in sp: print(i, end="") exit() n = int(input()) sp = list(map(int, input().split())) d = sum(sp) % 3 if sp.count(0) == 0: print(-1) exit() if d == 0: vyvod(sp) elif d == 1: sp.sort() count2 = 0 sum1 = -1 sum2 = [] for i in sp: if i % 3 == 1 and sum1 == -1: sum1 = i elif i % 3 == 2 and count2 < 2: count2 += 1 sum2.append(i) if sum1 == -1 and len(sum2) < 2: print(-1) elif sum1 == -1: del sp[sp.index(sum2[0])] del sp[sp.index(sum2[1])] vyvod(sp) elif len(sum2) < 2: del sp[sp.index(sum1)] vyvod(sp) else: f = min(sum1, sum(sum2)) if f == sum1: del sp[sp.index(sum1)] else: del sp[sp.index(sum2[0])] del sp[sp.index(sum2[1])] vyvod(sp) else: sp.sort() count1 = 0 sum2 = -1 sum1 = [] for i in sp: if i % 3 == 2 and sum2 == -1: sum2 = i elif i % 3 == 1 and count1 < 2: count1 += 1 sum1.append(i) if sum2 == -1 and len(sum1) < 2: print(-1) elif sum2 == -1: del sp[sp.index(sum1[0])] del sp[sp.index(sum1[1])] vyvod(sp) elif len(sum1) < 2: del sp[sp.index(sum2)] vyvod(sp) else: f = min(sum2, sum(sum1)) if f == sum2: del sp[sp.index(sum2)] else: del sp[sp.index(sum1[0])] del sp[sp.index(sum1[1])] vyvod(sp) exit()
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL 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 BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) c = input().split() if "0" not in c: print(-1) else: c.sort(reverse=True) d = list(map(int, c)) if sum(d) % 3 == 0: print(int("".join(c))) elif sum(d) % 3 == 1: if "1" in c: c.remove("1") print(int("".join(c))) elif "4" in c: c.remove("4") print(int("".join(c))) elif "7" in c: c.remove("7") print(int("".join(c))) else: k = [] for i in reversed(c): if i == "2" or i == "5" or i == "8": k.append(i) if len(k) > 1: break if len(k) > 1: c.remove(k[0]) c.remove(k[1]) print(int("".join(c))) else: print(0) elif sum(d) % 3 == 2: if "2" in c: c.remove("2") print(int("".join(c))) elif "5" in c: c.remove("5") print(int("".join(c))) elif "8" in c: c.remove("8") print(int("".join(c))) else: k = [] for i in reversed(c): if i == "1" or i == "4" or i == "7": k.append(i) if len(k) > 1: break if len(k) > 1: c.remove(k[0]) c.remove(k[1]) print(int("".join(c))) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) a = sorted([int(x) for x in input().split()]) s = sum(a) if a[0] != 0: print(-1) exit(0) if s % 3 != 0: for x in a: if x % 3 == s % 3: a.remove(x) break else: for x in a: if x % 3: a.remove(x) break for x in a: if x % 3: a.remove(x) break while len(a) > 1 and a[-1] == 0: a.pop() a = a[::-1] print("".join(str(x) for x in a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = input() digit = sorted(map(int, input().split()), reverse=True) if 0 not in digit: print(-1) else: mod = sum(digit) % 3 if mod: equal = [d for d in digit if d % 3 == mod] other = [d for d in digit if d % 3 not in [0, mod]] if equal: digit.remove(equal[-1]) elif len(other) >= 2: digit.remove(other[-1]) digit.remove(other[-2]) else: digit = [-1] if digit[0] == 0: digit = [0] print("".join(map(str, digit)))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER LIST NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) ans = list(map(int, input().split())) if 0 not in ans: print(-1) exit() ans.sort(reverse=True) if ans.count(0) == n: print(0) elif not sum(ans) % 3: print("".join(map(str, ans))) else: flag = 0 x = sum(ans) % 3 for i in range(n - 1, -1, -1): if ans[i] % 3: if x == ans[i] % 3: flag = 1 index = i break if flag: l = [] for i in range(n): if i != index: l.append(ans[i]) if l.count(0) == len(l): print(0) else: print("".join(map(str, l))) else: index = [] for i in range(n - 1, -1, -1): if ans[i] % 3: index.append(i) if len(index) == 1: print(-1) else: l = [] for i in range(n): if i == index[0] or i == index[1]: continue else: l.append(ans[i]) if l.count(0) == len(l): print(0) else: print("".join(map(str, l)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) arr = list(map(int, input().strip().split())) arr = sorted(arr, reverse=True) x = sum(arr) if x % 3 != 0: if x % 3 == 1: a = [] for i in range(n - 1, -1, -1): if arr[i] % 3 == 1 and x % 3 == 1: x -= arr[i] else: a.append(arr[i]) if x % 3 == 1: a = [] for i in range(n - 1, -1, -1): if arr[i] % 3 == 2 and x % 3 != 0: x -= arr[i] else: a.append(arr[i]) arr = a else: arr = a else: a = [] for i in range(n - 1, -1, -1): if arr[i] % 3 == 2 and x % 3 == 2: x -= arr[i] else: a.append(arr[i]) if x % 3 == 2: a = [] for i in range(n - 1, -1, -1): if arr[i] % 3 == 1 and x % 3 != 0: x -= arr[i] else: a.append(arr[i]) arr = a else: arr = a arr = sorted(arr, reverse=True) if x % 3 != 0: print(-1) elif arr[len(arr) - 1] != 0: print(-1) else: s = "" for i in range(len(arr)): s += str(arr[i]) if s[0] == "0": print(0) else: print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) v = list(map(int, input().split())) fv = [0] * 10 for x in v: fv[x] += 1 if fv[0] == 0: print(-1) else: r = sum(v) % 3 ok, i = r == 0, r while not ok and i <= 9: if fv[i] > 0: ok = 1 fv[i] -= 1 i += 3 if not ok: i = 3 - r while ok < 2 and i <= 9: while ok < 2 and fv[i] > 0: ok += 1 fv[i] -= 1 i += 3 pr = 0 for i in range(9, -1, -1): if i > 0 and fv[i] > 0: pr = 1 if i > 0 or pr == 1: print(str(i) * fv[i], end="") 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 ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR WHILE VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) z = list(map(int, input().split())) a = [0] * 10 for i in z: a[i] += 1 if a[0] == 0: print(-1) else: b = sum(z) % 3 c = True if b == 2: if a[2] != 0: a[2] -= 1 elif a[5] != 0: a[5] -= 1 elif a[8] != 0: a[8] -= 1 elif a[1] > 0: if a[1] > 1: a[1] -= 2 elif a[4] > 0: a[1] -= 1 a[4] -= 1 elif a[7] > 0: a[1] -= 1 a[7] -= 1 else: c = False elif a[4] > 0: if a[4] > 1: a[4] -= 2 elif a[7] > 0: a[4] -= 1 a[7] -= 1 else: c = False elif a[7] > 1: a[7] -= 2 else: c = False elif b == 1: if a[1] != 0: a[1] -= 1 elif a[4] != 0: a[4] -= 1 elif a[7] != 0: a[7] -= 1 elif a[2] > 0: if a[2] > 1: a[2] -= 2 elif a[5] > 0: a[2] -= 1 a[5] -= 1 elif a[8] > 0: a[2] -= 1 a[8] -= 1 else: c = False elif a[5] > 0: if a[5] > 1: a[5] -= 2 elif a[8] > 0: a[5] -= 1 a[8] -= 1 else: c = False elif a[8] > 1: a[8] -= 2 else: c = False if a[0] == sum(a): print(0) else: print(*[(str(i) * a[i]) for i in range(9, -1, -1)], sep="" if c else -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 BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR STRING NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
x = input() y = input().split(" ") z = [int(i) for i in y] z.sort() if z[0] != 0: print(-1) else: list3 = [] list2 = [] list1 = [] for i in z: if i == 1 or i == 4 or i == 7: list1.append(i) elif i == 2 or i == 5 or i == 8: list2.append(i) elif i == 3 or i == 6 or i == 9 or i == 0: list3.append(i) if sum(z) % 3 == 1: if len(list1) >= 1: list1.remove(list1[0]) elif len(list2) >= 2: list2.remove(list2[0]) list2.remove(list2[0]) if sum(z) % 3 == 2: if len(list2) >= 1: list2.remove(list2[0]) elif len(list1) >= 2: list1.remove(list1[0]) list1.remove(list1[0]) z = list1 + list2 + list3 z.sort() z.reverse() z = [str(i) for i in z] result = "".join(z) if result[0] == "0": print("0") else: print(result)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
from sys import stdin, stdout def main(): from sys import stdin, stdout N = int(stdin.readline()) arr = list(map(int, stdin.readline().split())) arr.sort() if arr[0] != 0: stdout.write("-1\n") return res = sum(arr) % 3 if res: tmp1 = list(filter(lambda x: x % 3 == 1, arr)) tmp2 = list(filter(lambda x: x % 3 == 2, arr)) tmp3 = list(filter(lambda x: x % 3 == 0, arr)) if res & 1: if len(tmp1): tmp1 = tmp1[1:] else: tmp2 = tmp2[2:] elif len(tmp2): tmp2 = tmp2[1:] else: tmp1 = tmp1[2:] arr = sorted(tmp1 + tmp2 + tmp3) if arr[-1]: for i in arr[::-1]: stdout.write(str(i)) stdout.write("\n") else: stdout.write("0\n") main()
FUNC_DEF 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 NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = 1 for test in range(n): a = int(input()) lst = list(map(int, input().split())) lst.sort(reverse=True) s = 0 for el in lst: s += el if s % 3 == 2: c = False for i in range(a - 1, -1, -1): if lst[i] % 3 == 2: lst.remove(lst[i]) c = True break if not c: m = False for i in range(a - 1, -1, -1): if lst[i] % 3 == 1: lst.remove(lst[i]) if m: break m = True if s % 3 == 1: c = False for i in range(a - 1, -1, -1): if lst[i] % 3 == 1: lst.remove(lst[i]) c = True break if not c: m = False for i in range(a - 1, -1, -1): if lst[i] % 3 == 2: lst.remove(lst[i]) if m: break m = True if lst != [0] * len(lst): if lst[-1] == 0: for el in lst: print(el, end="") else: print(-1) else: print(0)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
def ep(msg): print(str(msg)) exit() n = input() l = list(map(int, input().split())) if 0 not in l: ep(-1) if sum(l) == 0: ep(0) l.sort() r = sum(l) % 3 if r == 0: l.reverse() ep("".join(map(str, l))) i = next((i for i, v in enumerate(l) if v % 3 == r), -1) if i != -1: del l[i] else: del l[next(i for i, v in enumerate(l) if v % 3 == 3 - r)] del l[next(i for i, v in enumerate(l) if v % 3 == 3 - r)] if sum(l) == 0: ep(0) l.reverse() ep("".join(map(str, l)))
FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? You are given a set of digits, your task is to find the maximum integer that you can make from these digits. The made number must be divisible by 2, 3, 5 without a residue. It is permitted to use not all digits from the set, it is forbidden to use leading zeroes. Each digit is allowed to occur in the number the same number of times it occurs in the set. Input A single line contains a single integer n (1 ≀ n ≀ 100000) β€” the number of digits in the set. The second line contains n digits, the digits are separated by a single space. Output On a single line print the answer to the problem. If such number does not exist, then you should print -1. Examples Input 1 0 Output 0 Input 11 3 4 5 4 5 3 5 3 4 4 0 Output 5554443330 Input 8 3 2 5 1 5 2 2 3 Output -1 Note In the first sample there is only one number you can make β€” 0. In the second sample the sought number is 5554443330. In the third sample it is impossible to make the required number.
n = int(input()) freq = {} for i in range(10): freq[i] = 0 a = [int(i) for i in input().split()] total = sum(a) for i in range(n): freq[a[i]] += 1 if not freq[0]: print(-1) else: residue = total % 3 if residue: found = False while residue < 18 and not found: if residue < 10 and freq[residue] > 0: freq[residue] -= 1 found = True else: for i in range(min(9, residue - 1), -1, -1): if residue - i > 9 or i <= residue // 2: break elif freq[i] > 0 and freq[residue - i] > 0: freq[i] -= 1 freq[residue - i] -= 1 found = True break if not residue % 2 and freq[residue // 2] > 1: freq[residue // 2] -= 2 found = True residue += 3 s = "" for i in range(9, -1, -1): s += str(i) * freq[i] if int(s) > 0: print(s) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
def pw(a, x, k): if a == 0: return 0 ans = a while k > 0: ans *= x k = k - 1 return ans n, k, x = list(map(int, input().split())) a = list(map(int, input().split())) lft = [] lft.append(0) for val in a: lft.append(val) lft.append(0) rht = [] for val in lft: rht.append(val) for i in range(1, len(lft)): lft[i] = lft[i] | lft[i - 1] for i in range(len(rht) - 2, -1, -1): rht[i] = rht[i] | rht[i + 1] ans = 0 for i in range(1, len(lft) - 1): if ans < lft[i - 1] | pw(a[i - 1], x, k) | rht[i + 1]: ans = lft[i - 1] | pw(a[i - 1], x, k) | rht[i + 1] print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
import sys def get_ints(): return list(map(int, sys.stdin.readline().strip().split())) def solve(N, K, X, A): prefix, suffix = [0] * (N + 2), [0] * (N + 2) for i in range(N): prefix[i + 1] = prefix[i] | A[i] for i in range(N - 1, -1, -1): suffix[i] = suffix[i + 1] | A[i] ans = 0 val = X**K for i in range(N): ans = max(ans, prefix[i] | A[i] * val | suffix[i + 1]) return ans N, K, X = map(int, input().split()) A = get_ints() print(solve(N, K, X, A))
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = map(int, input().split()) a = list(map(int, input().split())) x = x**k pref, suff = [0] * n, [0] * n for i in range(n - 1): pref[i + 1] = a[i] | pref[i] for i in range(n - 1, 0, -1): suff[i - 1] = a[i] | suff[i] res = 0 for i in range(n): res = max(res, a[i] * x | pref[i] | suff[i]) print(res)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = list(map(int, input().split())) a = list(map(int, input().split())) c = x**k pr = [0] su = [0] b = 0 for i in range(n - 1): b = a[i] | b pr.append(b) b = 0 for i in range(n - 1, 0, -1): b = a[i] | b su.append(b) su = su[::-1] d = [] for i in range(n): d.append(pr[i] | a[i] * c | su[i]) print(max(d))
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
x, n, k = [int(x) for x in input().split()] lista = [int(x) for x in input().split()] acumuladaL = [] acumuladaR = [] mult = k**n cont = 0 for i in lista: cont |= i acumuladaL.append(cont) cont = 0 for i in lista[::-1]: cont |= i acumuladaR.append(cont) if len(lista) == 1: print(lista[0] * mult) elif len(lista) == 2: acumuladaR = acumuladaR[::-1] maximo = max([lista[0] * mult | acumuladaR[1], lista[-1] * mult | acumuladaL[-2]]) print(maximo) else: maximo = max([lista[0] * mult | acumuladaR[1], lista[-1] * mult | acumuladaL[-2]]) acumuladaR = acumuladaR[::-1] for i in range(1, len(lista) - 1): var = lista[i] * mult | acumuladaR[i + 1] | acumuladaL[i - 1] if maximo < var: maximo = var print(maximo)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
class Ortree: def __init__(self, n, As): size = 1 while n > size: size *= 2 self.size = size data = [0] * size + As[:] + [0] * (size - n) for idx in range(self.size - 1, 0, -1): idx2 = idx << 1 data[idx] = data[idx2] | data[idx2 + 1] self.data = data def update(self, idx, val): pos = idx + self.size self.data[pos] = val pos >>= 1 while pos: pos2 = pos << 1 self.data[pos] = self.data[pos2] | self.data[pos2 + 1] pos >>= 1 return self.data[1] def solve(n, k, x, As): As.sort(reverse=True) xk = x**k if n == 1: As[0] *= xk return As[0] if is_simplecase(xk, As): As[0] *= xk return cumor(As) return complexcase(n, xk, As) def cumor(As): result = 0 for a in As: result |= a return result def is_simplecase(xk, As): len0 = len(bin(As[0] * xk)) len1 = len(bin(As[1] * xk)) return len0 > len1 def complexcase(n, xk, As): len0 = len(bin(As[0] * xk)) for i, a in enumerate(As[1:], 1): if len(bin(a * xk)) < len0: end = i rest = cumor(As[end:]) break else: end = n rest = 0 ortree = Ortree(end, As[:end]) record = rest for i in range(end): score = ortree.update(i, As[i] * xk) | rest if record < score: record = score ortree.update(i, As[i]) return record n, k, x = map(int, input().split()) As = list(map(int, input().split())) print(solve(n, k, x, As))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR VAR BIN_OP LIST NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR RETURN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 VAR VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = map(int, input().split()) (*Arr,) = map(int, input().split()) Prfx = [0] * (n + 2) Sffx = [0] * (n + 2) for i in range(0, n): Prfx[i + 1] = Prfx[i] | Arr[i] Sffx[n - i - 1] = Sffx[n - i] | Arr[n - i - 1] now = x**k Res = 0 for i in range(0, n): Res = max(Prfx[i] | Arr[i] * now | Sffx[i + 1], Res) print(Res)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = map(int, input().split()) a = [None] + list(map(int, input().split())) prefix = [0] * (n + 2) suffix = [0] * (n + 2) for i in range(1, n + 1): prefix[i] = a[i] | prefix[i - 1] for i in range(n, 0, -1): suffix[i] = a[i] | suffix[i + 1] mul = x**k ans = 0 for i in range(1, n + 1): ans = max(ans, prefix[i - 1] | a[i] * mul | suffix[i + 1]) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
input_1 = input() input_1 = input_1.split() n = int(input_1[0]) k = int(input_1[1]) x = int(input_1[2]) a = input() a = a.split() a = [int(x) for x in a] maximum = 0 prefix = [] suffix = [] prefix_or = 0 suffix_or = 0 for i in range(n): prefix_or = prefix_or | a[i] prefix.append(prefix_or) for i in range(n - 1, -1, -1): suffix_or = suffix_or | a[i] suffix.append(suffix_or) or_bit = 0 if n == 1: or_bit = a[0] * x**k else: for i in range(n): if i == 0: or_bit = max(or_bit, a[0] * x**k | suffix[n - 2]) elif i == n - 1: or_bit = max(or_bit, a[n - 1] * x**k | prefix[n - 2]) else: or_bit = max(or_bit, a[i] * x**k | prefix[i - 1] | suffix[n - 2 - i]) print(or_bit)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
import sys __author__ = "kitkat" try: while True: n, k, x = map(int, input().split(" ")) val = [0] + list(map(int, input().split(" "))) res = 0 L = [(0) for i in range(200003)] R = [(0) for i in range(200003)] for i in range(1, n + 1, 1): L[i] = L[i - 1] | val[i] R[n - i + 1] = R[n - i + 2] | val[n - i + 1] for i in range(1, n + 1, 1): for j in range(k): val[i] *= x for i in range(1, n + 1, 1): res = max(res, L[i - 1] | R[i + 1] | val[i]) print(res) except EOFError: pass
IMPORT ASSIGN VAR STRING WHILE NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
import sys class IoTool: DEBUG = 0 def _reader_dbg(): with open("./input.txt", "r") as f: lines = f.readlines() for l in lines: yield l.strip() def _reader_oj(): return iter(sys.stdin.read().split("\n")) reader = _reader_dbg() if DEBUG else _reader_oj() def read(): return next(IoTool.reader) input = IoTool.read def main(): n, k, x = map(int, input().split()) a = list(map(int, input().split())) mul = 1 for i in range(k): mul *= x if n == 1: print(a[0] * mul) return pre, tail = [0] * n, [0] * n pre[0] = a[0] tail[n - 1] = a[n - 1] for i in range(1, n): pre[i] = pre[i - 1] | a[i] for i in range(n - 2, -1, -1): tail[i] = tail[i + 1] | a[i] answer = max(pre[0] * mul | tail[1], pre[n - 2] | tail[n - 1] * mul) for i in range(1, n - 1): answer = max(answer, a[i] * mul | pre[i - 1] | tail[i + 1]) print(answer) main()
IMPORT CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF FUNC_CALL VAR STRING STRING VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, p = map(int, input().split()) numbers = list(map(int, input().split())) x = [0] * (n + 10) y = [0] * (n + 10) p = p**k maxi = 0 for i in range(n): x[i + 1] = numbers[i] | x[i] for j in range(n, -1, -1): y[j - 2] = numbers[j - 1] | y[j - 1] for i in range(n): if maxi < x[i] | numbers[i] * p | y[i]: maxi = x[i] | numbers[i] * p | y[i] print(maxi)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
def main(): n, k, x = list(map(int, input().split())) aa = list(map(int, input().split())) x, lo, u = x**k, [0] * n, 0 for i, a in enumerate(aa): lo[i] = u u |= a hi, u = [], 0 for a in reversed(aa): hi.append(u) u |= a hi.reverse() for i, u, a, v in zip(list(range(n)), lo, aa, hi): aa[i] = a * x | u | v print(max(aa)) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
import sys n, k, x = [int(x) for x in sys.stdin.readline().split()] a = [int(x) for x in sys.stdin.readline().split()] tl = 0 l = [0] * (n + 1) for i in range(n - 1, -1, -1): tl |= a[i] l[i] = tl ans = 0 tr = 0 for i in range(0, n): ta = tr | a[i] * x**k | l[i + 1] ans = ans if ans > ta else ta tr |= a[i] print(ans)
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
from sys import stdin input = stdin.readline def get_or(a, b): return a | b class SegmentTree: def __init__(self, data, default=0, func=max): self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size : _size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): start += self._size stop += self._size res_left = res_right = self._default while start < stop: if start & 1: res_left = self._func(res_left, self.data[start]) start += 1 if stop & 1: stop -= 1 res_right = self._func(self.data[stop], res_right) start >>= 1 stop >>= 1 return self._func(res_left, res_right) def __repr__(self): return "SegmentTree({0})".format(self.data) def f(a, k, x): st = SegmentTree(data=a, func=get_or, default=0) n = len(a) x = pow(x, k) cmax = st.query(0, n) for i in range(len(a)): st.__setitem__(i, a[i] * x) cmax = max(cmax, st.query(0, n)) st.__setitem__(i, a[i]) return cmax n, k, x = map(int, input().strip().split()) a = list(map(int, input().strip().split())) print(f(a, k, x))
ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_DEF RETURN VAR BIN_OP VAR VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = map(int, input().strip().split()) numbers = list(map(int, input().strip().split())) from_b = [(0) for i in range(n)] from_e = [(0) for i in range(n)] or_no = [(0) for i in range(n)] for j in range(1, n): from_b[j] = from_b[j - 1] | numbers[j - 1] from_e[n - j - 1] = from_e[n - j] | numbers[n - j] for j in range(n): or_no[j] = from_b[j] | from_e[j] number = pow(x, k) maxa = 0 for j in range(n): maxa = max(maxa, numbers[j] * number | or_no[j]) print(maxa)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = map(int, input().split()) a = list(map(int, input().split())) b = list(a) bits = [0] * 64 for i in range(32): for j in range(len(b)): bits[i] += b[j] & 1 b[j] >>= 1 ans = 0 for v in a: t = v u = v * x**k s = 0 for i in range(64): b = bits[i] - (t & 1) + (u & 1) t >>= 1 u >>= 1 if b: s |= 1 << i ans = max(ans, s) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = map(int, input().split()) a = list(map(int, input().split())) L = [0] * (n + 1) R = [0] * (n + 1) for i in range(1, n + 1): L[i] = a[i - 1] | L[i - 1] for i in range(n - 1, -1, -1): R[i] = a[i] | R[i + 1] ans = max(L[i] | a[i] * x**k | R[i + 1] for i in range(n)) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
from sys import stdin, stdout def solve(n, k, x, a): s = 0 prefix = [0] * (n + 10) suffix = [0] * (n + 10) a.append(0) a.insert(0, 0) m = pow(x, k) for i in range(1, n + 1): prefix[i] = prefix[i - 1] | a[i] for i in range(n, 0, -1): suffix[i] = suffix[i + 1] | a[i] for i in range(1, n + 1): s = max(s, prefix[i - 1] | a[i] * m | suffix[i + 1]) return s def main(infile, outfile): n, k, x = list(map(int, infile.readline().split())) a = list(map(int, infile.readline().split())) outfile.write(str(solve(n, k, x, a)) + "\n") def __starting_point(): from sys import stdin, stdout main(stdin, stdout) __starting_point()
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given n numbers a_1, a_2, ..., a_{n}. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make [Image] as large as possible, where $1$ denotes the bitwise OR. Find the maximum possible value of [Image] after performing at most k operations optimally. -----Input----- The first line contains three integers n, k and x (1 ≀ n ≀ 200 000, 1 ≀ k ≀ 10, 2 ≀ x ≀ 8). The second line contains n integers a_1, a_2, ..., a_{n} (0 ≀ a_{i} ≀ 10^9). -----Output----- Output the maximum value of a bitwise OR of sequence elements after performing operations. -----Examples----- Input 3 1 2 1 1 1 Output 3 Input 4 2 3 1 2 4 8 Output 79 -----Note----- For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is $1|1|2 = 3$. For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.
n, k, x = list(map(int, input().split())) a = list(map(int, input().split())) pref = [(0) for el in a] suff = [(0) for el in a] pref[0] = a[0] for i in range(1, len(a)): pref[i] = pref[i - 1] | a[i] suff[-1] = a[-1] for i in range(len(a) - 2, -1, -1): suff[i] = suff[i + 1] | a[i] if n == 1: print(a[0] * x**k) return sol = max(a[0] * x**k | suff[1], pref[-2] | a[-1] * x**k) for i in range(1, len(a) - 1): sol = max(sol, pref[i - 1] | a[i] * x**k | suff[i + 1]) print(sol)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
C, Hr, Hb, Wr, Wb = map(int, input().split()) ans = 0 for i in range(10**5): if Wr * i <= C: ans = max(ans, Hr * i + (C - Wr * i) // Wb * Hb) for i in range(10**5): if Wb * i <= C: ans = max(ans, Hb * i + (C - Wb * i) // Wr * Hr) print(ans)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
import sys f = sys.stdin C, Hr, Hb, Wr, Wb = map(int, f.readline().strip().split()) if Hr / Wr < Hb / Wb: Hr, Hb, Wr, Wb = Hb, Hr, Wb, Wr if C % Wr == 0 and C // Wr > 0: print(C // Wr * Hr) elif C // Wr == 0: print(C // Wb * Hb) else: nmax = C // Wr pmax = nmax * Hr + (C - nmax * Wr) // Wb * Hb dmax = (C - (nmax - 0) * Wr) % Wb if Hr / Wr > Hb / Wb: dx = dmax * (Hb / Wb) / (Hr / Wr - Hb / Wb) elif Hr / Wr < Hb / Wb: dx = 0 else: dx = Wb * Wr if Wr < Wb: nmax = C // Wb pmax = nmax * Hb + (C - nmax * Wb) // Wr * Hr if Wr > Wb: nmax = C // Wr pmax = nmax * Hr + (C - nmax * Wr) // Wb * Hb if Wr > Wb and dx > 0: for k in range(1, C // Wr): if k * Wr > dx: break pk = (nmax - k) * Hr + (C - (nmax - k) * Wr) // Wb * Hb dk = (C - (nmax - k) * Wr) % Wb if pk > pmax: pmax = pk if dk == 0: break elif Wr < Wb and dx > 0: for j in range(1, C // Wb + 1): k = nmax - (C - j * Wb) // Wr if k * Wr > dx: break pk = (nmax - k) * Hr + (C - (nmax - k) * Wr) // Wb * Hb dk = (C - (nmax - k) * Wr) % Wb if pk > pmax: pmax = pk if dk == 0: break print(pmax)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
a, b, c, d, e = list(map(int, input().split(" "))) check = int(a**0.5) ans = 0 for red in range(check + 1): blue = (a - d * red) // e if blue >= 0: ans = max(ans, red * b + blue * c) for blue in range(check + 1): red = (a - e * blue) // d if red >= 0: ans = max(ans, red * b + blue * c) print(ans)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
__author__ = "trunghieu11" def calc(n, h1, h2, w1, w2): answer = 0 len = n // w1 for i in range(0, min(len, 100000) + 1): answer = max(answer, i * h1 + (n - i * w1) // w2 * h2) return answer n, hR, hB, wR, wB = map(int, input().split()) print(max(calc(n, hR, hB, wR, wB), calc(n, hB, hR, wB, wR)))
ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
C, Pr, Pb, Wr, Wb = list(map(int, input().split())) result = 0 if Wr * Wr >= C: i = 0 while Wr * i <= C: j = int((C - Wr * i) / Wb) result = max(result, Pr * i + Pb * j) i += 1 print(result) return if Wb * Wb >= C: i = 0 while Wb * i <= C: j = int((C - Wb * i) / Wr) result = max(result, Pb * i + Pr * j) i += 1 print(result) return Ab = Pb / Wb Ar = Pr / Wr if Ab < Ar: i = 0 while i * i <= C: j = int((C - Wb * i) / Wr) result = max(result, Pb * i + Pr * j) i += 1 print(result) else: i = 0 while i * i <= C: j = int((C - Wr * i) / Wb) result = max(result, Pr * i + Pb * j) i += 1 print(result)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
m, h1, h2, w1, w2 = map(int, input().split()) if h2 / w2 > h1 / w1: h1, h2 = h2, h1 w1, w2 = w2, w1 if w1**2 >= m: res = 0 for i in range(int(m**0.5 + 1)): if i * w1 <= m: new = i * h1 + (m - w1 * i) // w2 * h2 res = max(res, new) print(res) exit() res = 0 for i in range(int(m**0.5 + 5)): new_res = i * h2 + (m - i * w2) // w1 * h1 res = max(res, new_res) print(res)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
c, hr, hb, wr, wb = map(int, input().split()) s = 0 if wb < wr: hr, hb, wr, wb = hb, hr, wb, wr if wb * wb > c: for nb in range(c // wb + 1): s = max(s, nb * hb + hr * ((c - wb * nb) // wr)) else: if hr * wb < hb * wr: hr, hb, wr, wb = hb, hr, wb, wr for nb in range(min(31625, c // wb + 1)): s = max(s, nb * hb + hr * ((c - wb * nb) // wr)) print(s)
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? [Image] One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs W_{r} grams and each blue candy weighs W_{b} grams. Eating a single red candy gives Om Nom H_{r} joy units and eating a single blue candy gives Om Nom H_{b} joy units. Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat. -----Input----- The single line contains five integers C, H_{r}, H_{b}, W_{r}, W_{b} (1 ≀ C, H_{r}, H_{b}, W_{r}, W_{b} ≀ 10^9). -----Output----- Print a single integer β€” the maximum number of joy units that Om Nom can get. -----Examples----- Input 10 3 5 2 3 Output 16 -----Note----- In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
def main(): c, hr, hb, wr, wb = (int(i) for i in input().split()) if wb < c ** (1 / 2) > wr: if hr / wr < hb / wb: wr, hr, wb, hb = wb, hb, wr, hr mx = 0 for i in range(wr + 1): tmx = i * hb + (c - i * wb) // wr * hr mx = tmx if tmx > mx else mx else: if wr <= wb: wr, hr, wb, hb = wb, hb, wr, hr nr = 0 mx = 0 while nr * wr <= c: tmx = nr * hr + (c - nr * wr) // wb * hb mx = tmx if tmx > mx else mx nr += 1 print(mx) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR BIN_OP NUMBER NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. -----Input----- The first line contains two space-separated integers R, B(1 ≀ R, B ≀ 10). For 1 ≀ i ≀ R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| ≀ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. -----Output----- If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). -----Examples----- Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Output Yes Input 2 1 1 0 2 2 3 1 Output No -----Note----- For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
n, m = map(int, input().split()) for i in range(n): x, y = map(int, input().split()) for i in range(m): x, y = map(int, input().split()) print("Yes" if n == m else "No")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. -----Input----- The first line contains two space-separated integers R, B(1 ≀ R, B ≀ 10). For 1 ≀ i ≀ R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| ≀ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. -----Output----- If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). -----Examples----- Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Output Yes Input 2 1 1 0 2 2 3 1 Output No -----Note----- For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
def ccw(A, B, C): return (C[1] - A[1]) * (B[0] - A[0]) > (B[1] - A[1]) * (C[0] - A[0]) def intersect(A, B, C, D): return ccw(A, C, D) != ccw(B, C, D) and ccw(A, B, C) != ccw(A, B, D) R, B = map(int, input().split()) rs = [] bs = [] for r in range(R): rs.append(list(map(int, input().split()))) for r in range(B): bs.append(list(map(int, input().split()))) if R != B: print("No") else: def rec(at, done, remain): if at >= B: return True for b in remain: for r, d in zip(rs, done): if intersect(r, bs[d], rs[at], bs[b]): break else: ok = rec(at + 1, done + [b], remain - {b}) if ok: return True return False print(["NO", "YES"][rec(0, [], set(range(B)))])
FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR LIST VAR BIN_OP VAR VAR IF VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR LIST STRING STRING FUNC_CALL VAR NUMBER LIST FUNC_CALL VAR FUNC_CALL VAR VAR
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. -----Input----- The first line contains two space-separated integers R, B(1 ≀ R, B ≀ 10). For 1 ≀ i ≀ R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| ≀ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. -----Output----- If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). -----Examples----- Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Output Yes Input 2 1 1 0 2 2 3 1 Output No -----Note----- For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
def check(a, b): ansa = b[0] - a[0] ansb = b[1] - a[1] return [ansa, ansb] while True: try: n, m = map(int, input().split()) if n != m: a = list() b = list() for i in range(n): a.append(list(map(int, input().split()))) for i in range(m): b.append(list(map(int, input().split()))) print("No") else: a = list() b = list() for i in range(n): a.append(list(map(int, input().split()))) for i in range(m): b.append(list(map(int, input().split()))) flag = False for i in range(len(a)): for j in range(i + 1, len(a)): for k in range(len(b)): for l in range(k + 1, len(b)): ansa = check(a[i], a[j]) ansb = check(a[i], b[k]) ansc = check(a[i], b[l]) if ( ansa[0] == ansb[0] and ansa[0] == ansc[0] and ansa[0] == 0 or ansa[0] != 0 and ansb[0] != 0 and ansc[0] != 0 and ansa[1] / ansa[0] == ansb[1] / ansb[0] and ansa[1] / ansa[0] == ansc[1] / ansc[0] ): print("No") flag = True break else: continue if flag == True: break if flag == True: break if flag == True: break if flag == False: print("Yes") except EOFError: break
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN LIST VAR VAR WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. -----Input----- The first line contains two space-separated integers R, B(1 ≀ R, B ≀ 10). For 1 ≀ i ≀ R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| ≀ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. -----Output----- If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). -----Examples----- Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Output Yes Input 2 1 1 0 2 2 3 1 Output No -----Note----- For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
r, b = [int(i) for i in input().split()] for i in range(r): discard = [int(i) for i in input().split()] print("Yes" if r == b else "No")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. -----Input----- The first line contains two space-separated integers R, B(1 ≀ R, B ≀ 10). For 1 ≀ i ≀ R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| ≀ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. -----Output----- If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). -----Examples----- Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Output Yes Input 2 1 1 0 2 2 3 1 Output No -----Note----- For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
b, r = input().split() for x in range(int(b)): temp, temp = input().split() for x in range(int(r)): temp, temp = input().split() if b == r: print("Yes") else: print("No")
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign R Rebel spaceships to guard B bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. -----Input----- The first line contains two space-separated integers R, B(1 ≀ R, B ≀ 10). For 1 ≀ i ≀ R, the i + 1-th line contains two space-separated integers x_{i} and y_{i} (|x_{i}|, |y_{i}| ≀ 10000) denoting the coordinates of the i-th Rebel spaceship. The following B lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. -----Output----- If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). -----Examples----- Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Output Yes Input 2 1 1 0 2 2 3 1 Output No -----Note----- For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
str1 = input() l_1 = str1.split(" ") tedad = int(l_1[0]) tedad1 = int(l_1[1]) for i in range(tedad): n = input() for i in range(tedad1): n = input() if tedad1 == tedad: print("Yes") else: print("No")
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
from sys import stdin n, m = list(map(int, stdin.readline().split())) c = {} for _ in range(m): a, b = list(map(int, stdin.readline().split())) if a - 1 not in c.keys(): c[a - 1] = [] x = b - a + (n if b < a else 0) c[a - 1].append(x) for k, l in c.items(): c[k] = min(l) + (len(l) - 1) * n toprint = [] for x in range(n): res = 0 for y, v in c.items(): s = y - x + (n if y < x else 0) res = max(res, v + s) toprint.append(res) print(*toprint)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER LIST ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled. Alice received a set of Toy Trainβ„’ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≀ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described. Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≀ i ≀ m), now at station a_i, should be delivered to station b_i (a_i β‰  b_i). <image> The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example. The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible. Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there. Input The first line contains two space-separated integers n and m (2 ≀ n ≀ 100; 1 ≀ m ≀ 200) β€” the number of stations and the number of candies, respectively. The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≀ a_i, b_i ≀ n; a_i β‰  b_i) β€” the station that initially contains candy i and the destination station of the candy, respectively. Output In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i. Examples Input 5 7 2 4 5 1 2 3 3 4 4 1 5 3 3 5 Output 10 9 10 10 9 Input 2 3 1 2 1 2 1 2 Output 5 6 Note Consider the second sample. If the train started at station 1, the optimal strategy is as follows. 1. Load the first candy onto the train. 2. Proceed to station 2. This step takes 1 second. 3. Deliver the first candy. 4. Proceed to station 1. This step takes 1 second. 5. Load the second candy onto the train. 6. Proceed to station 2. This step takes 1 second. 7. Deliver the second candy. 8. Proceed to station 1. This step takes 1 second. 9. Load the third candy onto the train. 10. Proceed to station 2. This step takes 1 second. 11. Deliver the third candy. Hence, the train needs 5 seconds to complete the tasks. If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
n, m = map(int, input().split()) def dist(a, b): return (n + b - a) % n def main(): inp1 = [0] * (n + 1) inp2 = [n] * (n + 1) for _ in range(m): a, b = map(int, input().split()) inp1[a] += 1 inp2[a] = min(inp2[a], dist(a, b)) inp = tuple((r1 - 1) * n + r2 for r1, r2 in zip(inp1, inp2)) print( *( max(dist(i, j) + inp[j] for j in range(1, n + 1) if inp[j]) for i in range(1, n + 1) ) ) main()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = list(map(int, input().split())) a = list(map(int, input().split())) d = [] for i in range(m): d.append(list(map(int, input().split()))) par = [] g = [] for i in range(n): g.append(0) for i in range(n): par.append(g.copy()) mas = [] for i in range(n): mas.append([]) for i in range(m): for k in range(max(0, d[i][0] - 1), min(n, d[i][1])): mas[k].append(i) for c in range(max(0, d[i][0] - 1), min(n, d[i][1])): par[c][k] += 1 max = -100000000 for i in range(n): for j in range(n): c = a[j] - a[i] + len(mas[i]) - par[i][j] if c > max: max = c count = len(mas[i]) ans = mas[i] print(max) print(count) for i in ans: print(i + 1, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) mas = list(map(int, input().split())) otmas = [] for i in range(m): otmas.append(list(map(int, input().split()))) for i in range(m): otmas[i][0] -= 1 otmas[i][1] -= 1 ans = -1 masotv = [] for i in range(n): mascop = mas.copy() masotvizc = [] for j in range(m): if not (i >= otmas[j][0] and i <= otmas[j][1]): masotvizc.append(j + 1) for k in range(otmas[j][0], otmas[j][1] + 1): mascop[k] -= 1 if max(mascop) - min(mascop) > ans: masotv = masotvizc ans = max(ans, max(mascop) - min(mascop)) print(ans) print(len(masotv)) print(*masotv)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = list(map(int, input().split())) l = [] v = [] for _ in range(m): l.append(list(map(int, input().split()))) ans = max(a) - min(a) c = a.copy() for _ in range(n): for __ in range(n): a = c.copy() cur = [] if _ == __: continue for ___ in range(m): if __ in range(l[___][0] - 1, l[___][1]) and _ not in range( l[___][0] - 1, l[___][1] ): a[__] -= 1 cur.append(___ + 1) ans = max(ans, a[_] - a[__]) if ans == a[_] - a[__]: v = cur.copy() print(int(ans)) print(len(v)) for _ in v: print(_, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
from itertools import accumulate n, m = map(int, input().split()) A = list(map(int, input().split())) LR = [] for i in range(m): l, r = map(int, input().split()) l, r = l - 1, r - 1 LR.append((l, r)) ans = -float("inf") subs = -1 for i in range(n): a = A[i] imos = [0] * (n + 1) s = [] for j in range(m): l, r = LR[j] if l <= i and i <= r: continue else: imos[l] += 1 imos[r + 1] -= 1 s.append(j + 1) imos = list(accumulate(imos)) B = [0] * n for k in range(n): B[k] = A[k] - imos[k] min_ = min(B) temp = max(B) - min_ if temp >= ans: ans = temp subs = tuple(s) print(ans) print(len(subs)) print(*subs)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = list(map(int, input().split())) A = [int(a) for a in input().split()] L = [0] * m R = [0] * m for i in range(m): l, r = list(map(int, input().split())) L[i] = l - 1 R[i] = r - 1 ma = -1 mai = -1 for i in range(n): B = [a for a in A] for j in range(m): if L[j] > i or R[j] < i: for k in range(L[j], R[j] + 1): B[k] -= 1 d = B[i] - min(B) if d > ma: ma = d mai = i print(ma) B = [a for a in A] X = [] for j in range(m): if L[j] > mai or R[j] < mai: X.append(str(j + 1)) print(len(X)) print(" ".join(X))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
from sys import stdin, stdout def listIn(): return list(map(int, stdin.readline().strip().split())) def stringIn(): return [x for x in stdin.readline().split()] def intIn(): return int(stdin.readline()) n, m = listIn() a = [0] + listIn() arr = {} segments = [(0, 0)] for i in range(m): l, r = listIn() segments.append((l, r)) for j in range(l, r + 1): e = a[j], j if e not in arr: arr[e] = [0] arr[e].append(i + 1) for i in range(1, n + 1): e = a[i], i if e not in arr: arr[e] = [0] maxx = max(a[1:]) - min(a[1:]) minn = float("inf") for ele in arr: e = arr[ele] e[0] = ele[0] - (len(e) - 1) ans = [] for e1 in arr: s1 = set(arr[e1][1:]) for e2 in arr: if e1 != e2: s2 = set(arr[e2][1:]) s3 = s2 - s1 s4 = s1 - s2 b1 = e1[0] - (e2[0] - len(s3)) b2 = e2[0] - (e1[0] - len(s4)) b = max(b1, b2) if b > maxx: maxx = b if b == b1: ans = s3 else: ans = s4 print(maxx) print(len(ans)) print(*list(ans))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = [*map(int, input().split())] i = 0 c = [] d = [] while i < m: e, f = map(int, input().split()) c.append(e) d.append(f) i += 1 i = 0 j = 0 ans = max(a) - min(a) ii = 0 jj = 0 while i < n: j = 0 while j < n: k = 0 l = a[j] - a[i] if l + m <= ans: k = m + 1 while k < m: if l + m - k <= ans: k = m - 1 if c[k] <= i + 1 and d[k] >= i + 1: if c[k] > j + 1 or d[k] < j + 1: l += 1 k += 1 if l > ans: ii = i jj = j ans = l j += 1 i += 1 k = 0 j = jj i = ii l = a[j] - a[i] s = [] while k < m: if c[k] <= i + 1 and d[k] >= i + 1: if c[k] > j + 1 or d[k] < j + 1: s.append(k + 1) k += 1 print(ans) print(len(s)) print(*s, sep=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = [int(i) for i in input().split()] b = [] for i in range(m): x, y = map(int, input().split()) b.append((x, y)) seg = [] ma = -1000002 for i in range(len(a)): for j in range(len(a)): seg.clear() if i == j: continue cnt = 0 for idx in range(len(b)): l, r = b[idx] if i >= l - 1 and i < r: if j < l - 1 or j >= r: cnt += 1 seg.append(idx + 1) if a[j] - a[i] + cnt > ma: pos1, pos2 = i, j ma = a[j] - a[i] + cnt seg2 = seg.copy() if len(a) == 1: print(0) print(0) print() else: print(ma) print(len(seg2)) for l in seg2: print(l, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
def solve(n, m, values, segments): if n < 2: return 0, 0, [] def find_min_max(max_value_index): values_updated = values[:] max_value = values[max_value_index] min_value_index, min_value = None, None segment_indexes = [] for seg_index in range(m): start, end = segments[seg_index] if max_value_index < start - 1 or max_value_index > end - 1: for val_index in range(start - 1, end): values_updated[val_index] -= 1 segment_indexes.append(str(seg_index + 1)) for index in range(n): if index != max_value_index and ( min_value_index is None or values_updated[index] < min_value ): min_value_index = index min_value = values_updated[index] return min_value, max_value, segment_indexes d, segment_indexes = None, None for index in range(n): min_value, max_value, indexes = find_min_max(index) if min_value is None: continue if d is None or max_value - min_value > d: d = max_value - min_value segment_indexes = indexes if d is None: return 0, 0, [] return d, len(segment_indexes), segment_indexes n, m = map(int, input().strip().split(" ")) values = list(map(int, input().strip().split(" "))) segments = [] for i in range(m): a, b = map(int, input().strip().split(" ")) segments.append((a, b)) d, q, segment_indexes = solve(n, m, values, segments) print(d) print(q) print(" ".join(segment_indexes))
FUNC_DEF IF VAR NUMBER RETURN NUMBER NUMBER LIST FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE NONE ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NONE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR NONE NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR NONE IF VAR NONE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NONE RETURN NUMBER NUMBER LIST RETURN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
def read_nums(): return [int(x) for x in input().split()] def calculate_diff(i1, i2, nums, all_segments): cur_diff = nums[i2] - nums[i1] used_segments = [] for index, segment in enumerate(all_segments): left, right = segment if left <= i1 <= right and not left <= i2 <= right: used_segments.append(index) cur_diff += 1 return cur_diff, used_segments def main(): n, m = read_nums() nums = read_nums() segments = [[(x - 1) for x in read_nums()] for _ in range(m)] if n == 1: print(0) print(0) return candidates = [] for i1, num1 in enumerate(nums): for i2, num2 in enumerate(nums): if i1 == i2: continue diff, used_segments = calculate_diff(i1, i2, nums, segments) candidates.append((diff, used_segments)) res_diff, segment_indexes = max(candidates, key=lambda x: x[0]) print(res_diff) print(len(segment_indexes)) if len(segment_indexes) > 0: print(" ".join([str(x + 1) for x in segment_indexes])) def __starting_point(): main() __starting_point()
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = [int(x) for x in input().split()] L = [int(x) for x in input().split()] large = max(L) R = [] used = 0 Pairs = [] best = 0 lol = [] for i in range(n): if L[i] == large: R.append(i) for T in range(m): l, j = [int(x) for x in input().split()] Pairs.append((l, j)) for j in range(0, n): S = L[:] tu = 0 tuu = [] for i in range(m): if Pairs[i][0] - 1 > j or Pairs[i][1] <= j: tu += 1 tuu.append(i + 1) for k in range(Pairs[i][0] - 1, Pairs[i][1]): S[k] -= 1 t = max(S) - min(S) if t > best: best = t used = tu lol = tuu print(best) print(used) print(" ".join(str(i) for i in lol))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) mass = list(map(int, input().split())) lr = [] for t in range(m): l, r = map(int, input().split()) lr.append([l, r]) mq = 0 mdel = [] mitog = 0 for a in range(len(mass)): ma = mass[a] for b in range(len(mass)): mb = mass[b] q = 0 delete = [] itog = 0 for x in range(len(lr)): l, r = lr[x][0], lr[x][1] if l <= b + 1 <= r and (a + 1 < l or a + 1 > r): q += 1 delete.append(x + 1) itog = ma + q - mb if mitog < itog: mitog = itog mq = q mdel = delete print(mitog) print(mq) if len(mdel): print(" ".join(list(map(str, mdel)))) else: print("")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
def f(a, b): b = sorted(b, key=lambda s: s[1] - s[0]) t = {} for i in range(len(a)): l = [] for j in b: if i in range(j[0], j[1] + 1): l.append(j) t[i] = l t = dict(sorted(t.items(), key=lambda s: a[s[0]])) t = dict(sorted(t.items(), key=lambda s: len(s[1]) - a[s[0]], reverse=True)) mn = list(t.items())[0] curr = max(a) - min(a) bigans = 0 bigd = [] for mn in t.items(): s = a.copy() ans = [] a2 = [] for i in mn[1]: l = i[0] r = i[1] for j in range(l, r + 1): s[j] -= 1 tempans = max(s) - min(s) if tempans >= curr: a2.append(i[2] + 1) ans += a2 a2 = [] else: a2.append(i[2] + 1) curr = max(curr, tempans) if bigans < curr: bigans = curr bigd = ans else: break return bigd, bigans a, b = map(int, input().strip().split()) blacnk = [] lst = list(map(int, input().strip().split())) for i in range(b): l, r = map(int, input().strip().split()) blacnk.append([l - 1, r - 1, i]) x = f(lst, blacnk) print(x[1]) print(len(x[0])) print(*x[0])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = list(map(int, input().split())) b = [] for i in range(m): b.append(list(map(int, input().split()))) ans = max(a) - min(a) temp = [] for i in range(n): c = a[:] l = [] for j in range(m): if i not in range(b[j][0] - 1, b[j][1]): for k in range(b[j][0] - 1, b[j][1]): c[k] -= 1 l.append(j + 1) if ans < max(c) - min(c): ans = max(c) - min(c) temp = l print(ans) print(len(temp)) print(*temp)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = list(map(int, input().split())) ans = [] pair = [] for i in range(m): x = list(map(int, input().split())) pair.append(x) for i in range(n): for j in range(n): d = [] b = 0 for k in range(m): if ( j + 1 >= pair[k][0] and j + 1 <= pair[k][1] and (i + 1 < pair[k][0] or i + 1 > pair[k][1]) ): b += 1 d.append(k + 1) d = [a[i] - a[j] + b] + d ans.append(d) ans.sort() print(ans[-1][0]) sz = len(ans[-1]) - 1 print(sz) print(*ans[-1][1:])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = list(map(int, input().split())) segments = [] for i in range(m): l, r = map(int, input().split()) segments.append([l, r]) ans = 0 flag = 0 for i in range(n): for j in range(i, n): ai = a[i] aj = a[j] seglist1 = [] seglist2 = [] for k in range(m): if segments[k][0] - 1 <= i <= segments[k][1] - 1 and segments[k][1] - 1 < j: ai -= 1 seglist1.append(k + 1) if segments[k][0] - 1 > i and segments[k][0] - 1 <= j <= segments[k][1] - 1: aj -= 1 seglist2.append(k + 1) if ans < a[j] - ai: anslist = seglist1 ans = a[j] - ai flag = 1 if ans < a[i] - aj: anslist = seglist2 ans = a[i] - aj flag = 1 if flag == 1 and len(anslist) >= 1: ansseg = str(anslist[0]) for i in range(1, len(anslist)): ansseg += " " + str(anslist[i]) else: anslist = [] ansseg = "" print(ans) print(len(anslist)) print(ansseg)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) lis = list(map(int, input().split())) res = max(lis) - min(lis) seg = [] cur = [] pre = [0] * n for i in range(m): a, b = map(int, input().split()) seg.append([a - 1, b - 1]) for i in range(n): c = lis[:] ans = [] for j in range(m): aa, bb = seg[j][0], seg[j][1] if i not in range(aa, bb + 1): for k in range(aa, bb + 1): c[k] -= 1 ans.append(j + 1) if res < max(c) - min(c): res = max(c) - min(c) cur = ans print(res) print(len(cur)) print(*cur)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
dat = list(map(int, input().split())) l = list(map(int, input().split())) d = {} i = 1 while i < dat[1] + 1: inter = list(map(int, input().split())) j = inter[0] while j < inter[1] + 1: if j not in d: d[j] = set() d[j].add(i) j += 1 i += 1 max_res = -1 ans = None i = 0 while i < len(l): j = 0 while j < len(l): ii = i + 1 jj = j + 1 res_i = l[i] res_j = l[j] if ii in d: res_i = l[i] - len(d[ii]) if jj in d: for dd in d[ii]: if dd in d[jj]: res_j -= 1 res = res_j - res_i if res > max_res: max_res = res ans = ii j += 1 i += 1 if max_res == -1: print(0) print(0) else: print(max_res) if ans in d: print(len(d[ans])) print(" ".join(map(str, d[ans]))) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
def main(): n, m = map(int, input().split()) aa = list(map(int, input().split())) res = max(aa) - min(aa) ll, rr = [999], [999] segments = res_segments = [False] * (m + 1) for _ in range(m): l, r = map(int, input().split()) ll.append(l - 1) rr.append(r - 1) il, ir = (sorted(range(m + 1), key=xx.__getitem__, reverse=True) for xx in (ll, rr)) for i in range(n): while True: k = il[-1] if ll[k] > i: break segments[k] = True for j in range(ll[k], rr[k] + 1): aa[j] -= 1 del il[-1] x = max(aa) - min(aa) if res < x: res = x res_segments = segments[:] while True: k = ir[-1] if rr[k] > i: break segments[k] = False for j in range(ll[k], rr[k] + 1): aa[j] += 1 del ir[-1] print(res) segments = [i for i, f in enumerate(res_segments) if f] print(len(segments)) print(*segments) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
horizon, num_blocks = [int(x) for x in input().split()] c = 0 terrain = [] for x in input().split(): terrain.append([int(x), c]) c += 1 ori_shallow = max(terrain) ori_deep = min(terrain) record = abs(ori_deep[0] - ori_shallow[0]) if num_blocks == 0 or horizon == 1: print(record) print(0) exit() record_index = "" blocks = [] c = 0 new_terrain = [list(x) for x in terrain] for x in range(num_blocks): new_block = [(int(j) - 1) for j in input().split()] blocks.append([new_block, c]) c += 1 for y in range(new_block[0], new_block[1] + 1): new_terrain[y][0] -= 1 blocks.sort() landscape = sorted(new_terrain) new_shallow = landscape[-1] used_blocks_index = "" for y in range(horizon): if abs(ori_shallow[0] - landscape[y][0]) <= record: break else: used_blocks_index = "" present_index = landscape[y][1] new_terrain = [list(s) for s in terrain] for t in range(num_blocks): if blocks[t][0][0] > present_index: break if blocks[t][0][1] < present_index: continue for x in range(blocks[t][0][0], blocks[t][0][1] + 1): new_terrain[x][0] -= 1 used_blocks_index += " " + str(blocks[t][1]) new_shallow = max(new_terrain)[0] gap = abs(new_shallow - landscape[y][0]) if gap > record: record = gap record_index = used_blocks_index print(record) ans_index = [(int(x) + 1) for x in record_index.split()] print(len(ans_index)) print(" ".join([str(x) for x in ans_index]))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR IF VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = [int(i) for i in input().split()] A = [int(i) for i in input().split()] queries = [] for i in range(m): l, r = [int(x) for x in input().split()] queries.append([l, r]) if True: B = A[:] bestans = 0 bestq = [] for mnind in range(n): qans = [] B = A[:] for i in range(m): if queries[i][0] - 1 > mnind or mnind > queries[i][1] - 1: qans.append(i) for j in range(queries[i][0] - 1, queries[i][1]): B[j] -= 1 if max(B) - min(B) > bestans: bestans = max(B) - min(B) bestq = qans[:] print(bestans) print(len(bestq)) for i in bestq: print(i + 1, end=" ") print() def brute(): res = 0 resq = [] for i in range(1 << m): ans = 0 qs = [] tmp = A[:] for j in range(m): if 1 << j & i: qs.append(j + 1) for k in range(queries[j][0] - 1, queries[j][1]): tmp[k] -= 1 ans = max(tmp) - min(tmp) print(qs, ans) res = max(res, ans) if res == ans: resq = qs[:] print("Final", res, resq)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR IF NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys input = sys.stdin.readline def inp(): return int(input()) def inara(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) n, m = invr() a = inara() seg = [] for i in range(m): l, r = invr() l -= 1 r -= 1 seg.append([l, r]) maxi = max(a) - min(a) moves = [] for i in range(n): for j in range(n): curr = a[i] - a[j] temp = [] for k in range(m): l = seg[k][0] r = seg[k][1] if j >= l and j <= r: temp.append(k + 1) if i < l or i > r: curr += 1 if curr > maxi: maxi = curr moves = temp print(maxi) print(len(moves)) print(*moves)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = list(map(int, input().split())) A = list(map(int, input().split())) lst = [] for i in range(m): a, b = list(map(int, input().split())) lst.append([a, b]) answer = 0 answer_1 = [] for i in range(n): B = A.copy() kek = [] for j in range(m): a, b = lst[j][0], lst[j][1] if a <= i + 1 <= b: kek.append(j + 1) for q in range(a - 1, b): B[q] -= 1 elem = max(B) if answer < elem - B[i]: answer = elem - B[i] answer_1 = kek.copy() print(answer) print(len(answer_1)) print(" ".join(map(str, answer_1)))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
( lambda N, M: ( lambda n, m, d, dM, dm: [ [ [ ( lambda a, b: [ [ (0 if k[0] <= i <= k[1] else b.__setitem__(0, b[0] - 1)) for k in m if not k[0] <= i <= k[1] and k[0] <= j <= k[1] ], d[0] < a[0] - b[0] and [ d.__setitem__(0, a[0] - b[0]), dM.__setitem__(0, i), dm.__setitem__(0, j), ], ] )([n[i - 1]], [n[j - 1]]) for j in range(1, N + 1) ] for i in range(1, N + 1) ], (lambda p: [print(d[0]), print(len(p)), print(*p)])( [ (k + 1) for k in range(M) if not m[k][0] <= dM[0] <= m[k][1] and m[k][0] <= dm[0] <= m[k][1] ] ), ] )( list(map(int, input().split())), [list(map(int, input().split())) for i in range(M)], [-1], [0], [0], ) )(*map(int, input().split()))
EXPR FUNC_CALL FUNC_CALL LIST FUNC_CALL LIST VAR NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER LIST FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL LIST FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST NUMBER LIST NUMBER LIST NUMBER FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = list(map(int, input().split())) a = list(map(int, input().split())) b = [list(map(int, input().split())) for i in range(m)] gl_ans = -1 gl_mas = [] for num_i in range(n): ma = -10000000.0 ans_m = [] o = [] for i in range(m): if b[i][0] - 1 > num_i or b[i][1] - 1 < num_i: o.append([b[i][0] - 1, -1]) o.append([b[i][1], 0]) ans_m.append(i + 1) for i in range(n): o.append([i, 1]) o.sort() cnt = 0 mi = 10000000.0 for u in range(len(o)): if o[u][1] == -1: cnt -= 1 elif o[u][1] == 0: cnt += 1 elif a[o[u][0]] + cnt < mi: mi = a[o[u][0]] + cnt ma = a[num_i] if ma - mi > gl_ans: gl_ans = ma - mi gl_mas = ans_m print(gl_ans) print(len(gl_mas)) print(*gl_mas)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys big = 10**9 class super_seg: def __init__(self, data): n = len(data) m = 1 while m < n: m *= 2 self.n = n self.m = m self.data = [big] * (2 * m) for i in range(n): self.data[i + m] = data[i] for i in reversed(range(m)): self.data[i] = min(self.data[2 * i], self.data[2 * i + 1]) self.query = [0] * (2 * m) def push(self, seg_ind): q = self.query[seg_ind] self.query[2 * seg_ind] += q self.query[2 * seg_ind + 1] += q self.data[2 * seg_ind] += q self.data[2 * seg_ind + 1] += q self.data[seg_ind] = min(self.data[2 * seg_ind], self.data[2 * seg_ind + 1]) self.query[seg_ind] = 0 def update(self, seg_ind): seg_ind //= 2 inds = [] while seg_ind > 0: inds.append(seg_ind) seg_ind //= 2 for ind in reversed(inds): self.push(ind) def build(self, seg_ind): seg_ind //= 2 while seg_ind > 0: self.data[seg_ind] = ( min(self.data[2 * seg_ind], self.data[2 * seg_ind + 1]) + self.query[seg_ind] ) seg_ind //= 2 def add(self, l, r, value): l += self.m r += self.m l0 = l r0 = r while l < r: if l % 2 == 1: self.query[l] += value self.data[l] += value l += 1 if r % 2 == 1: r -= 1 self.query[r] += value self.data[r] += value l //= 2 r //= 2 self.build(l0) self.build(r0 - 1) def min(self, l, r): l += self.m r += self.m self.update(l) self.update(r - 1) segs = [] while l < r: if l % 2 == 1: segs.append(l) l += 1 if r % 2 == 1: r -= 1 segs.append(r) l //= 2 r //= 2 return min(self.data[ind] for ind in segs) def find_min(self, l, r): l += self.m r += self.m self.update(l) self.update(r - 1) segs = [] while l < r: if l % 2 == 1: segs.append(l) l += 1 if r % 2 == 1: r -= 1 segs.append(r) l //= 2 r //= 2 ind = min(segs, key=lambda i: self.data[i]) mini = self.data[ind] while ind < self.m: self.push(ind) if self.data[2 * ind] == mini: ind *= 2 else: ind = 2 * ind + 1 return ind - self.m, mini n, m = [int(x) for x in input().split()] A = [int(x) for x in input().split()] inter = [] update = [[] for _ in range(n + 1)] for _ in range(m): l, r = [int(x) for x in input().split()] l -= 1 inter.append((l, r)) update[l].append((l, r)) update[r].append((l, r)) Aneg = super_seg([(-a) for a in A]) besta = -1 besta_ind = -1 active_intervals = 0 for i in range(n): for l, r in update[i]: Aneg.add(l, r, 1 if l == i else -1) active_intervals += 1 if l == i else -1 Amax = -Aneg.data[1] Ai = A[i] - active_intervals if Amax - Ai > besta: besta = Amax - Ai besta_ind = i ints = [i for i in range(m) if inter[i][0] <= besta_ind < inter[i][1]] print(besta) print(len(ints)) print(*[(x + 1) for x in ints])
IMPORT ASSIGN VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().strip().split()) lst = list(map(int, input().strip().split())) vidr = [] for i in range(m): a, b = map(int, input().strip().split()) vidr.append([a - 1, b - 1]) dif = max(lst) - min(lst) used = [] for i in range(n): new_vidr = [] new_lst = lst.copy() for v in range(m): if vidr[v][0] > i or vidr[v][1] < i: new_vidr.append(v + 1) for j in range(vidr[v][0], vidr[v][1] + 1): new_lst[j] -= 1 d = max(new_lst) - min(new_lst) if dif < d: dif = d used = new_vidr dif = max(dif, max(new_lst) - min(new_lst)) print(dif) print(len(used)) for i in used: print(i, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = [*map(int, input().split())] i = 0 c = [] d = [] while i < m: e, f = map(int, input().split()) c.append(e) d.append(f) i += 1 i = 0 j = 0 ans = -1 s = [] while i < n: j = 0 while j < n: k = 0 l = a[j] - a[i] ma = [] while k < m: if c[k] <= i + 1 and d[k] >= i + 1: if c[k] > j + 1 or d[k] < j + 1: l += 1 ma.append(k + 1) k += 1 if l > ans: s = ma ans = l j += 1 i += 1 print(ans) print(len(s)) print(*s, sep=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = list(map(int, input().split())) arr = [set() for i in range(n)] for i in range(m): l, r = map(int, input().split()) l -= 1 for j in range(l, r): arr[j].add(i) maxi = 0 b, s = 0, 0 d = 0 ans = [] f_ans = [] for i in range(n): for j in range(n): d = 0 ans = [] for k in range(300): if k in arr[i] and k not in arr[j]: d += 1 ans.append(k + 1) if maxi < a[j] - a[i] + d: maxi = a[j] - a[i] + d f_ans = ans print(maxi) print(len(f_ans)) print(*f_ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = [int(x) for x in input().split()] a = [int(x) for x in input().split()] q = [] for i in range(m): l, r = [int(c) for c in input().split()] q.append([l, r, i + 1]) fin = [] fin_val = 0 for i in range(n): mx = a[i] copy = a[:] ans = [] for j in range(m): l, r = q[j][0], q[j][1] if i >= l - 1 and i <= r - 1: continue for k in range(l - 1, r): copy[k] -= 1 ans.append(q[j][2]) val = mx - min(copy) if val > fin_val: fin = ans[:] fin_val = val print(fin_val) print(len(fin)) print(*fin)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) a = list(map(int, input().split())) ma = max(a) - min(a) rec = [] L, R = [], [] for i in range(m): l, r = map(int, input().split()) l, r = l - 1, r - 1 L.append(l) R.append(r) for k in range(n): c = a.copy() tmp = [] for i in range(m): l, r = L[i], R[i] if l <= k <= r: tmp.append(i + 1) for j in range(l, r + 1): c[j] -= 1 mac = max(c) - min(c) if ma < mac: ma = mac rec = tmp print(ma) print(len(rec)) print(" ".join(map(str, rec)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) x = [*map(int, input().split())] seg = [] for i in range(m): a = [*map(int, input().split())] seg.append(a) ans = -1 ansi = [] for i in range(len(x)): tx = x[:] oi = [] for n, j in enumerate(seg): a, b = j if not a - 1 <= i <= b - 1: oi.append(n + 1) for k in range(a - 1, b): tx[k] -= 1 m = max(tx) - min(tx) if m > ans: ans = m ansi = oi[:] print(ans) print(len(ansi)) for i in ansi: print(i, end=" ")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = list(map(int, input().split())) lst = list(map(int, input().split())) segments = [list(map(int, input().split())) for i in range(m)] max_diff = max(lst) - min(lst) final_inds_segments = [] for ind_max, est_max in enumerate(lst): est_final_list = lst.copy() est_inds_segments = [] for segm_pair_ind, segment in enumerate(segments): if not segment[0] - 1 <= ind_max <= segment[1] - 1: est_inds_segments.append(segm_pair_ind + 1) for segm_ind in range(segment[0] - 1, segment[1]): est_final_list[segm_ind] += -1 est_max_diff = max(est_final_list) - min(est_final_list) if est_max_diff > max_diff: max_diff = est_max_diff final_inds_segments = est_inds_segments print(max_diff) if final_inds_segments: print(len(final_inds_segments)) print(" ".join(map(str, final_inds_segments))) else: print(0)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) A = list(map(int, input().split())) Lf = [[] for _ in range(n)] Rb = [[] for _ in range(n)] LR = [] for i in range(m): l, r = map(int, input().split()) l, r = l - 1, r - 1 Lf[r].append(l) Rb[l].append(r) LR.append((l, r)) minus = [0] * n INF = 10**18 ans = [-INF] * n mn = A[0] for i in range(n): ans[i] = max(ans[i], A[i] - mn) for l in Lf[i]: for j in range(l, i + 1): minus[j] -= 1 mn = min(mn, A[j] + minus[j]) mn = min(mn, A[i] + minus[i]) minus = [0] * n mn = A[n - 1] for i in reversed(range(n)): ans[i] = max(ans[i], A[i] - mn) for r in Rb[i]: for j in range(i, r + 1): minus[j] -= 1 mn = min(mn, A[j] + minus[j]) mn = min(mn, A[i] + minus[i]) ans_ = max(ans) res = [] for i in range(n): if ans[i] == ans_: for j in range(m): l, r = LR[j] if not (l <= i and i <= r): res.append(j + 1) break print(ans_) print(len(res)) print(*res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = list(map(int, input().split())) lr = [list(map(int, input().split())) for _ in range(m)] for i in range(m): lr[i][0] -= 1 inf = 1145141919 d = -inf for i in range(n): for j in range(n): b = [0] * (n + 1) c0 = [] for k in range(m): l, r = lr[k] if l <= i < r and not l <= j < r: b[l] -= 1 b[r] += 1 c0.append(k + 1) mi, ma = inf, -inf s = 0 for k in range(n): s += b[k] ak = s + a[k] mi = min(mi, ak) ma = max(ma, ak) if d < ma - mi: d = ma - mi c = c0 q = len(c) print(d) print(q) print(*c)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) arr = [int(x) for x in input().split()] seg = [] el = [] el2 = [] h = [] def cont(a, b, c): return a <= c <= b def count_delta(): return max(h) - min(h) def decrement(l, r): for i in range(l - 1, r): h[i] -= 1 for i in range(m): seg.append([int(x) for x in input().split()]) for i in range(len(arr)): h = arr[:] tmp = [] for j in range(len(seg)): if cont(seg[j][0], seg[j][1], i + 1): decrement(seg[j][0], seg[j][1]) tmp.append(j + 1) el.append(count_delta()) el2.append(tmp) print(max(el)) k = el.index(max(el)) print(len(el2[k])) print(*el2[k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF RETURN VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
aLen, numOfSeg = map(int, input().split(" ")) aList = list(map(int, input().split(" "))) segList = [] for x in range(numOfSeg): s, e = map(int, input().split(" ")) segList.append((s, e)) res = max(aList) - min(aList) hList = [] for x in range(0, len(aList)): ele = aList[x] c = aList[:] h = [] for y in range(numOfSeg): if x not in range(segList[y][0] - 1, segList[y][1]): h.append(str(y + 1)) for z in range(segList[y][0] - 1, segList[y][1]): c[z] -= 1 lres = max(c) - min(c) if lres > res: res = lres hList = h print(res) print(len(hList)) print(" ".join(hList))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) l = list(map(int, input().split())) seg = [] for i in range(m): a, b = map(int, input().split()) seg.append((a - 1, b - 1)) ans = max(l) - min(l) minind = 0 maxind = 0 for i in range(n): for j in range(n): temp1 = l[i] temp2 = l[j] for k in seg: if j >= k[0] and j <= k[1] and (i < k[0] or i > k[1]): temp2 -= 1 c = temp1 - temp2 if c > ans: ans = c minind = j maxind = i ansl = [] for i in range(m): t = seg[i] if t[0] <= minind and t[1] >= minind and (t[0] > maxind or t[1] < maxind): ansl.append(i + 1) print(ans) print(len(ansl)) print(*ansl)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
from sys import stdin n, m = map(int, input().split()) l = list(map(int, stdin.readline().split())) segment = [tuple(map(int, inp.split())) for inp in stdin.read().splitlines()] ans = 0 chosen = [] for i in range(n): temp = l.copy() curpair = [] for j in range(m): left, right = segment[j] if i not in range(left - 1, right): for k in range(left - 1, right): temp[k] -= 1 curpair.append(j + 1) cur = max(temp) - min(temp) if cur > ans: ans = cur chosen = curpair.copy() print(ans) print(len(chosen)) print(*chosen)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = list(map(int, input().split())) lr = [list(map(int, input().split())) for i in range(m)] max_d = -1 ans_use = [] for i in range(n): use = [] c = [a[i] for i in range(n)] for j in range(m): l, r = lr[j] if l <= i + 1 <= r: use.append(j + 1) for k in range(l, r + 1): c[k - 1] -= 1 dd = max(c) - min(c) if dd > max_d: ans_use = use max_d = dd print(max_d) print(len(ans_use)) print(*ans_use)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys input = sys.stdin.readline n, m = map(int, input().split()) A = list(map(int, input().split())) LR = [list(map(int, input().split())) for i in range(m)] def MINUS(A, B): MIN = float("inf") MAX = -float("inf") for i in range(len(A)): if MIN > A[i] - B[i]: MIN = A[i] - B[i] if MAX < A[i] - B[i]: MAX = A[i] - B[i] return MAX - MIN MINUSLIST = [([0] * n) for i in range(n)] for l, r in LR: for j in range(l - 1, r): for k in range(l - 1, r): MINUSLIST[j][k] += 1 ANSLIST = [0] * n for i in range(n): ANSLIST[i] = MINUS(A, MINUSLIST[i]) print(max(ANSLIST)) x = ANSLIST.index(max(ANSLIST)) AN = [] for i in range(m): z, w = LR[i] if z <= x + 1 <= w: AN.append(i + 1) print(len(AN)) for a in AN: print(a, end=" ")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
import sys inn = lambda: input().strip() N, M = map(int, inn().split(" ")) array = list(map(int, inn().split(" "))) segment = [] for _ in range(M): l, r = tuple(map(int, inn().split(" "))) segment.append((l - 1, r - 1)) answer = -1 selectedSeg = [] for i, a in enumerate(array): array_temp = array.copy() selectedSeg_temp = [] for segI, (l, r) in enumerate(segment): if i >= l and i <= r: selectedSeg_temp.append(segI + 1) for decrease in range(l, r + 1): array_temp[decrease] -= 1 interval = max(array_temp) - min(array_temp) if answer < interval: answer = interval selectedSeg = list(map(str, selectedSeg_temp)) print(answer) print(len(selectedSeg)) print(" ".join(selectedSeg))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, q = map(int, input().split()) l = [int(i) for i in input().split()] eff = [0] * (n + 5) qry = [] for i in range(q): a, b = map(int, input().split()) a -= 1 b -= 1 qry.append([a, b]) maxi = 0 res = [] for ans in range(n): l1 = l[:] now = l[ans] apply = [] for j in range(q): a = qry[j][0] b = qry[j][1] if a <= ans <= b: pass else: apply.append(j) for i in range(a, b + 1): l1[i] -= 1 if now - min(l1) > maxi: maxi = now - min(l1) res = apply print(maxi) print(len(res)) if not res: exit() for i in res: print(i + 1, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) arr = list(map(int, input().split())) b = [] for i in range(m): l, r = map(int, input().split()) b.append([l, r]) ans = 0 rt = [] for i in range(1, n + 1): brr = arr[:] temp = [] for j in range(m): if i >= b[j][0] and i <= b[j][1]: continue temp.append(j) for k in range(b[j][0] - 1, b[j][1]): brr[k] -= 1 mx = max(brr) mi = min(brr) if mx - mi > ans: rt = temp[:] ans = max(ans, mx - mi) print(ans) print(len(rt)) for i in rt: print(i + 1, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
def main(): n, m = map(int, input().split()) aa = list(map(int, input().split())) res = max(aa) - min(aa) ll, rr = [n + 1], [n + 1] segments = res_segments = [False] * (m + 1) bounds = {0, n, n + 1} for _ in range(m): l, r = map(int, input().split()) l -= 1 ll.append(l) rr.append(r) bounds.add(l) bounds.add(r) xlat = sorted(bounds) mi, ma = [], [] for l, r in zip(xlat, xlat[1:-1]): t = aa[l:r] mi.append(min(t)) ma.append(max(t)) bounds = {x: i for i, x in enumerate(xlat)} for xx in (ll, rr): for i, x in enumerate(xx): xx[i] = bounds[x] il, ir = (sorted(range(m + 1), key=xx.__getitem__, reverse=True) for xx in (ll, rr)) for i in range(len(xlat) - 1): while True: k = il[-1] lo = ll[k] if lo > i: break segments[k] = True for j in range(lo, rr[k]): mi[j] -= 1 ma[j] -= 1 del il[-1] x = max(ma) - min(mi) if res < x: res = x res_segments = segments[:] while True: k = ir[-1] hi = rr[k] if hi > i: break segments[k] = False for j in range(ll[k], hi): mi[j] += 1 ma[j] += 1 del ir[-1] print(res) segments = [i for i, f in enumerate(res_segments) if f] print(len(segments)) print(*segments) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
R = lambda: map(int, input().split()) n, m = R() l = list(R()) d = max(l) - min(l) s = [] qc = [] for i in range(m): s.append(list(R())) for i in range(1, n + 1): str = l.copy() c = [] for j in range(m): if i not in range(s[j][0], s[j][1] + 1): c.append(j + 1) for t in range(s[j][0] - 1, s[j][1]): str[t] -= 1 distance = max(str) - min(str) if distance > d: d = distance qc = c print(d, len(qc), sep="\n") print(*qc)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
class Segments: def __init__(self, segments): self.segments = segments def all(self): return [i for i in range(len(self.segments))] def at_point(self, point, included=None, excluded=None): if included is None: included = self.all() if excluded is None: excluded = [] segments = [] for i in range(len(self.segments)): if i not in excluded and i in included: if self.segments[i][0] <= point <= self.segments[i][1]: segments.append(i) return segments n, m = map(int, input().split()) a = list(map(int, input().split())) ends = [] for i in range(m): l, r = map(int, input().split()) l -= 1 r -= 1 ends.append((l, r)) s = Segments(ends) delta = max(a) - min(a) c = [] segment_cache = [s.at_point(i) for i in range(len(a))] for i in range(len(a)): prev_segments = [] my_segments = [] for j in range(len(a)): if j != i: his_segments = segment_cache[j] if his_segments != prev_segments: my_segments = s.at_point(i, included=his_segments) local_max = a[i] - len(my_segments) local_min = a[j] - len(his_segments) if local_max - local_min > delta: delta = local_max - local_min c = set(my_segments + his_segments) prev_segments = his_segments print(delta) print(len(c)) for c_ in c: print(c_ + 1, end=" ")
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF NONE NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR IF VAR NONE ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING
The only difference between easy and hard versions is a number of elements in the array. You are given an array $a$ consisting of $n$ integers. The value of the $i$-th element of the array is $a_i$. You are also given a set of $m$ segments. The $j$-th segment is $[l_j; r_j]$, where $1 \le l_j \le r_j \le n$. You can choose some subset of the given set of segments and decrease values on each of the chosen segments by one (independently). For example, if the initial array $a = [0, 0, 0, 0, 0]$ and the given segments are $[1; 3]$ and $[2; 4]$ then you can choose both of them and the array will become $b = [-1, -2, -2, -1, 0]$. You have to choose some subset of the given segments (each segment can be chosen at most once) in such a way that if you apply this subset of segments to the array $a$ and obtain the array $b$ then the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ will be maximum possible. Note that you can choose the empty set. If there are multiple answers, you can print any. If you are Python programmer, consider using PyPy instead of Python when you submit your code. -----Input----- The first line of the input contains two integers $n$ and $m$ ($1 \le n \le 300, 0 \le m \le 300$) β€” the length of the array $a$ and the number of segments, respectively. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^6 \le a_i \le 10^6$), where $a_i$ is the value of the $i$-th element of the array $a$. The next $m$ lines are contain two integers each. The $j$-th of them contains two integers $l_j$ and $r_j$ ($1 \le l_j \le r_j \le n$), where $l_j$ and $r_j$ are the ends of the $j$-th segment. -----Output----- In the first line of the output print one integer $d$ β€” the maximum possible value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ if $b$ is the array obtained by applying some subset of the given segments to the array $a$. In the second line of the output print one integer $q$ ($0 \le q \le m$) β€” the number of segments you apply. In the third line print $q$ distinct integers $c_1, c_2, \dots, c_q$ in any order ($1 \le c_k \le m$) β€” indices of segments you apply to the array $a$ in such a way that the value $\max\limits_{i=1}^{n}b_i - \min\limits_{i=1}^{n}b_i$ of the obtained array $b$ is maximum possible. If there are multiple answers, you can print any. -----Examples----- Input 5 4 2 -2 3 1 2 1 3 4 5 2 5 1 3 Output 6 2 1 4 Input 5 4 2 -2 3 1 4 3 5 3 4 2 4 2 5 Output 7 2 3 2 Input 1 0 1000000 Output 0 0 -----Note----- In the first example the obtained array $b$ will be $[0, -4, 1, 1, 2]$ so the answer is $6$. In the second example the obtained array $b$ will be $[2, -3, 1, -1, 4]$ so the answer is $7$. In the third example you cannot do anything so the answer is $0$.
n, m = map(int, input().split()) b = list(map(int, input().split())) segs = [list(map(int, input().split())) for _ in range(m)] a = [] d = [] for i in range(n): w = [i for i in b] x = [0] * (n + 1) t = [] for j in range(m): left, right = segs[j][0], segs[j][1] if i >= left - 1 and i <= right - 1: x[left - 1] -= 1 x[right] += 1 t.append(j + 1) d.append(t) for j in range(1, n + 1): x[j] += x[j - 1] for j in range(n): w[j] += x[j] if m > 0: l = min(w) h = max(w) a.append(h - l) if a != []: f = a.index(max(a)) if max(b) - min(b) > max(a): print(max(b) - min(b)) print(0) print() else: print(max(a)) print(len(d[f])) print(*d[f]) else: print(max(b) - min(b)) print(0) print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR