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
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to parse the problem: it's a typical triangle path sum problem (like maximum path sum in a triangle from top to bottom moving down or down-right). But there is a twist: "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;" Actually in standard triangle, from a position (i,j) you can go to (i+1,j) (down-left?) Wait clarify: Typically if we have a triangle like:
a
b c
d e f
We start at top a. Then we can move to either b (directly below) or c (below and to the right). That is standard. So the allowed moves: down (same column index) or down-right (column index + 1) if we consider left-aligned triangle.
So the problem is to find maximum sum from top to bottom.
Constraints: number of rows < 100 (positive). Number of test cases up to about 1000. So total operations are fine.
We need to parse input format carefully. Input description:
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.
Example:
Input:
2
3
1
2 1
1 2 3
4
1
1 2
4 1 2
2 3 1 1
Output:
5
9
So we read n test cases. For each test case, first integer is the number of rows, let's call rows R. Then next R lines contain the numbers for each row. The numbers are separated by spaces. Possibly trailing spaces? But we can parse simply.
We compute max path sum.
We can do dynamic programming: initialize dp array of size rows (max 100). For each row i (0-indexed), we have i+1 numbers. Starting from top row: dp[0] = triangle[0][0]. Then for each subsequent row, we update from right to left to avoid overwriting needed values: For j from i down to 0: dp[j] = triangle[i][j] + max(prev dp[j-1]? Wait careful: When moving from previous row (i-1) with positions j (0..i-1), we can move down (to same j) or down-right (to j+1). So for current row i, we want to compute new dp for each position j (0..i). The contributions: dp_prev[j] contributes to dp_curr[j] (down) and dp_prev[j] contributes to dp_curr[j+1] (down-right). Alternatively we can do in-place update from bottom up? Standard approach: for i from 0 to R-1, we can keep an array dp of size R (or i+1). Usually we iterate row by row and update from right to left: for j from i down to 0: dp[j] = max(dp[j], dp[j-1])? Not exactly.
Let's define dp after processing row i, for each column j in that row, the maximum sum to reach that position. Starting with row 0: dp[0] = tri[0][0]; length=1. For row i (i>=1): we need previous dp values for row i-1. We can compute a new array ndp of size i+1. For each j in 0..i:
ndp[j] = tri[i][j] + max( (j <= i-1 ? dp[j] : -inf), (j-1 >= 0 ? dp[j-1] : -inf) )
That's straightforward. Since R <= 100, we can allocate new each time or reuse with careful ordering. To avoid extra allocation, we can iterate from right to left on dp: For j from i down to 0:
dp[j] = tri[i][j] + max( (j < i ? dp[j] : -inf), (j-1 >= 0 ? dp[j-1] : -inf) ). But careful: when j = i, dp[j] doesn't exist before? We have dp array sized maybe R, with values from previous row only up to i-1. So dp[i] initially is garbage. We could use a separate array. Simpler: Use a vector and push_back new dp. Since rows are small, it's fine.
Alternatively, we can process from bottom up: start from last row and combine upward. But simpler is top-down.
For each test case: read number of rows. Then read rows accordingly. We'll store triangle as vector of vectors or just process on the fly? We need to compute DP from top, so we need rows in order. Could read all rows first then compute, or compute DP while reading? Since DP only needs previous row, we can compute on the fly:
Initialize dp as empty.
Read first row: one number. dp = [num].
For each subsequent row i from 2 to R:
Read the row values into a vector cur of size i.
Compute ndp size = i.
For j in 0..i-1:
left = (j-1 >=0) ? dp[j-1] : -inf? But note that all numbers are positive, so we can set -1 as sentinel. However max sum will always be positive, so we can use -1 to indicate invalid. But careful: if we use -1 and dp values could be? Actually sums will be >0. So we can use -1. Or we can compute as:
val = cur[j]
if j == 0: candidate = dp[0] (only from left? Actually from above-left there is none, only from above (j=0) which is dp[0]).
else if j == i-1: candidate = dp[j-1] (since dp[j] would be out of range, i-1? Wait row i has indices 0..i-1. For last column j = i-1, only previous dp at index i-2? Actually from previous row of length i-1, indices 0..i-2. For j = i-1, you can only come from dp[i-2] (down-right from previous last element) because down (same j) would require dp[i-1] which doesn't exist. So it's dp[j-1]. So we can handle edges.
So:
for j=0: ndp[0] = cur[0] + dp[0]
for j from 1 to i-2: ndp[j] = cur[j] + max(dp[j-1], dp[j])
for j = i-1: ndp[j] = cur[j] + dp[j-1]
Then set dp = ndp.
At end, answer = max(dp). Actually dp after last row contains sums to each bottom position. So max is answer.
That's O(R^2) per test case.
Since test cases up to 1000, and R up to 100, worst-case operations ~1000100100 = 10 million, fine.
We need to parse input quickly. Use fast I/O in C++ (cin with sync false, or scanf). I'll use scanf for simplicity.
Input format: First line n. Then for each test case: a line with number of rows (call rows). Then rows lines each containing numbers. The example shows that rows lines may have spaces. Possibly multiple spaces. We'll read using scanf or cin.
Because the numbers are up to 99, small. But we need to read each line. There is a catch: The problem statement says "the number of rows is strictly positive, but less than 100". So each test case: first integer is rows, then rows lines each containing exactly (row index+1) numbers. There may be extra spaces or newlines. However, reading with scanf in a loop reading numbers should be fine if we treat the input as a stream. But careful: The input lines may not be exactly separate? However it's typical: after reading rows, we then read rows lines. But we can also just read numbers sequentially ignoring line breaks, because we know how many numbers per row. However, the problem likely expects line-based reading. But to be safe, we can read line by line with getline, but that might be slower. Better: use scanf to read integers as they appear. For each test case, we read rows. Then we loop for i=0..rows-1: we need to read i+1 numbers. So we can have inner loop for j=0..i: read integer using scanf. This will consume numbers irrespective of newlines. That should work because the input is formatted such that each row's numbers are on a separate line, but even if there are extra spaces or line breaks, reading integers sequentially will still get them correctly. However, one must be cautious: after reading rows, the next token is the first number of the triangle. So we can simply read integers one by one. That is simpler and avoids line handling.
Thus algorithm:
int t; scanf("%d",&t);
while(t--) {
int rows; scanf("%d",&rows);
// dp initialization:
vector dp;
// first row
int val; scanf("%d",&val);
dp.push_back(val);
// for row = 2 to rows:
for (int i = 2; i <= rows; ++i) {
vector ndp(i);
for (int j = 0; j < i; ++j) {
scanf("%d",&val);
if (j == 0) ndp[j] = val + dp[0];
else if (j == i-1) ndp[j] = val + dp[j-1];
else ndp[j] = val + max(dp[j-1], dp[j]);
}
dp = ndp;
}
int ans = *max_element(dp.begin(), dp.end());
printf("%d\n", ans);
}
Edge cases: rows=1: then we just read first row, dp has one element, answer is that number. Our loop from i=2 to rows will not run because rows=1, so after reading first number, we go straight to max. But we need to ensure we read only one number. We did: after reading rows, we read the first row's number using scanf. That is correct.
But careful: In rows=1 case, after reading rows, there is a line containing that number. Our scanf reading that number will read it. Good.
One more nuance: The input may have test cases where numbers are separated by spaces and newlines arbitrarily. Using sequential integer reading is safe.
Potential pitfalls: Large input output, so use faster I/O: set sync_with_stdio(false) and use cin/cout or use scanf/printf. I'll use scanf/printf for simplicity in C.
But we need to ensure we don't read extra spaces. scanf("%d") skips whitespace, so fine.
Complexity: O(total numbers) which is sum over test cases of R*(R+1)/2, max about 1000*5050 ≈ 5 million, fine.
Now implement in C.
We'll need to include stdio.h, stdlib.h? Actually only need stdio for scanf/printf. And we may need to define a max function.
Implementation details: We'll use dynamic arrays? But rows <= 100, so we can use static arrays of size 100 for dp. However dp size changes each row. We can use two arrays: dp and ndp of size 100. We'll fill ndp for current row, then copy to dp. Or we can update in place from right to left to avoid extra copy. But with size 100, copying is trivial. We'll do two arrays.
Pseudo:
int t;
scanf("%d", &t);
while(t--) {
int rows;
scanf("%d", &rows);
int dp[100] = {0};
int ndp[100] = {0};
// first row
scanf("%d", &dp[0]);
int size = 1; // number of elements in current dp
for (int i = 2; i <= rows; ++i) {
// read i numbers into ndp temporarily? Actually we need to read the row values.
// We can read into an array rowVals or directly use ndp for sums after reading.
// But we need the row numbers to add. So we can read into ndp first as row values? Then later compute sum.
// Let's use row array:
int row[100];
for (int j = 0; j < i; ++j) {
scanf("%d", &row[j]);
}
// now compute ndp as sums:
ndp[0] = row[0] + dp[0];
for (int j = 1; j < i-1; ++j) {
int left = dp[j-1];
int right = dp[j];
ndp[j] = row[j] + (left > right ? left : right);
}
ndp[i-1] = row[i-1] + dp[i-2];
// copy ndp to dp
for (int j = 0; j < i; ++j) {
dp[j] = ndp[j];
}
size = i;
}
// compute max of dp[0..size-1]
int ans = dp[0];
for (int i = 1; i < size; ++i) {
if (dp[i] > ans) ans = dp[i];
}
printf("%d\n", ans);
}
But note: For rows=1, size remains 1, and we compute ans from dp[0]. That's fine.
Check indexing: For i=2, i-1=1, so the inner loop for j=1 to i-2 (i-2=0) doesn't run, then we set ndp[1] = row[1] + dp[0]. So works.
Alternatively, we can combine reading and computing without storing row values: while reading each number, we can compute ndp position? But need dp values for both neighbors. Could read all row numbers first, then compute. It's fine.
Potential overflow? Numbers up to 99, rows up to 100, max sum < 100*99 = 9900 fits in int.
Now, test with given example. Should output 5 and 9.
Now I'll consider if there are any traps: The problem statement says "the number of rows is strictly positive, but less than 100". So we are fine.
The warning: "large Input/Output data, be careful with certain languages". For C, scanf/printf is fine.
One nuance: The first line of input is n (number of test cases). But it says "equal to about 1000". So up to 1000 test cases.
Our code loops exactly n times.
Now I'll produce the final solution in C.
Edge cases: If rows=0? Not possible. So ignore.
Thus final answer.
But let's think if there is any alternate interpretation: The triangle path may be "either directly below or below and one place to the right". In some representations, it's "down-left and down-right" but here it's as we used.
Thus solution is correct.
Now produce final answer in proper format: Provide a solution description and code.
Since this is a typical problem, we can also mention dynamic programming.
I'll write final code with includes, main, and using simple loops.
Optionally we can use in-place dp from bottom up reading all rows first, but top-down is simpler.
But one caution: Our dp array of size 100, but we may need to clear after each test case. But we overwrite all used entries, so no problem.
Now deliver.
I will include comments to explain.
Alright. ```c
#include <stdio.h>
// Returns maximum of two integers
static int max(int a, int b) {
return a > b ? a : b;
}
int main(void) {
int t; // number of test cases
scanf("%d", &t);
code
while (t--) {
int rows;
scanf("%d", &rows);
// dp array – maximum sums to the current row
int dp[100] = {0};
// auxiliary array for the next row
int ndp[100] = {0};
// read the first row (only one number)
scanf("%d", &dp[0]);
int size = 1; // number of valid entries in dp
// process remaining rows (row index i = 2 .. rows)
for (int i = 2; i <= rows; ++i) {
// read the whole i-th row
int row[100];
for (int j = 0; j < i; ++j) {
scanf("%d", &row[j]);
}
// leftmost element: can only come from above left? Actually from above (same column)
ndp[0] = row[0] + dp[0];
// middle elements: can come from above-left (dp[j-1]) or above (dp[j])
for (int j = 1; j < i - 1; ++j) {
ndp[j] = row[j] + max(dp[j-1], dp[j]);
}
// rightmost element: can only come from above-left (dp[i-2])
ndp[i-1] = row[i-1] + dp[i-2];
// copy ndp back to dp for the next iteration
for (int j = 0; j < i; ++j) {
dp[j] = ndp[j];
}
size = i;
}
// the answer is the maximum value in the last row
int ans = dp[0];
for (int i = 1; i < size; ++i) {
if (dp[i] > ans) ans = dp[i];
}
printf("%d\n", ans);
}
return 0;