Let's consider a triangle of numbers in which a number appears in the first line, two…
- User
Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths starting from the top towards the base, so that: on each path the next number is located on the row below, more precisely either directly below or below and one place to the right; the number of rows is strictly positive, but less than 100 all numbers are positive integers between O and 99. Input
In the first line integer n - the number of test cases (equal to about 1000). Then n test cases follow. Each test case starts with the number of lines which is followed by their content.
Output
For each test case write the determined value in a separate line.
Example
Input: 2 3 1 2 1 1 2 3 4 1 1 2 4 1 2 2 3 1 1
Output: 5 9
Warning: large Input/Output data, be careful with certain languages
Assistant
pythonimport sys data = list(map(int, sys.stdin.read().split())) ptr = 0 n_tests = data[ptr] ptr += 1 for _ in range(n_tests): n_rows = data[ptr] ptr += 1 total_elements = n_rows * (n_rows + 1) // 2 elements = data[ptr:ptr + total_elements] ptr += total_elements triangle = [] current = 0 for i in range(n_rows): row_length = i + 1 row = elements[current:current + row_length] triangle.append(row) current += row_length dp_prev = [triangle[0][0]] for i in range(1, n_rows): current_row = triangle[i] dp_curr = [] for j in range(len(current_row)): left = dp_prev[j-1] if j-1 >= 0 else 0 right = dp_prev[j] if j < len(dp_prev) else 0 dp_curr.append(max(left, right) + current_row[j]) dp_prev = dp_curr print(max(dp_prev))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.