package arrays;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Input: 2
* 3
* 1
* 2 1
* 1 2 3
*
* 4
* 1
* 1 2
* 4 1 2
* 2 3 1 1
*
* Output: 5
* 9
*
* @author rajeevprasanna
*
*/
public class MaxDownwardSum {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
int n, i, j;
int[][] a = new int[100][100];
while (t-- > 0) {
n = Integer.parseInt(in.readLine());
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
a[i][j] = Integer.parseInt(in.readLine());
}
}
for (i = n - 2; i >= 0; i--) {
for (j = 0; j <= i; j++) {
a[i][j] += max(a[i + 1][j], a[i + 1][j + 1]);
}
}
System.out.println(a[0][0]);
}
}
private static int max(int a, int b) {
return a > b ? a : b;
}
}
Popular Posts
-
Given the sequence S1 = {a,b,c,d,…,x,y,z,aa,ab,ac…. } and given that this sequence corresponds (term for term) to the sequence S2 = {0,1...
-
Given a binary matrix, find out the maximum size square sub-matrix with all 1s. Algorithm: Let the given binary matrix be M[R][C]. The idea ...
-
The bustling town of Siruseri has just one sports stadium. There are a number of schools, colleges, sports associations, etc. that use this ...
-
Refer : http://education.cdacmumbai.in/education/pgdst/dsalfac/notes/ShellSort.pdf Shellsort works by comparing elements that are distant r...
-
Many of you might not be knowing that if you have 1 gmail id … you have infinte gmail ids… Seems to be a joke.. It’s not actually… for e.g ...
-
How to find kth smallest element in BST . you cannot use static/global variable and you cannot pass value of k to any function ? Solution ...
-
Find the maximum rectangle (in terms of area) under a histogram in linear time. I mean the area of largest rectangle that fits entirely in t...
-
There is an integer array consisting positive and negative integers. Find maximum positive difference S defined as: O(N) solution S = a[i] ...
-
Q: Given an integer x and an unsorted array of integers, describe an algorithm to determine whether two of the numbers add up to x. If we ha...
-
http://effprog.blogspot.com/2011/02/duplicate-removal-in-binary-search-tree.html http://shashank7s.blogspot.com/2011/03/write-program-to-rem...
Wednesday, March 23, 2011
Max Downward sum
Labels:
DP
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment