description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0). Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N. How many cities can Steven start his car from so that he can travel around the world and reach the same city he started? Note The fuel tank is initially empty. Input Format The first line contains two integers (separated by a space): city number N and capacity C. The second line contains N space-separated integers: a[0], a[1], … , a[N - 1]. The third line contains N space-separated integers: b[0], b[1], … , b[N - 1]. Constraints 2 ≤ N ≤ $10^{5}$ 1 ≤ C ≤ $10^{18} $ 0 ≤ a[i], b[i] ≤ $10^{9} $ Output Format The number of cities which can be chosen as the start city. Sample Input 3 3 3 1 2 2 2 2 Sample Output 2 Explanation Steven starts from city 0, fills his car with 3 gallons of fuel, and use 2 gallons of fuel to travel to city 1. His fuel tank now has 1 gallon of fuel. On refueling 1 gallon of fuel at city 1, he then travels to city 2 by using 2 gallons of fuel. His fuel tank is now empty. On refueling 2 gallon of fuel at city 2, he then travels back to city 0 by using 2 gallons of fuel. Here is the second possible solution. Steven starts from city 2, fill his car with 2 gallons, and travels to city 0. On refueling 3 gallons of fuel from city 0, he then travels to city 1, and exhausts 2 gallons of fuel. His fuel tank contains 1 gallon of fuel now. He can then refuel 1 gallon of fuel at City 1, and increase his car's fuel to 2 gallons and travel to city 2. However, Steven cannot start from city 1, because he is given only 1 gallon of fuel, but travelling to city 2 requires 2 gallons. Hence the answer 2.
__author__ = "Omotola" N, C = [int(nc) for nc in input().strip().split(" ")] A = [int(a) for a in input().strip().split(" ")] B = [int(b) for b in input().strip().split(" ")] check = [(0) for ch in range(N)] failed = debt = credit = 0 i = N - 1 j = 0 while j < N and check[i] == 0: if check[i] == 0: check[i] = 1 sum_debt = 0 credit = min(C, A[i]) debt = B[i] instance = credit - debt if debt > C: failed = N break if instance >= 0: j += 1 i = i - 1 if i > 0 else N - 1 continue counter = 0 while counter < N: if check[i] == 0: check[i] = 1 credit = min(C, A[i]) debt = B[i] instance = credit - debt j = 0 if sum_debt == 0: sum_debt = debt - credit if check[i] != 2: failed += 1 check[i] = 2 else: sum_debt -= instance if sum_debt <= 0: i = i - 1 if i > 0 else N - 1 break else: if check[i] != 2: failed += 1 check[i] = 2 if credit == C: failed = N j = N break if sum_debt > C: failed = N j = N break i = i - 1 if i > 0 else N - 1 counter += 1 else: break print(N - failed)
ASSIGN VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0). Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N. How many cities can Steven start his car from so that he can travel around the world and reach the same city he started? Note The fuel tank is initially empty. Input Format The first line contains two integers (separated by a space): city number N and capacity C. The second line contains N space-separated integers: a[0], a[1], … , a[N - 1]. The third line contains N space-separated integers: b[0], b[1], … , b[N - 1]. Constraints 2 ≤ N ≤ $10^{5}$ 1 ≤ C ≤ $10^{18} $ 0 ≤ a[i], b[i] ≤ $10^{9} $ Output Format The number of cities which can be chosen as the start city. Sample Input 3 3 3 1 2 2 2 2 Sample Output 2 Explanation Steven starts from city 0, fills his car with 3 gallons of fuel, and use 2 gallons of fuel to travel to city 1. His fuel tank now has 1 gallon of fuel. On refueling 1 gallon of fuel at city 1, he then travels to city 2 by using 2 gallons of fuel. His fuel tank is now empty. On refueling 2 gallon of fuel at city 2, he then travels back to city 0 by using 2 gallons of fuel. Here is the second possible solution. Steven starts from city 2, fill his car with 2 gallons, and travels to city 0. On refueling 3 gallons of fuel from city 0, he then travels to city 1, and exhausts 2 gallons of fuel. His fuel tank contains 1 gallon of fuel now. He can then refuel 1 gallon of fuel at City 1, and increase his car's fuel to 2 gallons and travel to city 2. However, Steven cannot start from city 1, because he is given only 1 gallon of fuel, but travelling to city 2 requires 2 gallons. Hence the answer 2.
N, C = map(int, input().split()) A = [int(x) for x in input().split()] B = [int(x) for x in input().split()] A = A * 2 B = B * 2 R = [(0) for x in range(2 * N + 1)] for i in range(2 * N - 1, -1, -1): R[i] = R[i + 1] + B[i] if R[i] > C: print(0) exit() else: R[i] = max(0, R[i] - A[i]) print(sum([(1) for i in range(N) if R[i] == 0]))
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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER
There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0). Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N. How many cities can Steven start his car from so that he can travel around the world and reach the same city he started? Note The fuel tank is initially empty. Input Format The first line contains two integers (separated by a space): city number N and capacity C. The second line contains N space-separated integers: a[0], a[1], … , a[N - 1]. The third line contains N space-separated integers: b[0], b[1], … , b[N - 1]. Constraints 2 ≤ N ≤ $10^{5}$ 1 ≤ C ≤ $10^{18} $ 0 ≤ a[i], b[i] ≤ $10^{9} $ Output Format The number of cities which can be chosen as the start city. Sample Input 3 3 3 1 2 2 2 2 Sample Output 2 Explanation Steven starts from city 0, fills his car with 3 gallons of fuel, and use 2 gallons of fuel to travel to city 1. His fuel tank now has 1 gallon of fuel. On refueling 1 gallon of fuel at city 1, he then travels to city 2 by using 2 gallons of fuel. His fuel tank is now empty. On refueling 2 gallon of fuel at city 2, he then travels back to city 0 by using 2 gallons of fuel. Here is the second possible solution. Steven starts from city 2, fill his car with 2 gallons, and travels to city 0. On refueling 3 gallons of fuel from city 0, he then travels to city 1, and exhausts 2 gallons of fuel. His fuel tank contains 1 gallon of fuel now. He can then refuel 1 gallon of fuel at City 1, and increase his car's fuel to 2 gallons and travel to city 2. However, Steven cannot start from city 1, because he is given only 1 gallon of fuel, but travelling to city 2 requires 2 gallons. Hence the answer 2.
def first_starting_index(available_fuel, required_fuel, tank_capacity, n_cities): city_index = 0 while city_index < n_cities: current_city = city_index returned_to_start = False tank = 0 while not returned_to_start: tank = ( tank_capacity if tank + available_fuel[current_city] > tank_capacity else tank + available_fuel[current_city] ) tank -= required_fuel[current_city] if tank >= 0: current_city = (current_city + 1) % n_cities returned_to_start = current_city == city_index else: break if returned_to_start: return city_index city_index += 1 return -1 def able_to_reach_city( from_city, to_city, available_fuel, required_fuel, tank_capacity, n_cities ): tank = 0 current_city = from_city while current_city != to_city: tank = ( tank_capacity if tank + available_fuel[current_city] > tank_capacity else tank + available_fuel[current_city] ) tank -= required_fuel[current_city] if tank < 0: return False current_city = (current_city + 1) % n_cities tank = ( tank_capacity if tank + available_fuel[current_city] > tank_capacity else tank + available_fuel[current_city] ) tank -= required_fuel[current_city] return tank >= 0 def find_all_starting_indexes( available_fuel, required_fuel, tank_capacity, n_cities, starting_city ): count = 0 current_city = n_cities - 1 if starting_city == 0 else starting_city - 1 goal_city = starting_city while current_city != starting_city: can_reach_goal_city = able_to_reach_city( current_city, goal_city, available_fuel, required_fuel, tank_capacity, n_cities, ) if can_reach_goal_city: count += 1 goal_city = current_city current_city = n_cities - 1 if current_city == 0 else current_city - 1 return count n, c = map(int, input().split(" ")) a = [int(x) for x in input().split(" ")] b = [int(x) for x in input().split(" ")] starting_city = first_starting_index(a, b, c, n) count = 0 if starting_city == -1 else 1 if count == 1: count += find_all_starting_indexes(a, b, c, n, starting_city) print(count)
FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR RETURN VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0). Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N. How many cities can Steven start his car from so that he can travel around the world and reach the same city he started? Note The fuel tank is initially empty. Input Format The first line contains two integers (separated by a space): city number N and capacity C. The second line contains N space-separated integers: a[0], a[1], … , a[N - 1]. The third line contains N space-separated integers: b[0], b[1], … , b[N - 1]. Constraints 2 ≤ N ≤ $10^{5}$ 1 ≤ C ≤ $10^{18} $ 0 ≤ a[i], b[i] ≤ $10^{9} $ Output Format The number of cities which can be chosen as the start city. Sample Input 3 3 3 1 2 2 2 2 Sample Output 2 Explanation Steven starts from city 0, fills his car with 3 gallons of fuel, and use 2 gallons of fuel to travel to city 1. His fuel tank now has 1 gallon of fuel. On refueling 1 gallon of fuel at city 1, he then travels to city 2 by using 2 gallons of fuel. His fuel tank is now empty. On refueling 2 gallon of fuel at city 2, he then travels back to city 0 by using 2 gallons of fuel. Here is the second possible solution. Steven starts from city 2, fill his car with 2 gallons, and travels to city 0. On refueling 3 gallons of fuel from city 0, he then travels to city 1, and exhausts 2 gallons of fuel. His fuel tank contains 1 gallon of fuel now. He can then refuel 1 gallon of fuel at City 1, and increase his car's fuel to 2 gallons and travel to city 2. However, Steven cannot start from city 1, because he is given only 1 gallon of fuel, but travelling to city 2 requires 2 gallons. Hence the answer 2.
import sys def travel(fuels, paths, num, cap): start_index = 0 cur_city = start_index fuel_gathered = 0 while ( cur_city != (start_index - 1) % num or paths[cur_city] > min(fuel_gathered + fuels[cur_city], cap) ) and start_index < num: fuel_gathered = min(fuel_gathered + fuels[cur_city], cap) if paths[cur_city] > fuel_gathered: if cur_city == start_index - 1: start_index = start_index + 1 else: start_index = cur_city + 1 cur_city = start_index % num fuel_gathered = 0 else: cur_city = (cur_city + 1) % num fuel_gathered = fuel_gathered - paths[cur_city] if start_index == num: return 0 start_count = 1 fuels_list = [0] * num for i in range(1, num): fuel_amount = ( min(cap, fuels[(start_index - i) % num]) - paths[(start_index - i) % num] ) if fuel_amount < 0: fuels_list[(start_index - i) % num] = ( fuels_list[(start_index - i + 1) % num] + fuel_amount ) else: fuels_list[(start_index - i) % num] = min( 0, fuels_list[(start_index - i + 1) % num] + fuel_amount ) if fuels_list[(start_index - i) % num] >= 0: start_count += 1 return start_count first = sys.stdin.readline().split(" ") n = int(first[0]) c = int(first[1]) a = list(int(p) for p in sys.stdin.readline().split(" ")) b = list(int(p) for p in sys.stdin.readline().split(" ")) print(travel(a, b, n, c))
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0). Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N. How many cities can Steven start his car from so that he can travel around the world and reach the same city he started? Note The fuel tank is initially empty. Input Format The first line contains two integers (separated by a space): city number N and capacity C. The second line contains N space-separated integers: a[0], a[1], … , a[N - 1]. The third line contains N space-separated integers: b[0], b[1], … , b[N - 1]. Constraints 2 ≤ N ≤ $10^{5}$ 1 ≤ C ≤ $10^{18} $ 0 ≤ a[i], b[i] ≤ $10^{9} $ Output Format The number of cities which can be chosen as the start city. Sample Input 3 3 3 1 2 2 2 2 Sample Output 2 Explanation Steven starts from city 0, fills his car with 3 gallons of fuel, and use 2 gallons of fuel to travel to city 1. His fuel tank now has 1 gallon of fuel. On refueling 1 gallon of fuel at city 1, he then travels to city 2 by using 2 gallons of fuel. His fuel tank is now empty. On refueling 2 gallon of fuel at city 2, he then travels back to city 0 by using 2 gallons of fuel. Here is the second possible solution. Steven starts from city 2, fill his car with 2 gallons, and travels to city 0. On refueling 3 gallons of fuel from city 0, he then travels to city 1, and exhausts 2 gallons of fuel. His fuel tank contains 1 gallon of fuel now. He can then refuel 1 gallon of fuel at City 1, and increase his car's fuel to 2 gallons and travel to city 2. However, Steven cannot start from city 1, because he is given only 1 gallon of fuel, but travelling to city 2 requires 2 gallons. Hence the answer 2.
N, C = [int(_) for _ in input().split()] a = [int(_) for _ in input().split()] b = [int(_) for _ in input().split()] def nbCity(N, C, a, b): start = 0 while start < N: c, valid = 0, True for i in range(N): c = min(C, c + a[(start + i) % N]) if c < b[(start + i) % N]: start = start + i + 1 valid = False break else: c -= b[(start + i) % N] if valid: break if start >= N: return 0 c, count = 0, 1 for i in range(1, N): c = max(c + b[(start - i) % N] - min(C, a[(start - i) % N]), 0) if c == 0: count += 1 return count print(nbCity(N, C, a, b))
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 VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR
There are N cities and N directed roads in Steven's world. The cities are numbered from 0 to N - 1. Steven can travel from city i to city (i + 1) % N, ( 0-> 1 -> 2 -> .... -> N - 1 -> 0). Steven wants to travel around the world by car. The capacity of his car's fuel tank is C gallons. There are a[i] gallons he can use at the beginning of city i and the car takes b[i] gallons to travel from city i to (i + 1) % N. How many cities can Steven start his car from so that he can travel around the world and reach the same city he started? Note The fuel tank is initially empty. Input Format The first line contains two integers (separated by a space): city number N and capacity C. The second line contains N space-separated integers: a[0], a[1], … , a[N - 1]. The third line contains N space-separated integers: b[0], b[1], … , b[N - 1]. Constraints 2 ≤ N ≤ $10^{5}$ 1 ≤ C ≤ $10^{18} $ 0 ≤ a[i], b[i] ≤ $10^{9} $ Output Format The number of cities which can be chosen as the start city. Sample Input 3 3 3 1 2 2 2 2 Sample Output 2 Explanation Steven starts from city 0, fills his car with 3 gallons of fuel, and use 2 gallons of fuel to travel to city 1. His fuel tank now has 1 gallon of fuel. On refueling 1 gallon of fuel at city 1, he then travels to city 2 by using 2 gallons of fuel. His fuel tank is now empty. On refueling 2 gallon of fuel at city 2, he then travels back to city 0 by using 2 gallons of fuel. Here is the second possible solution. Steven starts from city 2, fill his car with 2 gallons, and travels to city 0. On refueling 3 gallons of fuel from city 0, he then travels to city 1, and exhausts 2 gallons of fuel. His fuel tank contains 1 gallon of fuel now. He can then refuel 1 gallon of fuel at City 1, and increase his car's fuel to 2 gallons and travel to city 2. However, Steven cannot start from city 1, because he is given only 1 gallon of fuel, but travelling to city 2 requires 2 gallons. Hence the answer 2.
def travelAroundTheWorld(a, b, c, n): d = [] dmin = [] for i in range(n): d.append(a[i] - b[i]) dmin.append(0) dmin[n - 1] = d[n - 1] for i in range(n - 2, -1, -1): dmin[i] = min(d[i], d[i] + dmin[i + 1]) dmin[n - 1] = min(d[n - 1], d[n - 1] + dmin[0]) for i in range(n - 2, -1, -1): dmin[i] = min(d[i], d[i] + dmin[i + 1]) for i in range(n): if -dmin[i] + a[i] > c: print(0) return 0 count = 0 for i in range(n): if dmin[i] >= 0: count += 1 print(count) nc = input().split() n = int(nc[0]) c = int(nc[1]) a = list(map(int, input().rstrip().split())) b = list(map(int, input().rstrip().split())) result = travelAroundTheWorld(a, b, c, n)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL 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 FUNC_CALL VAR VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution1: def palindromePartition(self, s: str, k: int) -> int: dp = [([0] * len(s)) for i in range(len(s))] n = len(s) def transferPalimCount(s) -> int: res = 0 q = len(s) - 1 p = 0 while p < q: if s[p] != s[q]: res += 1 p += 1 q -= 1 return res for i in range(n): for j in range(i, n): dp[i][j] = transferPalimCount(s[i : j + 1]) def findMin(s, e, k, count, candidates): if k == 0: if s > e: candidates.append(count) return else: count += dp[s][e] candidates.append(count) return if e - s + 1 < k + 1: return for i in range(s, e + 1): findMin(i + 1, e, k - 1, count + dp[s][i], candidates) candidates = [] findMin(0, n - 1, k - 1, 0, candidates) return min(candidates) class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) memo = {} def cost(s, i, j): r = 0 while i < j: if s[i] != s[j]: r += 1 i += 1 j -= 1 return r def dfs(i, k): if (i, k) in memo: return memo[i, k] if n - i < k: return float("inf") if n - i == k: return 0 if k == 1: return cost(s, i, n - 1) res = float("inf") for j in range(i + 1, n): res = min(res, dfs(j, k - 1) + cost(s, i, j - 1)) memo[i, k] = res return res return dfs(0, k)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR RETURN FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) palindrome = [[(0) for x in range(n)] for y in range(n)] for i in range(n - 1): if s[i] != s[i + 1]: palindrome[i][i + 1] = 1 for gap in range(2, n): for start in range(n - gap): end = start + gap if s[start] == s[end]: palindrome[start][end] = palindrome[start + 1][end - 1] else: palindrome[start][end] = palindrome[start + 1][end - 1] + 1 dp = [[(2**31) for x in range(k + 1)] for y in range(n + 1)] dp[0][0] = 0 for i in range(1, n + 1): for j in range(1, k + 1): for l in range(i, 0, -1): dp[i][j] = min( dp[i][j], dp[l - 1][j - 1] + palindrome[l - 1][i - 1] ) return dp[n][k]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def getCount(i: int, j: int): count = 0 while i < j: if s[i] != s[j]: count += 1 i += 1 j -= 1 return count INT_MAX = 2147483647 N = len(s) dp = [[INT_MAX for x in range(k)] for y in range(N)] for i in range(N): dp[i][0] = getCount(0, i) for i in range(1, k): for j in range(i, N): for x in range(i - 1, j): dp[j][i] = min(dp[j][i], dp[x][i - 1] + getCount(x + 1, j)) return dp[N - 1][k - 1]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: dp = [[(2**30) for _ in range(k + 1)] for _ in range(len(s) + 1)] for p in range(k + 1): dp[0][p] = 0 def minChange(s: str) -> int: i, j = 0, len(s) - 1 cnt = 0 while i < j: if s[i] != s[j]: cnt += 1 i += 1 j -= 1 return cnt for i in range(1, len(s) + 1): for p in range(1, min(i, k) + 1): for j in range(i, p - 1, -1): dp[i][p] = min(dp[i][p], dp[j - 1][p - 1] + minChange(s[j - 1 : i])) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FUNC_DEF VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def count_char(a): n = len(a) b = a[::-1] return sum(a[i] != b[i] for i in range(n)) // 2 n = len(s) d = {} for i in range(n): for j in range(i, n): d[i, j] = count_char(s[i : j + 1]) @lru_cache(None) def dfs(i, j, l): if l == j - i + 1: return 0 if l == 1: return d[i, j] res = float("inf") for m in range(i, j - l + 2): res = min(res, d[i, m] + dfs(m + 1, j, l - 1)) return res return dfs(0, len(s) - 1, k)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def cost(self, i, j): if i + 1 >= j: return 0 if i + 2 == j: return 0 if self.s[i] == self.s[j - 1] else 1 try: return self.cost_cache[i, j] except KeyError: ret = self.cost(i + 1, j - 1) + (0 if self.s[i] == self.s[j - 1] else 1) self.cost_cache[i, j] = ret return ret def num_changes(self, i, kk): rem_len = len(self.s) - i if rem_len < kk: return self.MAX if rem_len == kk: return 0 if kk == 1: return self.cost(i, len(self.s)) try: return self.num_changes_cache[i, kk] except KeyError: ret = self.MAX for j in range(i + 1, len(self.s) - (kk - 1) + 1): ret = min(ret, self.cost(i, j) + self.num_changes(j, kk - 1)) self.num_changes_cache[i, kk] = ret return ret def palindromePartition(self, s: str, k: int) -> int: self.s = s self.MAX = len(s) self.cost_cache = {} self.num_changes_cache = {} for l in range(3, len(s)): for i in range(len(s) - l + 1): j = i + l self.cost(i, j) for i in range(len(s) - 3, -1, -1): for kk in range(2, min(len(s) - i - 1, k - 1) + 1): self.num_changes(i, kk) return self.num_changes(0, k)
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: @lru_cache(None) def dp(index, l): if l == 1: leftstr = s[index:] count = 0 left, right = 0, len(leftstr) - 1 while left < right: if leftstr[left] != leftstr[right]: count += 1 left += 1 right -= 1 return count if index == len(s) - 1: return 0 minvalue = float("inf") for i in range(index + 1, len(s)): leftstr = s[index:i] count = 0 left, right = 0, len(leftstr) - 1 while left < right: if leftstr[left] != leftstr[right]: count += 1 left += 1 right -= 1 minvalue = min(minvalue, count + dp(i, l - 1)) return minvalue return dp(0, k)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: self.cache = {} return self.dfs(s, k) def dfs(self, s, k): if (s, k) in self.cache: return self.cache[s, k] if k == 1: self.cache[s, k] = self.helper(s) return self.cache[s, k] n = len(s) ans = 100 for i in range(n - k + 1): temp = self.helper(s[: i + 1]) ans = min(ans, temp + self.dfs(s[i + 1 :], k - 1)) self.cache[s, k] = ans return ans def helper(self, s): l, r = 0, len(s) - 1 count = 0 while l < r: if s[l] != s[r]: count += 1 l += 1 r -= 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def __init__(self): self.table = {} def palindromePartition(self, s: str, k: int) -> int: def distanceToPalindrome(subs): k = 0 for i in range(int(len(subs) / 2)): if subs[i] != subs[len(subs) - i - 1]: k += 1 return k if len(s) == 1: return 0 if k == 1: return distanceToPalindrome(s) lens = len(s) if lens in self.table: if k in self.table[lens]: return self.table[lens][k] out = len(s) for i in range(1, len(s)): out = min( out, distanceToPalindrome(s[:i]) + self.palindromePartition(s[i:], k - 1), ) if lens not in self.table: self.table[lens] = {} self.table[lens][k] = out return out
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR DICT ASSIGN VAR VAR VAR VAR RETURN VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int): prefix = [([0] * len(s)) for _ in range(len(s))] for r in range(len(prefix)): for c in range(len(prefix[0])): if r < c: prefix[r][c] = self.find_change(s, r, c) dp = [([0] * len(s)) for _ in range(k)] dp[0] = prefix[0] for r in range(1, len(dp)): for c in range(len(dp[0])): if r < c: tmp = float("inf") for i in range(c): tmp = min(tmp, dp[r - 1][i] + prefix[i + 1][c]) dp[r][c] = tmp return dp[-1][-1] def find_change(self, s, r, c): res = 0 while r < c: if s[r] != s[c]: res += 1 r += 1 c -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: memo = dict() n = len(s) def find_cost(l, r): cost = 0 while l < r: if s[l] != s[r]: cost += 1 l += 1 r -= 1 return cost def dp(i, k): if (i, k) in memo: return memo[i, k] if n - i == 1: return 0 if k == 1: return find_cost(i, n - 1) memo[i, k] = float("inf") for j in range(i + 1, n - k + 2): memo[i, k] = min(memo[i, k], find_cost(i, j - 1) + dp(j, k - 1)) return memo[i, k] return dp(0, k)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: dp = {} def pali(i, j): diff = 0 while i >= 0 and j < len(s): if s[i] != s[j]: diff += 1 dp[i, j] = diff i -= 1 j += 1 for i in range(len(s)): pali(i, i) pali(i, i + 1) memo = {} def recurse(idx, numCuts): if numCuts == k and idx == len(s): return 0 if numCuts == k or idx == len(s): return float("inf") if (idx, numCuts) in memo: return memo[idx, numCuts] curMin = float("inf") for j in range(idx, len(s)): curMin = min(dp[idx, j] + recurse(j + 1, numCuts + 1), curMin) memo[idx, numCuts] = curMin return curMin return recurse(0, 0)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR STRING IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def count(s): l = len(s) c = 0 for i in range(int(l // 2)): if s[i] != s[l - 1 - i]: c += 1 return c s = "#" + s n = len(s) dp = [([float("inf")] * (k + 1)) for _ in range(n)] dp[0][0] = 0 for i in range(1, n): for j in range(1, k + 1): for m in range(j, i + 1): dp[i][j] = min(dp[i][j], dp[m - 1][j - 1] + count(s[m : i + 1])) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) dp = [[(0) for j in range(k)] for i in range(n)] memo = {} def cost(i, j): if memo.get((i, j)): return memo[i, j] c = 0 while i < j: if s[i] != s[j]: c += 1 i += 1 j -= 1 memo[i, j] = c return c for i in range(n): for j in range(k): if j > i: break if j == 0: dp[i][j] = cost(0, i) else: dp[i][j] = min( [ (dp[m][j - 1] + cost(m + 1, i)) for m in range(i) if j - 1 <= m ] ) return dp[n - 1][k - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) dp = [[math.inf for _ in range(k + 1)] for _ in range(n + 1)] dp[0][0] = 0 def changes(start, end): cnt = 0 while start < end: if s[start] != s[end]: cnt += 1 start += 1 end -= 1 return cnt for i in range(1, n + 1): for l in range(1, min(k, i) + 1): for j in range(l, i + 1): dp[i][l] = min(dp[i][l], dp[j - 1][l - 1] + changes(j - 1, i - 1)) return dp[n][k]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) dp = [[float("inf") for _ in range(k)] for _ in range(n)] def getMinChange(i, j): count = 0 while i < j: if s[i] != s[j]: count += 1 i += 1 j -= 1 return count dic = collections.defaultdict(int) for i in range(n): for j in range(i, n): dic[i, j] = getMinChange(i, j) for i in range(n): for j in range(k): if j >= i: dp[i][j] = 0 if j == 0: dp[i][j] = dic[0, i] z = k for i in range(n): for k in range(1, min(i + 1, z)): for j in range(0, i): dp[i][k] = min(dp[i][k], dp[j][k - 1] + dic[j + 1, i]) return dp[n - 1][z - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
def ch(a): l = len(a) c = 0 for i in range(l // 2): c += a[i] != a[l - i - 1] return c def f(s, ci, k, d): l = len(s) if k == 0: if ci != l: return math.inf return 0 if ci == l: return math.inf if d[ci][k] != -1: return d[ci][k] r = "" a = math.inf for i in range(ci, l): r += s[i] a = min(ch(s[ci : i + 1]) + f(s, i + 1, k - 1, d), a) d[ci][k] = a return a class Solution: def palindromePartition(self, s: str, k: int) -> int: l = len(s) d = [[(-1) for i in range(k + 1)] for j in range(l)] return f(s, 0, k, d)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, S: str, K: int) -> int: def palindrome_distance(li, ri): count = 0 while li < ri: count += S[li] != S[ri] li += 1 ri -= 1 return count def dfs(i, k): if i == N or k == 0: return 0 if i == N and k == 0 else math.inf if (i, k) in memo: return memo[i, k] ans = math.inf for j in range(i, N): if (i, j) not in dist_memo: dist_memo[i, j] = palindrome_distance(i, j) val = dist_memo[i, j] + dfs(j + 1, k - 1) ans = min(ans, val) memo[i, k] = ans return ans N = len(S) memo = {} dist_memo = {} ans = dfs(0, K) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR NUMBER NUMBER VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, key: int) -> int: @lru_cache(None) def repair(string: s) -> int: return sum( [ (1 if string[i] != string[-1 - i] else 0) for i in range(len(string) // 2) ] ) @lru_cache(None) def pp(start: int, end: int, k: int) -> int: if k == len(s): return 0 elif k == 1: return repair(s[start:end]) changes = end - start - k for i in range(k - 1, end - start): changes = min(pp(start, i, k - 1) + repair(s[i:end]), changes) return changes return pp(0, len(s), key)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NONE VAR FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: self.memo = {} return self.dfs(s, k) def dfs(self, s, k): if (s, k) not in self.memo: res = float("inf") if len(s) == k: res = 0 elif k == 1: res = sum(s[i] != s[-1 - i] for i in range(len(s) // 2)) else: for i in range(1, len(s) - k + 2): res = min(self.dfs(s[:i], 1) + self.dfs(s[i:], k - 1), res) self.memo[s, k] = res return self.memo[s, k]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) self.cost = [[(0) for _ in range(n)] for _ in range(n)] for l in range(2, n + 1): i = 0 while i + l - 1 < n: j = i + l - 1 self.cost[i][j] = self.cost[i + 1][j - 1] + (s[i] != s[j]) i += 1 self.dp = [[n for _ in range(k + 1)] for _ in range(n)] for i in range(n): self.dp[i][1] = self.cost[0][i] for K in range(2, k + 1): for j in range(i): self.dp[i][K] = min( self.dp[i][K], self.dp[j][K - 1] + self.cost[j + 1][i] ) return self.dp[n - 1][k]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def count_changes(_s): m = len(_s) // 2 return sum([int(c1 != c2) for c1, c2 in zip(_s[:m], _s[::-1][:m])]) n = len(s) if n == k: return 0 dp = [([float("inf")] * (n + 1)) for _ in range(k + 1)] for i in range(1, n + 1): dp[1][i] = count_changes(s[:i]) for _k in range(2, k + 1): for i in range(_k, n + 1): for j in range(_k - 1, i): dp[_k][i] = min(dp[_k][i], dp[_k - 1][j] + count_changes(s[j:i])) return dp[k][n]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, K: int) -> int: n = len(s) dp = [[(101) for k in range(K + 1)] for i in range(n)] num = [[(0) for j in range(n)] for i in range(n)] for i in range(n): for j in range(i): num[j][i] = self.helper(s[j : i + 1]) for i in range(n): dp[i][1] = num[0][i] for k in range(2, K + 1): for i in range(k - 1, n): for j in range(k - 2, i): dp[i][k] = min(dp[i][k], dp[j][k - 1] + num[j + 1][i]) return dp[n - 1][K] def helper(self, s): l, r = 0, len(s) - 1 count = 0 while l < r: if s[l] != s[r]: count += 1 l += 1 r -= 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: @lru_cache(None) def dfs(subs, k): if k == len(subs): return 0 if k == 1: return sum(subs[i] != subs[-1 - i] for i in range(len(subs) // 2)) res = float("inf") for i in range(1, len(subs) - k + 2): res = min(res, dfs(subs[:i], 1) + dfs(subs[i:], k - 1)) return res return dfs(s, k)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: changes = [([0] * len(s)) for _ in range(len(s))] for i in range(len(s)): for j in range(i, len(s)): start, end = i, j tmp = 0 while start < end: if s[start] != s[end]: tmp += 1 start += 1 end -= 1 changes[i][j] = tmp memo = [([float("inf")] * len(s)) for _ in range(k)] for i in range(len(s)): memo[0][i] = changes[i][-1] for i in range(1, k): for j in range(len(s)): for t in range(j + 1, len(s) - i + 1): memo[i][j] = min(memo[i - 1][t] + changes[j][t - 1], memo[i][j]) return memo[-1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) memo = dict() cost_memo = dict() def find_cost(l, r): a, b = l, r if (l, r) not in memo: cost = 0 while l < r: cost += s[l] != s[r] l += 1 r -= 1 cost_memo[a, b] = cost return cost_memo[a, b] def top_down(i, k): if n - i == 1: return 0 if k == 1: return find_cost(i, n - 1) if (i, k) not in memo: res = float("inf") for j in range(i + 1, n - k + 2): res = min(res, find_cost(i, j - 1) + top_down(j, k - 1)) memo[i, k] = res return memo[i, k] return top_down(0, k)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: sLen = len(s) dp = {} def moveOnce(prei, i0, k0): if (prei, i0, k0) in dp: return dp[prei, i0, k0] if k0 == k: l = prei r = sLen - 1 subAns = 0 while r > l: if s[r] != s[l]: subAns += 1 r -= 1 l += 1 dp[prei, i0, k0] = subAns return subAns l = prei r = i0 subAns = 0 while r > l: if s[r] != s[l]: subAns += 1 r -= 1 l += 1 if sLen - i0 - 1 <= k - k0: return subAns subAns = min( subAns + moveOnce(i0 + 1, i0 + 1, k0 + 1), moveOnce(prei, i0 + 1, k0) ) dp[prei, i0, k0] = subAns return subAns return moveOnce(0, 0, 1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: if len(s) <= k: return 0 def num_change(w): l, r = 0, len(w) - 1 count = 0 while l < r: if w[l] != w[r]: count += 1 l += 1 r -= 1 return count def dp(i, k): if (i, k) not in memo: if k == 1: memo[i, k] = num_change(s[: i + 1]) else: memo[i, k] = min( dp(j, k - 1) + num_change(s[j + 1 : i + 1]) for j in range(k - 2, i) ) return memo[i, k] memo = {} n = len(s) return dp(n - 1, k)
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def numChanges(self, s: str, start: int, end: int) -> int: num = 0 while start < end: if not s[start] is s[end]: num += 1 start += 1 end -= 1 return num def palindromePartition(self, s: str, k: int) -> int: cache = [[(0) for _ in range(len(s))] for _ in range(k)] size = len(s) for j in range(size - 1, -1, -1): cache[0][j] = self.numChanges(s, j, size - 1) for i in range(1, k): for j in range(size - 1 - i, -1, -1): cache[i][j] = min( [ (cache[i - 1][k + 1] + self.numChanges(s, j, k)) for k in range(j, size - i) ] ) return cache[k - 1][0]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) dp = [([float("inf")] * (k + 1)) for _ in range(n + 1)] dp[0][0] = 0 def cald(s): if len(s) == 1: return 0 else: lo, hi = 0, len(s) - 1 ans = 0 while lo < hi: if s[lo] != s[hi]: ans += 1 lo += 1 hi -= 1 return ans for i in range(1, n + 1): for j in range(1, k + 1): for h in range(j - 1, i): dp[i][j] = min(dp[i][j], dp[h][j - 1] + cald(s[h:i])) return dp[n][k]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def change(s): if len(s) == 1: return 0 i, j = 0, len(s) - 1 count = 0 while i < j: if s[i] != s[j]: count += 1 i += 1 j -= 1 return count memo = [([-1] * (k + 1)) for _ in range(len(s))] def dfs(s, start, k): if memo[start][k] != -1: return memo[start][k] if k == 1: memo[start][k] = change(s[start:]) return memo[start][k] count = len(s) - start for i in range(start, len(s) - k + 1): print(i, k) count = min(count, change(s[start : i + 1]) + dfs(s, i + 1, k - 1)) memo[start][k] = count return count return dfs(s, 0, k)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) costs = [([0] * (n - i)) for i in range(n)] for l in range(1, n): for start in range(n - l): old = costs[start + 1][l - 2] if l > 1 else 0 costs[start][l] = old + (0 if s[start] == s[start + l] else 1) opt = [costs[i][-1] for i in range(n)] for ki in range(2, k + 1): new_opt = [0] * (n - ki + 1) for start in range(len(new_opt)): new_opt[start] = min( costs[start][l] + opt[start + l + 1] for l in range(n - start - ki + 1) ) opt = new_opt return opt[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int): cost = [([0] * len(s)) for _ in range(len(s))] for i in range(len(s) - 1, -1, -1): for j in range(i + 1, len(s)): if s[i] == s[j]: cost[i][j] = cost[i + 1][j - 1] else: cost[i][j] = cost[i + 1][j - 1] + 1 dp = [([float("inf")] * k) for _ in range(len(s))] for i in range(len(s)): dp[i][0] = cost[0][i] for c in range(1, k): for j in range(i): dp[i][c] = min(dp[i][c], dp[j][c - 1] + cost[j + 1][i]) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def min_change(s, i, j): a = 0 while i < j: if s[i] != s[j]: a += 1 i += 1 j -= 1 return a cost = [[(0) for j in range(len(s))] for i in range(len(s))] n = len(s) for l in range(2, n + 1): for i in range(n - l + 1): j = i + l - 1 if s[i] != s[j]: cost[i][j] = cost[i + 1][j - 1] + 1 else: cost[i][j] = cost[i + 1][j - 1] dp = [[float("inf") for j in range(n)] for i in range(k + 1)] for i in range(n): dp[1][i] = cost[0][i] for kk in range(2, k + 1): for j in range(i): dp[kk][i] = min(dp[kk][i], dp[kk - 1][j] + cost[j + 1][i]) return dp[-1][-1]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: l = len(s) def jbn(a): le = len(a) return sum(a[i] != a[le - 1 - i] for i in range(le // 2)) dp = [([0] * (l + 1)) for _ in range(k + 1)] for j in range(1, l + 1): dp[1][j] = jbn(s[:j]) for i in range(2, k + 1): for j in range(i + 1, l + 1): dp[i][j] = min(dp[i - 1][m] + jbn(s[m:j]) for m in range(i - 1, j)) return dp[k][l]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) s = "#" + s dp = [([float("inf")] * (k + 1)) for _ in range(n + 1)] dp[0][0] = 0 count = [([0] * (n + 1)) for _ in range(n + 1)] for lens in range(2, n + 1): for i in range(n - lens + 2): j = i + lens - 1 if s[i] == s[j]: count[i][j] = count[i + 1][j - 1] else: count[i][j] = count[i + 1][j - 1] + 1 for i in range(1, n + 1): for t in range(1, min(i + 1, k + 1)): for j in range(t, i + 1): dp[i][t] = min(dp[i][t], dp[j - 1][t - 1] + count[j][i]) return dp[n][k]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: def substring_palindrome(substring): l = 0 r = len(substring) - 1 ans = 0 while l <= r: if substring[l] != substring[r]: ans += 1 l += 1 r -= 1 return ans n = len(s) dp = {} def backtrack(i, cuts): if (i, cuts) not in dp: if cuts == 1: dp[i, cuts] = substring_palindrome(s[: i + 1]) else: ans = float("inf") for end in range(cuts - 2, i): ans = min( ans, substring_palindrome(s[end + 1 : i + 1]) + backtrack(end, cuts - 1), ) dp[i, cuts] = ans return dp[i, cuts] return backtrack(n - 1, k)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: if not s: return self.memo = {} return self.helper(0, len(s), k, s) def helper(self, i, j, k, s): if (i, j, k) in self.memo: return self.memo[i, j, k] if len(s) <= k: return 0 if k == 1: min_change = self.cal_palindrome(s[i:j]) self.memo[i, j, k] = min_change return min_change min_change = float("inf") for split in range(i + 1, j): left = self.cal_palindrome(s[i:split]) right = self.helper(split, j, k - 1, s) if left + right < min_change: min_change = left + right self.memo[i, j, k] = min_change return min_change def cal_palindrome(self, s): if len(s) == 1: return 0 if len(s) % 2 == 0: c_right = len(s) // 2 c_left = c_right - 1 else: c_left = len(s) // 2 - 1 c_right = len(s) // 2 + 1 count = 0 while c_left >= 0: if s[c_left] != s[c_right]: count += 1 c_left -= 1 c_right += 1 return count
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN ASSIGN VAR DICT RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: dp = defaultdict(int) def pali(i, j): diff = 0 while i >= 0 and j < len(s): if s[i] != s[j]: diff += 1 dp[i, j] = diff i -= 1 j += 1 for i in range(len(s)): pali(i, i) pali(i, i + 1) @lru_cache(maxsize=None) def recurse(idx, numCuts): if numCuts > k: return float("inf") if idx == len(s): if numCuts == k: return 0 return float("inf") curMin = float("inf") for i in range(idx, len(s)): curMin = min(dp[idx, i] + recurse(i + 1, numCuts + 1), curMin) return curMin return recurse(0, 0)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN FUNC_CALL VAR STRING IF VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, k: int) -> int: n = len(s) visited = {} dic = {} def dfs(start, k): if (start, k) in dic: return dic[start, k] if k == 0: i = start j = n - 1 dic[start, k] = visited[i, j] return visited[i, j] count = float("inf") for i in range(start + 1, n - k + 1): count = min(count, visited[start, i - 1] + dfs(i, k - 1)) dic[start, k] = count return count for j in range(n): for i in range(j, -1, -1): if i == j: visited[i, j] = 0 elif i == j - 1: if s[i] != s[j]: visited[i, j] = 1 else: visited[i, j] = 0 elif s[i] == s[j]: visited[i, j] = visited[i + 1, j - 1] else: visited[i, j] = visited[i + 1, j - 1] + 1 return dfs(0, k - 1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
You are given a string s containing lowercase letters and an integer k. You need to : First, change some characters of s to other lowercase English letters. Then divide s into k non-empty disjoint substrings such that each substring is palindrome. Return the minimal number of characters that you need to change to divide the string.   Example 1: Input: s = "abc", k = 2 Output: 1 Explanation: You can split the string into "ab" and "c", and change 1 character in "ab" to make it palindrome. Example 2: Input: s = "aabbc", k = 3 Output: 0 Explanation: You can split the string into "aa", "bb" and "c", all of them are palindrome. Example 3: Input: s = "leetcode", k = 8 Output: 0   Constraints: 1 <= k <= s.length <= 100. s only contains lowercase English letters.
class Solution: def palindromePartition(self, s: str, K: int) -> int: len_s = len(s) if K >= len_s: return 0 dp = [([float("inf")] * (K + 1)) for _ in range(len_s + 1)] s = "0" + s dp[0][0] = 0 for i in range(1, len_s + 1): for k in range(1, min(K + 1, i + 1)): for j in range(k, i + 1): dp[i][k] = min(dp[i][k], dp[j - 1][k - 1] + self.helper(s, j, i)) print(dp) return dp[-1][-1] def helper(self, sub_s, l_idx, r_idx): count = 0 while l_idx < r_idx: if sub_s[l_idx] != sub_s[r_idx]: count += 1 l_idx += 1 r_idx -= 1 return count
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai) represents the number of meters the guest is going to take up if he sits at the restaurant table. Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table. Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number. Input The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters. The numbers in the lines are separated by single spaces. Output In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4. Examples Input 3 1 2 3 3 Output 1.3333333333 Note In the first sample the people will come in the following orders: * (1, 2, 3) — there will be two people in the restaurant; * (1, 3, 2) — there will be one person in the restaurant; * (2, 1, 3) — there will be two people in the restaurant; * (2, 3, 1) — there will be one person in the restaurant; * (3, 1, 2) — there will be one person in the restaurant; * (3, 2, 1) — there will be one person in the restaurant. In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).
n = int(input()) arr = list(map(int, input().split())) p = int(input()) dp = [[[(0) for k in range(n + 1)] for i in range(p + 1)] for i in range(n + 1)] for j in range(p + 1): for k in range(n + 1): dp[0][j][k] = 1 for i in range(1, n + 1): for j in range(p + 1): for k in range(1, n + 1): if j >= arr[k - 1]: dp[i][j][k] = dp[i][j][k - 1] + i * dp[i - 1][j - arr[k - 1]][k - 1] else: dp[i][j][k] = dp[i][j][k - 1] fact = n ans = 0 for i in range(1, n + 1): ans += dp[i][p][n] / fact fact *= n - i print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai) represents the number of meters the guest is going to take up if he sits at the restaurant table. Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table. Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number. Input The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters. The numbers in the lines are separated by single spaces. Output In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4. Examples Input 3 1 2 3 3 Output 1.3333333333 Note In the first sample the people will come in the following orders: * (1, 2, 3) — there will be two people in the restaurant; * (1, 3, 2) — there will be one person in the restaurant; * (2, 1, 3) — there will be two people in the restaurant; * (2, 3, 1) — there will be one person in the restaurant; * (3, 1, 2) — there will be one person in the restaurant; * (3, 2, 1) — there will be one person in the restaurant. In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).
n = input() n = int(n) arr = [0] * n fact = [0] * 51 a = input().split() p = input() p = int(p) for i in range(n): arr[i] = int(a[i]) if n == 1: if arr[0] <= p: print(1) else: print(0) exit(0) def pre(): fact[0] = 1 for i in range(1, 51): fact[i] = fact[i - 1] * i def get(arr, min_sum, max_sum): ways = [[(0) for i in range(max_sum + 1)] for i in range(len(arr) + 1)] ways[0][0] = 1 for i in range(len(arr)): for j in range(i, -1, -1): for k in range(max_sum, -1, -1): if k + arr[i] <= max_sum: ways[j + 1][k + arr[i]] += ways[j][k] ans = 0 counted = 0 for i in range(0, len(arr) + 1): for j in range(min_sum, max_sum + 1): ans += fact[i] * fact[n - i - 1] * ways[i][j] * i counted += fact[i] * fact[n - i - 1] * ways[i][j] return ans, counted pre() tot = 0 count = 0 sm = 0 for i in range(n): sm += arr[i] arr1 = [0] * (n - 1) got = 0 for j in range(n): if j == i: continue arr1[got] = arr[j] got += 1 how_many = get(arr1, max(0, p - arr[i] + 1), p) tot += how_many[0] count += how_many[1] def get_div(a, b): res = a // b a %= b for i in range(1, 10): a = int(a) a *= 10 x = a // b x1 = x res += pow(10, -i) * x1 a -= x * b return res if sm <= p: print(n) else: print(get_div(tot, fact[n]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is p meters. Maxim has got a dinner party tonight, n guests will come to him. Let's index the guests of Maxim's restaurant from 1 to n. Maxim knows the sizes of all guests that are going to come to him. The i-th guest's size (ai) represents the number of meters the guest is going to take up if he sits at the restaurant table. Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than p. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table. Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible n! orders of guests in the queue. Help Maxim, calculate this number. Input The first line contains integer n (1 ≤ n ≤ 50) — the number of guests in the restaurant. The next line contains integers a1, a2, ..., an (1 ≤ ai ≤ 50) — the guests' sizes in meters. The third line contains integer p (1 ≤ p ≤ 50) — the table's length in meters. The numbers in the lines are separated by single spaces. Output In a single line print a real number — the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10 - 4. Examples Input 3 1 2 3 3 Output 1.3333333333 Note In the first sample the people will come in the following orders: * (1, 2, 3) — there will be two people in the restaurant; * (1, 3, 2) — there will be one person in the restaurant; * (2, 1, 3) — there will be two people in the restaurant; * (2, 3, 1) — there will be one person in the restaurant; * (3, 1, 2) — there will be one person in the restaurant; * (3, 2, 1) — there will be one person in the restaurant. In total we get (2 + 1 + 2 + 1 + 1 + 1) / 6 = 8 / 6 = 1.(3).
n = int(input()) a = list(map(int, input().split())) p = int(input()) fact = [1] for i in range(1, 51): fact.append(fact[-1] * i) if sum(a) <= p: print(n) else: dp = [([0] * 56) for _ in range(56)] dp[0][0] = 1 for i in range(n): for j in range(n, -1, -1): for k in range(p): if a[i] + k <= p: dp[j + 1][a[i] + k] += dp[j][k] tot = sum( dp[i][j] * fact[i] * fact[n - i] for j in range(1, p + 1) for i in range(1, n + 1) ) print(f"{tot / fact[n]:.9f}")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari? You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation. -----Output----- Print the length of the longest common subsequence. -----Examples----- Input 4 3 1 4 2 3 4 1 2 3 1 2 4 3 Output 3 -----Note----- The answer for the first test sample is subsequence [1, 2, 3].
n, k = list(map(int, input().split())) ra = [([0] * k) for _ in range(n)] for p in range(k): for i, v in enumerate(map(int, input().split())): v -= 1 ra[v][p] = i g = [[] for _ in range(n)] for u in range(n): for v in range(n): if all(x < y for x, y in zip(ra[u], ra[v])): g[u].append(v) memo = [-1] * n def dfs(v): if memo[v] != -1: return memo[v] r = 1 for u in g[v]: r = max(r, dfs(u) + 1) memo[v] = r return r print(max(dfs(s) for s in range(n)))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari? You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation. -----Output----- Print the length of the longest common subsequence. -----Examples----- Input 4 3 1 4 2 3 4 1 2 3 1 2 4 3 Output 3 -----Note----- The answer for the first test sample is subsequence [1, 2, 3].
import sys input = sys.stdin.readline n, k = map(int, input().split()) a = [list(map(int, input().split())) for i in range(k)] g = [[] for i in range(n + 1)] for i in range(n): x = a[0][i] s = set(a[0][i + 1 :]) for j in range(k): idx = a[j].index(x) s1 = set(a[j][idx + 1 :]) s = s & s1 for y in s: g[x].append(y) dist = [-1] * (n + 1) def dfs(ver): if dist[ver] != -1: return dist[ver] res = 0 for to in g[ver]: res = max(res, dfs(to) + 1) dist[ver] = res return res MAX = max(dfs(i) for i in range(1, n + 1)) print(MAX + 1)
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 VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari? You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation. -----Output----- Print the length of the longest common subsequence. -----Examples----- Input 4 3 1 4 2 3 4 1 2 3 1 2 4 3 Output 3 -----Note----- The answer for the first test sample is subsequence [1, 2, 3].
def main(): n, k = tuple(map(int, input().split())) a = [set(range(n)) for _ in range(n)] for i in range(k): p = set() for j in map(int, input().split()): a[j - 1] -= p p.add(j - 1) sa = sorted(list(range(n)), key=lambda i: len(a[i])) maxx = [0] * n res = 0 for i in sa: m = 1 + maxx[max(a[i], key=lambda e: maxx[e])] if a[i] else 0 maxx[i] = m res = max(res, m) print(res) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari? You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation. -----Output----- Print the length of the longest common subsequence. -----Examples----- Input 4 3 1 4 2 3 4 1 2 3 1 2 4 3 Output 3 -----Note----- The answer for the first test sample is subsequence [1, 2, 3].
def solve(): n, k = map(int, input().split()) c = [[(-1) for i in range(n + 1)] for i in range(k)] dp = [(0) for i in range(n + 1)] a = [] for i in range(k): b = list(map(int, input().split())) for j, v in enumerate(b): c[i][v] = j a.append(b) for i in range(n): curpos = a[0][i] dp[i] = 1 for j in range(i): prevpos = a[0][j] ok = True for p in range(k): if c[p][curpos] < c[p][prevpos]: ok = False break if ok: dp[i] = max(dp[i], dp[j] + 1) print(max(dp)) return def main(): t = 1 for _ in range(t): solve() main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1, 2, ..., n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari? You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 1000; 2 ≤ k ≤ 5). Each of the next k lines contains integers 1, 2, ..., n in some order — description of the current permutation. -----Output----- Print the length of the longest common subsequence. -----Examples----- Input 4 3 1 4 2 3 4 1 2 3 1 2 4 3 Output 3 -----Note----- The answer for the first test sample is subsequence [1, 2, 3].
n, k = list(map(int, input().split())) a, b = [[] for row in range(6)], [[(0) for col in range(n + 1)] for row in range(6)] for i in range(k): a[i] = list(map(int, input().split())) for j in range(n): b[i][a[i][j]] = j dp = [1] * n for i in range(n): for j in range(i): flag = 1 for x in range(1, k): if b[x][a[0][j]] > b[x][a[0][i]]: flag = 0 break if flag: dp[i] = max(dp[i], dp[j] + 1) print(max(dp))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100
N, K, X = list(map(int, input().split())) arr = [0] + [int(i) for i in input().split()] dp = [[float("-inf") for _ in range(X + 1)] for _ in range(N + 1)] dp[0][X] = 0 for i in range(1, N + 1): for x in range(X): for p in range(1, K + 1): if i - p < 0: break if dp[i - p][x + 1] < 0: continue dp[i][x] = max(dp[i][x], dp[i - p][x + 1] + arr[i]) ans = max([max(v) for v in dp[N - K + 1 :]]) if ans < 0: print(-1) else: print(ans)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100
import sys S = sys.stdin.read() S = list( [ list(map(int, x)) for x in list([x.split() for x in [x for x in S.split("\n") if len(x) > 1]]) ] ) n, k, x, B = S[0][0], S[0][1], S[0][2], S[1] X = [[(-1) for i in range(x + 1)] for i in range(n + 1)] X[0][0] = 0 for i in range(1, n + 1): for j in range(1, x + 1): X[i][j] = max( [ X[i][j], max( [-1] + [ (X[i - l][j - 1] + B[i - 1]) for l in range(1, min([i, k]) + 1) if X[i - l][j - 1] != -1 ] ), ] ) print(max(list(map(max, X[n - k + 1 : n + 1]))))
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR LIST VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100
import sys input = sys.stdin.readline n, k, x = map(int, input().split()) a = list(map(int, input().split())) dp = [-1] * (n + 1) dp[0] = 0 for c in range(1, x + 1): dp0 = [-1] * (n + 1) for i in range(c, n + 1): ma = -1 for j in range(1, min(i, k) + 1): ma = max(ma, dp[i - j]) if ma == -1: break dp0[i] = ma + a[i - 1] dp = dp0 ans = -1 for i in range(k): ans = max(ans, dp[n - i]) print(ans)
IMPORT ASSIGN VAR 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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100
n, k, x = map(int, input().split()) a = [None] + list(map(int, input().split())) lo, hi = 0, 10**9 * 5000 q = [None] * (n + 1) def get(mid): f, r = 0, 0 q[0] = 0, 0, 0 for i in range(1, n + 1): if q[r][2] == i - k - 1: r += 1 cur = q[r][0] + a[i] - mid, q[r][1] + 1, i while r <= f and q[f] <= cur: f -= 1 f += 1 q[f] = cur if q[r][2] == n - k: r += 1 return q[r] while lo < hi: mid = lo + hi + 1 >> 1 _, cnt, _ = get(mid) if cnt >= x: lo = mid else: hi = mid - 1 sm, _, _ = get(lo) ans = max(-1, sm + x * lo) 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 VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100
import sys def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [([c] * b) for i in range(a)] def list3d(a, b, c, d): return [[([d] * c) for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[([e] * d) for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return list(map(int, input().split())) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print("Yes") def No(): print("No") def YES(): print("YES") def NO(): print("NO") INF = 10**18 MOD = 10**9 + 7 N, K, X = MAP() A = [0] + LIST() dp = list2d(X + 1, N + 1, -1) dp[0][0] = 0 for i in range(X): for j in range(N): if dp[i][j] == -1: continue for k in range(j + 1, min(j + K + 1, N + 1)): dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + A[k]) ans = max(dp[X][-K:]) print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP 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 NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $n$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $i$-th picture has beauty $a_i$. Vova wants to repost exactly $x$ pictures in such a way that: each segment of the news feed of at least $k$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $k=1$ then Vova has to repost all the pictures in the news feed. If $k=2$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. -----Input----- The first line of the input contains three integers $n, k$ and $x$ ($1 \le k, x \le n \le 200$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the beauty of the $i$-th picture. -----Output----- Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. -----Examples----- Input 5 2 3 5 1 3 10 1 Output 18 Input 6 1 5 10 30 30 70 10 10 Output -1 Input 4 3 1 1 100 1 1 Output 100
n, k, x = list(map(int, input().split())) a = [None] + list(map(int, input().split())) dp = [([-1] * (n + 1)) for i in range(x + 1)] dp[0][0] = 0 for i in range(1, x + 1): for j in range(1, n + 1): dp[i][j] = max(dp[i - 1][j2] for j2 in range(max(0, j - k), j)) if dp[i][j] != -1: dp[i][j] += a[j] ans = max(dp[x][j] for j in range(n - k + 1, n + 1)) print(ans)
ASSIGN VAR VAR VAR FUNC_CALL 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an array A of N positive integers. The task is to count the number of Arithmetic Progression subsequence in the array. Note: Empty sequence or single element sequence is Arithmetic Progression. Example 1: Input : N = 3 A[] = { 1, 2, 3 } Output : 8 Explanation: Arithmetic Progression subsequence from the given array are: {}, { 1 }, { 2 }, { 3 }, { 1, 2 }, { 2, 3 }, { 1, 3 }, { 1, 2, 3 }. Example 2: Input : N = 3 A[] = { 10, 20, 30,} Output : 8 Explanation: Arithmetic Progression subsequence from the given array are: {}, { 10 }, { 20 }, { 30 }, { 10, 20 }, { 20, 30 }, { 10, 30 }, { 10, 20, 30 }. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N and an array A as input parameters and returns the total count of AP sub-sequences in the array. Expected Time Complexity: O(N * (Minimum value in A - Maximum Value in A)) Expected Space Complexity: O(N) Constraints: 1<= N <=100 1<= A[i] <=100
class Solution: def count(self, N, A): dp = [] x = max(A) - min(A) y = min(A) - max(A) for i in range(N): d1 = {} dp.append(d1) c = N + 1 for i in range(N): for j in range(i): if A[i] - A[j] not in dp[j]: dp[i][A[i] - A[j]] = 2 c += 1 else: c += dp[j][A[i] - A[j]] dp[i][A[i] - A[j]] = dp[j][A[i] - A[j]] + 1 return c
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR
Given an array A of N positive integers. The task is to count the number of Arithmetic Progression subsequence in the array. Note: Empty sequence or single element sequence is Arithmetic Progression. Example 1: Input : N = 3 A[] = { 1, 2, 3 } Output : 8 Explanation: Arithmetic Progression subsequence from the given array are: {}, { 1 }, { 2 }, { 3 }, { 1, 2 }, { 2, 3 }, { 1, 3 }, { 1, 2, 3 }. Example 2: Input : N = 3 A[] = { 10, 20, 30,} Output : 8 Explanation: Arithmetic Progression subsequence from the given array are: {}, { 10 }, { 20 }, { 30 }, { 10, 20 }, { 20, 30 }, { 10, 30 }, { 10, 20, 30 }. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N and an array A as input parameters and returns the total count of AP sub-sequences in the array. Expected Time Complexity: O(N * (Minimum value in A - Maximum Value in A)) Expected Space Complexity: O(N) Constraints: 1<= N <=100 1<= A[i] <=100
class Solution: def count(self, n, a): mini = min(a) maxi = max(a) dp = [(0) for i in range(n + 1)] r = n + 1 for d in range(mini - maxi, maxi - mini + 1): sumi = [(0) for i in range(100000)] for i in range(n): dp[i] = 1 if a[i] - d >= 1: dp[i] += sumi[a[i] - d] r += dp[i] - 1 sumi[a[i]] += dp[i] return r
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR
Given an array A of N positive integers. The task is to count the number of Arithmetic Progression subsequence in the array. Note: Empty sequence or single element sequence is Arithmetic Progression. Example 1: Input : N = 3 A[] = { 1, 2, 3 } Output : 8 Explanation: Arithmetic Progression subsequence from the given array are: {}, { 1 }, { 2 }, { 3 }, { 1, 2 }, { 2, 3 }, { 1, 3 }, { 1, 2, 3 }. Example 2: Input : N = 3 A[] = { 10, 20, 30,} Output : 8 Explanation: Arithmetic Progression subsequence from the given array are: {}, { 10 }, { 20 }, { 30 }, { 10, 20 }, { 20, 30 }, { 10, 30 }, { 10, 20, 30 }. Your Task: You don't need to read input or print anything. Your task is to complete the function count() which takes an integer N and an array A as input parameters and returns the total count of AP sub-sequences in the array. Expected Time Complexity: O(N * (Minimum value in A - Maximum Value in A)) Expected Space Complexity: O(N) Constraints: 1<= N <=100 1<= A[i] <=100
class Solution: def count(self, N, arr): dp = [{} for i in range(0, N)] ans = N + 1 for i in range(1, N): for j in range(0, i): diff = arr[i] - arr[j] val = 1 + dp[j].get(diff, 0) if diff in dp[i]: dp[i][diff] += val else: dp[i][diff] = val ans += val return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
mod = 1000000007 ncr = [([0] * (n + 1)) for n in range(301)] for n in range(301): ncr[n][0] = ncr[n][n] = 1 for r in range(1, n): ncr[n][r] = ncr[n - 1][r] + ncr[n - 1][r - 1] T = int(input()) for _ in range(T): N, M = [int(i) for i in input().split()] x = [(int(i) - 1) for i in input().split()] D = [int(i) for i in input().split()] paths = [[[(0) for _ in range(D[n])] for m in range(M + 1)] for n in range(N)] for n in range(N): paths[n][0][x[n]] = 1 if D[n] > 1: for m in range(1, M + 1): paths[n][m][0] = paths[n][m - 1][1] paths[n][m][-1] = paths[n][m - 1][-2] for k in range(1, D[n] - 1): paths[n][m][k] = ( paths[n][m - 1][k - 1] + paths[n][m - 1][k + 1] ) % mod path_counts = [[(sum(paths[n][m]) % mod) for m in range(M + 1)] for n in range(N)] results = [[(0) for _ in range(M + 1)] for _ in range(N)] results[0] = path_counts[0] for n in range(1, N): for m in range(M + 1): for k in range(m + 1): results[n][m] = ( results[n][m] + ncr[m][k] * path_counts[n][k] * results[n - 1][m - k] ) % mod print(results[-1][-1])
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
MOD = 1000000007 Paths = [] Solve = [] Choose = [] p = [] s = [] N = 0 M = 0 def C(n, k): if n == 0 or k == 0 or k >= n: return 1 if Choose[n][k] != -1: return Choose[n][k] result = C(n - 1, k - 1) + C(n - 1, k) result %= MOD Choose[n][k] = result return result def paths(idx, position, m): if position < 0 or position >= s[idx]: return 0 if m == 0: return 1 if Paths[idx][position][m] != -1: return Paths[idx][position][m] result = paths(idx, position + 1, m - 1) + paths(idx, position - 1, m - 1) result %= MOD Paths[idx][position][m] = result return result def solve(idx, m): if m < 0: return 0 if idx == N: if m == 0: return 1 else: return 0 if Solve[idx][m] != -1: return Solve[idx][m] result = 0 for i in range(0, m + 1): result += C(m, i) * paths(idx, p[idx], i) * solve(idx + 1, m - i) result %= MOD Solve[idx][m] = result return result T = int(input().strip()) for i in range(T): N, M = map(int, input().strip().split(" ")) p = list(map(int, input().strip().split(" "))) p = [(i - 1) for i in p] s = list(map(int, input().strip().split(" "))) Paths = [ [[(-1) for m in range(M + 1)] for position in range(max(s) + 1)] for idx in range(N + 1) ] Solve = [[(-1) for m in range(M + 1)] for idx in range(N + 1)] Choose = [[(-1) for n in range(333)] for k in range(333)] print(solve(0, M))
ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR 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 STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
import sys MOD = 1000000007 def choose(n, k): if k < 0 or k > n: return 0 else: p, q = 1, 1 for i in range(1, min(k, n - k) + 1): p *= n q *= i n -= 1 return p // q def count_ways(N, M, D): ways = [[([0] * D[i]) for _ in range(M + 1)] for i in range(N)] for i in range(N): for j in range(D[i]): ways[i][0][j] = 1 if j > 0: ways[i][1][j] += 1 if j < D[i] - 1: ways[i][1][j] += 1 for s in range(2, M + 1): for j in range(D[i]): if j > 0: ways[i][s][j] += ways[i][s - 1][j - 1] if j < D[i] - 1: ways[i][s][j] += ways[i][s - 1][j + 1] return ways T = int(sys.stdin.readline()) c = {} for _ in range(T): N, M = list(map(int, sys.stdin.readline().split())) X = list(map(int, sys.stdin.readline().split())) D = list(map(int, sys.stdin.readline().split())) ways = count_ways(N, M, D) total = [ways[0][i][X[0] - 1] for i in range(M + 1)] for i in range(1, N): for j in reversed(range(1, M + 1)): k = j while k >= 0 and (j, k) not in c: c[j, k] = choose(j, k) k -= 1 total[j] = sum( total[k] * c[j, k] * ways[i][j - k][X[i] - 1] for k in range(j + 1) ) print(total[M] % MOD)
IMPORT ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR 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 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 FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def memoize(func): pool = {} def wrapper(*arg): if arg not in pool: pool[arg] = func(*arg) return pool[arg] return wrapper mod = 1000000007 ncr_max = 310 ncr = [([0] * ncr_max) for _ in range(ncr_max)] for i in range(ncr_max): ncr[i][0] = ncr[i][i] = 1 for j in range(1, i): ncr[i][j] = (ncr[i - 1][j] + ncr[i - 1][j - 1]) % mod @memoize def get_single_dim_path(dim, x, currM): if not 0 <= x < dim: return 0 if currM == 0: return 1 return ( get_single_dim_path(dim, x + 1, currM - 1) + get_single_dim_path(dim, x - 1, currM - 1) ) % mod T = int(input()) for case in range(T): N, M = map(int, input().split(" ")) xarr = [(int(s) - 1) for s in input().rstrip().split(" ")] darr = list(map(int, input().rstrip().split(" "))) @memoize def solve(k, currM): if k == N: return 1 if currM == 0 else 0 res = 0 for i in range(currM + 1): r = get_single_dim_path(darr[k], xarr[k], i) * ncr[currM][i] res += r * solve(k + 1, currM - i) return res % mod print(solve(0, M))
FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF NUMBER VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER 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 FUNC_DEF IF VAR VAR RETURN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
M, F, IF = 10**9 + 7, [1] * 301, [1] * 301 def powM(b, e): return ( b if e == 1 else powM(b, e >> 1) ** 2 * b % M if e & 1 else powM(b, e >> 1) ** 2 % M ) for i in range(2, 301): F[i] = F[i - 1] * i % M IF[i] = powM(F[i], M - 2) def C(n, m): return F[n] * IF[n - m] % M * IF[m] % M CP, rs, prs, crs = ( [([0] * (301 - i)) for i in range(301)], [0] * 301, [0] * 301, [0] * 301, ) def buildDim(x, d, m, rs): for i in range(m + 1): rs[i] = 0 for i in range(m + 1): for j in range(m - i + 1): CP[i][j] = 0 for i in range(0, x): CP[i][0] = 1 rs[i] += 1 for i in range(1, d - x + 1): CP[0][i] = 1 rs[i] += 1 for k in range(1, m + 1): for i in range(1, k): if i - (k - i) < x and k - i - i < d - x + 1: CP[i][k - i] = (CP[i - 1][k - i] + CP[i][k - i - 1]) % M rs[k] = (rs[k] + CP[i][k - i]) % M for _ in range(int(input())): n, m = map(int, input().split(" ")) x, D = [int(x) for x in input().rstrip().split(" ")], [ int(x) for x in input().rstrip().split(" ") ] buildDim(x[0], D[0], m, rs) for i in range(1, n): buildDim(x[i], D[i], m, crs) for i in range(m + 1): prs[i] = 0 for a in range(m + 1): for b in range(m - a + 1): prs[a + b] = (prs[a + b] + C(a + b, a) * crs[a] % M * rs[b]) % M rs, prs = prs, rs print(rs[m])
ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
import sys DEBUG = 0 if DEBUG: handler = open("./input") else: handler = sys.stdin cases = int(handler.readline()) MOD = 1000000007 def mirror(grid, upper_bound): ret = list(grid) for i in range(len(grid)): if ret[i] * 2 > upper_bound[i] + 1: ret[i] = upper_bound[i] + 1 - grid[i] return tuple(ret) mem_1 = {} mem_2 = {} cache = {} def calc_single(pos, upper_bound, moves): if (pos, upper_bound, moves) in mem_1: return mem_1[pos, upper_bound, moves] if moves == 0: return 1 ret = 0 if pos > 1: tmp = pos - 1 if tmp * 2 > upper_bound + 1: tmp = upper_bound + 1 - tmp ret += calc_single(tmp, upper_bound, moves - 1) if pos < upper_bound: tmp = pos + 1 if tmp * 2 > upper_bound + 1: tmp = upper_bound + 1 - tmp ret += calc_single(tmp, upper_bound, moves - 1) mem_1[pos, upper_bound, moves] = ret % MOD return ret def cmb(n, k): if k * 2 > n: k = n - k if k == 0: return 1 if (n, k) in mem_2: return mem_2[n, k] mem_2[n, k] = (cmb(n - 1, k - 1) + cmb(n - 1, k)) % MOD return mem_2[n, k] def calc_total(start, limits, steps): dimensions = len(start) if (dimensions, steps) in cache: return cache[dimensions, steps] if dimensions == 1: ret = calc_single(start[0], limits[0], steps) % MOD cache[dimensions, steps] = ret return ret ret = 0 for s in range(steps + 1): ret += ( cmb(steps, s) * calc_single(start[-1], limits[-1], s) * calc_total(start[:-1], limits[:-1], steps - s) ) ret %= MOD cache[dimensions, steps] = ret return cache[dimensions, steps] for _ in range(cases): dimensions, steps = map(int, handler.readline().split()) start = tuple(map(int, handler.readline().split())) limits = tuple(map(int, handler.readline().split())) start = mirror(start, limits) print(calc_total(start, limits, steps)) cache.clear() handler.close()
IMPORT ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
T = int(input()) MOD = 1000000007 C = [([0] * 400) for _ in range(400)] C[0][0] = 1 for i in range(400): C[i][0], C[i][i] = 1, 1 for j in range(1, i): C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD for t in range(T): N, M = [int(c) for c in input().split()] X = [int(c) for c in input().split()] D = [int(c) for c in input().split()] dp = [[([0] * (M + 1)) for j in range(D[i])] for i in range(N)] for i in range(N): for j in range(D[i]): dp[i][j][0] = 1 if j > 0: dp[i][j][1] += 1 if j < D[i] - 1: dp[i][j][1] += 1 for m in range(2, M + 1): for j in range(D[i]): if j > 0: dp[i][j][m] += dp[i][j - 1][m - 1] if j < D[i] - 1: dp[i][j][m] += dp[i][j + 1][m - 1] total = [dp[0][X[0] - 1][m] for m in range(M + 1)] for i in range(1, N): for j in reversed(range(1, M + 1)): total[j] = sum( total[k] * C[j][k] * dp[i][X[i] - 1][j - k] for k in range(j + 1) ) print(total[M] % MOD)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def nCk(n, k): if k >= 0 and k <= n: ntok = 1 ktok = 1 T = min(k, n - k) + 1 for t in range(1, T): ntok *= n ktok *= t n -= 1 return ntok // ktok else: return 0 def CalculateD(StartingPoint, Dim, N, M): D = [([0] * (M + 1)) for _ in range(N)] for n in range(N): if Dim[n] == 1: D[n][0] = 1 continue arr = [0] * (Dim[n] + 1) arr[StartingPoint[n]] = 1 D[n][0] = 1 for m in range(1, M + 1): narr = [0] * (Dim[n] + 1) for i in range(1, Dim[n] + 1): if arr[i] > 0: if i == 1: narr[i + 1] += arr[i] elif i == Dim[n]: narr[i - 1] += arr[i] else: narr[i + 1] += arr[i] narr[i - 1] += arr[i] D[n][m] = sum(narr) arr = narr return D def CalculateF(D, N, M): F = [([0] * (M + 1)) for _ in range(N)] for m in range(M + 1): F[0][m] = D[0][m] nck = [([0] * (m + 1)) for m in range(M + 1)] for m in range(1, M + 1): for i in range(m + 1): nck[m][i] = nCk(m, i) for n in range(1, N): F[n][0] = 1 for m in range(1, M + 1): toSum = [0] * (m + 1) for i in range(m + 1): toSum[i] = nck[m][i] * D[n][i] * F[n - 1][m - i] F[n][m] = sum(toSum) return F T = int(input()) for _ in range(T): N, M = tuple(map(int, input().split())) StartingPoint = tuple(map(int, input().split())) Dimensions = tuple(map(int, input().split())) D = CalculateD(StartingPoint, Dimensions, N, M) F = CalculateF(D, N, M) print(F[N - 1][M] % 1000000007)
FUNC_DEF IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
MOD = 1000000007 t = eval(input()) def predict(x, d, value): return x + value if x + value <= d and x + value >= 1 else x combi = [([1] * (300 + 1)) for i in range(300 + 1)] for i in range(1, 300 + 1): for j in range(1, i + 1): if j >= i // 2 + 1: combi[i][j] = combi[i][i - j] else: combi[i][j] = combi[i - 1][j - 1] + combi[i - 1][j] combi[i][j] %= MOD def fact(n): return 1 if n == 0 else n * fact(n - 1) % MOD def comb(n, c): result = 1 for i in range(c): result *= n - i result //= i + 1 result %= MOD return result while t > 0: t -= 1 n, m = map(int, input().split()) x1 = list(map(int, input().split())) x2 = list(x1) d = list(map(int, input().split())) count = [([0] * (d[i] + 1)) for i in range(n)] for i in range(n): count[i][x1[i]] = 1 newCount = [([0] * (d[i] + 1)) for i in range(n)] med = [([1] * (m + 1)) for i in range(n)] for temp in range(m): for i in range(n): for j in range(x2[i], x1[i] + 1): if j + 1 <= d[i]: newCount[i][j + 1] += count[i][j] if j - 1 >= 1: newCount[i][j - 1] += count[i][j] for i in range(n): if x1[i] + 1 <= d[i]: x1[i] += 1 if x2[i] - 1 >= 1: x2[i] -= 1 for i in range(n): curSum = 0 for j in range(d[i] + 1): count[i][j] = newCount[i][j] curSum = curSum + count[i][j] newCount[i][j] = 0 med[i][temp + 1] = curSum calc = [([0] * (m + 1)) for i in range(n)] sum = 0 for i in range(n): for j in range(m + 1): if i > 0: for mid in range(j + 1): calc[i][j] += calc[i - 1][mid] * med[i][j - mid] * combi[j][mid] else: calc[i][j] = med[i][j] print(calc[n - 1][m] % MOD)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_DEF RETURN VAR NUMBER NUMBER BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR WHILE VAR NUMBER VAR NUMBER 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def grid_walking(N, M, X, D): W = [[(0) for _ in range(N + 1)] for _ in range(M + 1)] for i in range(N): T = [[(0) for _ in range(M + 1)] for _ in range(D[i] + 1)] for j in range(1, D[i] + 1): T[j][0] = 1 for j in range(1, M + 1): for k in range(1, D[i] + 1): if k - 1 > 0: T[k][j] += T[k - 1][j - 1] T[k][j] %= 1000000007 if k + 1 <= D[i]: T[k][j] += T[k + 1][j - 1] T[k][j] %= 1000000007 W[0][i + 1] = 1 for j in range(1, M + 1): W[j][i + 1] = T[X[i]][j] C = [[(0) for _ in range(M + 1)] for _ in range(M + 1)] for i in range(M + 1): C[i][0] = 1 C[i][i] = 1 for i in range(1, M + 1): for j in range(1, i): C[i][j] = C[i - 1][j - 1] + C[i - 1][j] C[i][j] %= 1000000007 T = [[(0) for _ in range(N + 1)] for _ in range(M + 1)] for i in range(M + 1): T[i][1] = W[i][1] for i in range(N + 1): T[0][i] = 1 for i in range(2, N + 1): for j in range(1, M + 1): T[j][i] = 0 for k in range(j + 1): T[j][i] += C[j][j - k] * T[k][i - 1] * W[j - k][i] T[j][i] %= 1000000007 return T[M][N] def main(): T = int(input()) for _ in range(T): N, M = [int(v) for v in input().split()] X = [int(v) for v in input().split()] D = [int(v) for v in input().split()] print(grid_walking(N, M, X, D)) main()
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def grid_walking(startpos, maxpos, maxsteps): mod = 1000000007 one_d = [] for dim in range(len(maxpos)): table = [list([1] + [0] * maxsteps) for _ in range(maxpos[dim])] for steps in range(1, maxsteps + 1): for pos in range(1, maxpos[dim]): table[pos][steps] = table[pos - 1][steps - 1] for pos in range(maxpos[dim] - 1): table[pos][steps] += table[pos + 1][steps - 1] table[pos][steps] %= mod one_d.append(table[startpos[dim] - 1]) all_d = [one_d[0]] for dim in range(1, len(maxpos)): all_d.append([1] + [0] * maxsteps) for steps in range(1, maxsteps + 1): perms = 1 for i in range(steps + 1): all_d[dim][steps] += perms * one_d[dim][i] * all_d[dim - 1][steps - i] perms *= steps - i perms //= i + 1 all_d[dim][steps] %= mod return all_d[-1][-1] for _ in range(int(input())): _, maxsteps = [int(x) for x in input().split()] startpos = [int(x) for x in input().split()] maxpos = [int(x) for x in input().split()] answer = grid_walking(startpos, maxpos, maxsteps) print(answer)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def cal(loc, dim, n, m): if m == 0: return 1 ans = 0 for i, x in enumerate(loc): for step in [-1, 1]: if 0 < x + step <= dim[i]: curr = loc.copy() curr[i] = x + step ans += cal(curr, dim, n, m - 1) return ans def dimension(x, d, m): curr = [0] * d curr[x - 1] = 1 ans = [1] for _ in range(1, m + 1): next = list() for i in range(d): next.append( (curr[i - 1] if i > 0 else 0) + (curr[i + 1] if i < d - 1 else 0) ) ans.append(sum(next)) curr = next return ans cache = dict() def combi(n, k): if n in cache and k in cache[n]: return cache[n][k] ans = 1 for i in range(k): ans *= n - i for i in range(k): ans //= i + 1 if n not in cache: cache[n] = dict() if k not in cache[n]: cache[n][k] = ans return ans def path(d, n, m, curr): if m == 0: return 1 if curr == n - 1: return d[curr][m] ans = 0 for step in range(m + 1): ans += d[curr][step] * path(d, n, m - step, curr + 1) * combi(m, step) return ans % 1000000007 def path_new(d, n, m): ans = list() ans.append(d[0]) for i in range(1, n): line = [1] for j in range(1, m + 1): c = 0 for k in range(j + 1): c += ans[i - 1][k] * d[i][j - k] * combi(j, k) line.append(c) ans.append(line) return ans for _ in range(int(input().strip())): n, m = [int(x) for x in input().strip().split()] start = [int(x) for x in input().strip().split()] dim = [int(x) for x in input().strip().split()] d = list() for i in range(n): d.append(dimension(start[i], dim[i], m)) print(path_new(d, n, m)[n - 1][m] % 1000000007)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR LIST NUMBER NUMBER IF NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
tableCnK = {} def CnK(n, k): if k == 0: return 1 if k == 1: return n if (n, k) not in tableCnK: tableCnK[n, k] = CnK(n, k - 1) * (n - k + 1) // k return tableCnK[n, k] table = {} def PathsInOneDim(x, steps, max_x): global table if x > max_x or x < 1: return 0 if steps == 0: return 1 if (x, steps) not in table: if min(max_x - x, x - 1) >= steps: table[x, steps] = 2**steps else: table[x, steps] = PathsInOneDim(x - 1, steps - 1, max_x) + PathsInOneDim( x + 1, steps - 1, max_x ) return table[x, steps] def sol(start, M, dims): global table dptable1 = [([1] * (M + 1)) for _ in range(len(dims))] for k in range(len(dims)): table = {} for m in range(1, M + 1): dptable1[k][m] = PathsInOneDim(start[k], m, dims[k]) dptable2 = [dptable1[0], [1] * (M + 1)] index = 1 for k in range(1, len(dims)): for m in range(1, M + 1): count = 0 for m_in_dim_k in range(m + 1): a = dptable2[1 - index][m - m_in_dim_k] b = dptable1[k][m_in_dim_k] count += a * b * CnK(m, m_in_dim_k) dptable2[index][m] = count index = 1 - index return dptable2[1 - index][M] t = int(input()) for test in range(t): _, m = map(int, input().split()) start = tuple(map(int, input().split())) dims = list(map(int, input().split())) print(sol(start, m, dims) % 1000000007)
ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def f(di, xi, m): if not hasattr(f, "cache"): f.cache = [None] * 101 if f.cache[di] == None: f.cache[di] = [([-1] * 301) for i in range(di + 1)] if m == 0: res = 1 else: if f.cache[di][xi][m] == -1: f.cache[di][xi][m] = (f(di, xi - 1, m - 1) if xi > 0 else 0) + ( f(di, xi + 1, m - 1) if xi < di - 1 else 0 ) res = f.cache[di][xi][m] return res def k(m, j): if not hasattr(k, "cache"): k.cache = [([-1] * 300) for i in range(300)] if m <= j: res = 1 elif j == 1: res = m else: if k.cache[m - 1][j - 1] == -1: k.cache[m - 1][j - 1] = k(m - 1, j - 1) + k(m - 1, j) res = k.cache[m - 1][j - 1] return res def fd(n, m, x, d, cache): if cache == None: cache = [([None] * 301) for i in range(11)] if cache[n][m] == None: if n == 1: cache[n][m] = f(d[0], x[0], m) else: cache[n][m] = f(d[n - 1], x[n - 1], m) + fd(n - 1, m, x, d, cache) for j in range(1, m): cache[n][m] += ( f(d[n - 1], x[n - 1], m - j) * fd(n - 1, j, x, d, cache) * k(m, j) ) return cache[n][m] for i in range(int(input())): n, m = [int(i) for i in input().split()] x = [(int(i) - 1) for i in input().split()] d = [int(i) for i in input().split()] print(fd(n, m, x, d, None) % 1000000007)
FUNC_DEF IF FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP LIST NONE NUMBER IF VAR VAR NONE ASSIGN VAR VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR NONE ASSIGN VAR BIN_OP LIST NONE NUMBER VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NONE IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NONE NUMBER
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
MOD = 10**9 + 7 def main(): ncases = int(input()) for case in range(ncases): ndim, nmoves = readints() x = readints() d = readints() assert ndim == len(x) == len(d) print(solve(nmoves, x, d)) def readints(): return list(map(int, input().split())) def solve(m, x, d): prev = [1] + [0] * m for x1, d1 in zip(x, d): curr = [] newdim = comb1d(m, 1 - x1, d1 - x1) for i in range(m + 1): paths = 0 comb = 1 for j in range(i + 1): paths = (paths + prev[j] * newdim[i - j] * comb) % MOD comb = comb * (i - j) * pow(j + 1, MOD - 2, MOD) % MOD curr.append(paths) prev = curr return curr[-1] def comb1d(m, a, b): if not a <= 0 <= b: return [0] * (m + 1) tbl = [{n: (1) for n in range(max(-m, a), min(m, b) + 1)}] for seqlen in range(1, m + 1): lo = max(seqlen - m, a) hi = min(m - seqlen, b) newrow = {} prevrow = tbl[-1] prevrow[a - 1] = prevrow[b + 1] = 0 for i in range(lo, hi + 1): newrow[i] = (prevrow[i - 1] + prevrow[i + 1]) % MOD tbl.append(newrow) return [row[0] for row in tbl] main()
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR RETURN BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR
You are situated in an $n$ dimensional grid at position $\left(x[1],x[2],\ldots,x[n]\right)$. The dimensions of the grid are $(D[1],D[2],...D[n])$. In one step, you can walk one step ahead or behind in any one of the $n$ dimensions. This implies that there are always $2\times n$ possible moves if movements are unconstrained by grid boundaries. How many ways can you take $m$ steps without leaving the grid at any point? You leave the grid if at any point $x[i]$, either $x[i]\leq0$ or $x[i]>D[i]$. For example, you start off in a 3 dimensional grid at position $x=[2,2,2]$. The dimensions of the grid are $D=[3,3,3]$, so each of your axes will be numbered from $1$ to $3$. If you want to move $m=1$ step, you can move to the following coordinates: $\{[1,2,2],[2,1,2],[2,2,1],[3,2,2],[2,3,2],[2,2,3]\}$. If we started at $x=[1,1,1]$ in the same grid, our new paths would lead to $\{[1,1,2],[1,2,1],[2,1,1]\}$. Other moves are constrained by $x[i]\not\leq0$. Function Description Complete the gridWalking function in the editor below. It should return an integer that represents the number of possible moves, modulo $(10^9+7)$. gridWalking has the following parameter(s): m: an integer that represents the number of steps x: an integer array where each $x[i]$ represents a coordinate in the $i^{\mbox{th}}$ dimension where $1\leq i\leq n$ D: an integer array where each $D[i]$ represents the upper limit of the axis in the $i^{\mbox{th}}$ dimension Input Format The first line contains an integer $\boldsymbol{\boldsymbol{t}}$, the number of test cases. Each of the next $\boldsymbol{\boldsymbol{t}}$ sets of lines is as follows: The first line contains two space-separated integers, $n$ and $m$. The next line contains $n$ space-separated integers $x[i]$. The third line of each test contains $n$ space-separated integers $D[i]$. Constraints $1\leq t\leq10$ $1\leq n\leq10$ $1\leq m\leq300$ $1\leq D[i]\leq100$ $1\leq x[i]\leq D[i]$ Output Format Output one line for each test case. Since the answer can be really huge, output it modulo $10^9+7$. Sample Input 1 2 3 1 1 2 3 Sample Output 12 Explanation We are starting from (1, 1) in a $2\times3$ 2-D grid, and need to count the number of possible paths with length equal to $3$. Here are the $12$ paths: $(1,1)\rightarrow(1,2)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(1,1)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(1,2)$ $(1,1)\rightarrow(1,2)\rightarrow(1,3)\rightarrow(2,3)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\to(1,2)\to(2,2)\to(2,1)$ $(1,1)\rightarrow(1,2)\rightarrow(2,2)\rightarrow(2,3)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(1,1)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,1)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(1,2)$ $(1,1)\rightarrow(2,1)\rightarrow(2,2)\rightarrow(2,3)$ $12\mod(10^9+7)=12$
def fillOneDWalks(walksOneD, maxPoint): for size in maxPoint: if size in walksOneD: continue walksOneD[size] = walks1D(size, steps) def walks1D(size, steps): walks = [[(0) for s in range(0, steps + 1)] for origin in range(0, size + 1 + 1)] for origin in range(1, size + 1): walks[origin][0] = 1 for step in range(1, steps + 1): for origin in range(1, size + 1): walks[origin][step] = ( walks[origin - 1][step - 1] + walks[origin + 1][step - 1] ) return walks def stepMix(maxSteps): mix = [[(0) for ss in range(0, maxSteps + 1)] for s in range(0, maxSteps + 1)] for ss in range(0, maxSteps + 1): mix[1][ss] = ss + 1 mix[ss][1] = ss + 1 for s in range(2, maxSteps + 1): mix[s][0] = 1 mix[0][s] = 1 for ss in range(s, maxSteps + 1): combination = 0 for space in range(ss, -1, -1): combination += mix[s - 1][space] mix[s][ss] = combination mix[ss][s] = mix[s][ss] return mix def gridWalks(dimensionQty, steps, origin, maxPoint, walksOneD, mixSteps): walks = [[(0) for s in range(0, steps + 1)] for d in range(0, dimensionQty + 1)] for d in range(1, dimensionQty + 1): for s in range(1, steps + 1): dimSize = maxPoint[d - 1] dimOrigin = origin[d - 1] walks[d][s] = walks[d - 1][s] walks[d][s] += walksOneD[dimSize][dimOrigin][s] for dimStep in range(1, s): lowerDimStep = s - dimStep combinedWalks = ( walksOneD[dimSize][dimOrigin][dimStep] * walks[d - 1][lowerDimStep] * mixSteps[dimStep][lowerDimStep] ) walks[d][s] += combinedWalks return walks tests = int(input()) for t in range(0, tests): dimensionQty, steps = [int(i) for i in input().split(" ")] origin = [int(o) for o in input().split(" ")] maxPoint = [int(mo) for mo in input().split(" ")] walksOneD = {} fillOneDWalks(walksOneD, maxPoint) mixSteps = stepMix(steps) walks = gridWalks(dimensionQty, steps, origin, maxPoint, walksOneD, mixSteps) walkCount = walks[dimensionQty][steps] print(walkCount % 1000000007)
FUNC_DEF FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l = [[*map(int, input().split())], [*map(int, input().split())]] dp = [0, 0] for i in range(n): new_dp = dp[:] new_dp[0] = max(new_dp[0], dp[1] + l[0][i]) new_dp[1] = max(new_dp[1], dp[0] + l[1][i]) dp = new_dp print(max(dp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) r1 = [int(i) for i in input().split()] r2 = [int(i) for i in input().split()] dp1 = [0] * n dp2 = [0] * n dp1[0] = r1[0] dp2[0] = r2[0] for i in range(n - 1): if i < n - 2: dp1[i + 2] = max(dp1[i + 2], dp2[i] + r1[i + 2]) dp2[i + 2] = max(dp2[i + 2], dp1[i] + r2[i + 2]) dp1[i + 1] = max(dp1[i + 1], dp2[i] + r1[i + 1]) dp2[i + 1] = max(dp2[i + 1], dp1[i] + r2[i + 1]) print(max(dp1[-1], dp2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) dp = [[(0) for i in range(2)] for j in range(n + 2)] for i in range(1, n + 1): dp[i][0] = max(dp[i - 1][1] + l1[i - 1], dp[i - 1][0]) dp[i][1] = max(dp[i - 1][0] + l2[i - 1], dp[i - 1][1]) print(max(dp[n][0], dp[n][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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
dp = [[], []] def solve(T, N): dp[0].append(T[0][0]) dp[1].append(T[1][0]) for i in range(1, N): dp[0].append(T[0][i] + max(dp[1][i - 1], dp[1][i - 2] if i >= 2 else 0)) dp[1].append(T[1][i] + max(dp[0][i - 1], dp[0][i - 2] if i >= 2 else 0)) print(max(dp[0][-1], dp[1][-1])) n = int(input()) team = [] line = list(map(int, input().split())) team.append(line) line = list(map(int, input().split())) team.append(line) solve(team, n)
ASSIGN VAR LIST LIST LIST FUNC_DEF EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def CF1195C(): N = int(input()) r1 = tuple(map(int, input().split())) r2 = tuple(map(int, input().split())) r1sum = r1[-1] r2sum = r2[-1] for i in range(N - 2, -1, -1): r1sum, r2sum = max(r1[i] + r2sum, r1sum), max(r2[i] + r1sum, r2sum) return max(r1sum, r2sum) res = CF1195C() print(res)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) x = y = 0 for i in range(n): x, y = max(x, y + a[i]), max(y, x + b[i]) print(max(x, y))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) r1 = [int(i) for i in input().split(" ")] r2 = [int(i) for i in input().split(" ")] stat = [-1] * 240000 rm1 = r1[-1] rm2 = r2[-1] for i in range(n - 2, -1, -1): nrm1 = max(rm1, rm2 + r1[i]) nrm2 = max(rm2, rm1 + r2[i]) rm1 = nrm1 rm2 = nrm2 print(max(rm1, rm2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def solve(): n = int(input()) t1 = list(map(int, input().split())) t2 = list(map(int, input().split())) t1_score = [(0) for i in range(n)] t2_score = [(0) for i in range(n)] n -= 1 t1_score[n] = t1[n] t2_score[n] = t2[n] n -= 1 while n >= 0: t1_score[n] = max(t2_score[n + 1] + t1[n], t1_score[n + 1]) t2_score[n] = max(t1_score[n + 1] + t2[n], t2_score[n + 1]) n -= 1 print(max(t1_score[0], t2_score[0])) solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = [int(i) for i in input().split()] b = [int(i) for i in input().split()] ans = [a[n - 1], b[n - 1]] bans = [0, 0] for i in range(n - 2, -1, -1): bans[0] = max(a[i] + ans[1], ans[0]) bans[1] = max(b[i] + ans[0], ans[1]) ans[0] = bans[0] ans[1] = bans[1] bans[0] = 0 bans[1] = 0 print(max(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
N = int(input()) line2 = list(map(int, input().split())) line3 = list(map(int, input().split())) h = [line2, line3] dp = [[(0) for a in range(4)] for b in range(N + 2)] for i in range(N): for row in range(1, 4): if row == 1: first = dp[i - 1][2] + h[0][i] second = dp[i - 1][3] + h[0][i] dp[i][row] = max(first, second, h[0][i]) if row == 2: first = dp[i - 1][1] + h[1][i] second = dp[i - 1][3] + h[1][i] dp[i][row] = max(first, second, h[1][i]) if row == 3: first = dp[i - 1][1] second = dp[i - 1][2] dp[i][row] = max(first, second) print(max(dp[N - 1][1], dp[N - 1][2], dp[N - 1][3]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
input() c, d = 0, 0 for i, j in zip(map(int, input().split()), map(int, input().split())): c, d = max(c, d + i), max(d, c + j) print(max(c, d))
EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a = [*map(int, input().split())] b = [*map(int, input().split())] cache1 = [a[0]] cache2 = [b[0]] for i in range(n - 1): cache1.append(max(cache1[i], cache2[i] + a[i + 1])) cache2.append(max(cache2[i], cache1[i] + b[i + 1])) print(max(cache1[-1], cache2[-1]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
def gns(): return list(map(int, input().split())) n = int(input()) ps = [] ps.append(gns()) ps.append(gns()) dp = [([0] * 2) for i in range(n)] def get(i, t): if i < 0: return 0 else: return dp[i][t] ans = 0 for i in range(n): for t in range(2): mx = max(get(i - 1, 1 - t), get(i - 2, 1 - t)) dp[i][t] = mx + ps[t][i] ans = max(ans, dp[i][t]) print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) l1 = list(map(int, input().split())) l2 = list(map(int, input().split())) m1 = l1[0] m2 = l2[0] ans = max(m1, m2) i = 1 while i < n: l1[i] = max(l1[i], l1[i] + m2) l2[i] = max(l2[i], l2[i] + m1) m1 = max(m1, l1[i]) m2 = max(m2, l2[i]) ans = max(ans, l1[i], l2[i]) i = i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
from sys import stdin input = stdin.readline n = int(input()) h_1 = [int(i) for i in input().split()] h_2 = [int(i) for i in input().split()] max_h = [[(0) for i in range(n)] for j in range(2)] max_h[0][0] = h_1[0] max_h[1][0] = h_2[0] if n > 1: max_h[0][1] = h_1[1] + h_2[0] max_h[1][1] = h_2[1] + h_1[0] for i in range(2, n): max_h[0][i] = h_1[i] + max([max_h[1][i - 1], max_h[1][i - 2]]) max_h[1][i] = h_2[i] + max([max_h[0][i - 1], max_h[0][i - 2]]) print(max([max_h[0][n - 1], max_h[1][n - 1]]))
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) arr1 = [int(i) for i in input().split()] arr2 = [int(i) for i in input().split()] maxleft = arr1[-1] maxright = arr2[-1] extra = 0 dp = [(0, 0) for i in range(n)] dp[n - 1] = maxleft, maxright ans = max(maxleft, maxright) for i in range(n - 2, -1, -1): if i + 2 < n: maxleft = max(maxleft, arr1[i] + dp[i + 1][1], arr1[i] + dp[i + 2][1]) maxright = max(maxright, arr2[i] + dp[i + 1][0], arr2[i] + dp[i + 2][0]) dp[i] = maxleft, maxright else: maxleft = max(maxleft, arr1[i] + dp[i + 1][1]) maxright = max(maxright, arr2[i] + dp[i + 1][0]) dp[i] = maxleft, maxright ans = max(ans, dp[i][0], dp[i][1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
try: while True: n = int(input()) dp = [([0] * 100005) for _ in range(2)] a = [([0] * n) for _ in range(2)] a[0] = list(map(int, input().split())) a[0].insert(0, 0) a[1] = list(map(int, input().split())) a[1].insert(0, 0) for i in range(1, n + 1): dp[0][i] = max(dp[1][i - 1] + a[0][i], dp[0][i - 1]) dp[1][i] = max(dp[0][i - 1] + a[1][i], dp[1][i - 1]) print(max(dp[0][n], dp[1][n])) except EOFError: pass
WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
I = lambda: map(int, input().split()) n = I() temp = [0] * 3 for x, y in zip(I(), I()): c = max(temp[0], temp[1]) a = max(temp[1] + x, temp[2] + x, x) b = max(temp[0] + y, temp[2] + y, y) temp = [a, b, c] print(max(temp[0], temp[1]))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) v = [[int(i) for i in input().split()], [int(i) for i in input().split()]] dp = [[0] * (n + 1), [0] * (n + 1)] for i in range(1, n + 1): dp[0][i] = max(dp[0][i - 1], dp[1][i - 1] + v[0][i - 1]) dp[1][i] = max(dp[1][i - 1], dp[0][i - 1] + v[1][i - 1]) print(max(dp[0][n], dp[1][n]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image]
n = int(input()) a1 = list(map(int, input().split())) a2 = list(map(int, input().split())) if n == 1: print(max(a1[0], a2[0])) else: f = [a1[0], a2[0]] s = [a1[1] + f[1], a2[1] + f[0]] dp = [f, s] for i in range(2, n): an = [ max(dp[i - 1][1] + a1[i], dp[i - 2][1] + a1[i]), max(dp[i - 1][0] + a2[i], dp[i - 2][0] + a2[i]), ] dp.append(an) print(max(dp[-1][0], dp[-1][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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER