ime Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 45620   Accepted: 27612

Description

7
3 8
8 1 0
2 7 4 4
4 5 2 6 5 (Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Sample Output

30
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int N=;
int mp[N][N];
int dp[N][N];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
scanf("%d",&mp[i][j]); }
dp[i][i]=dp[i-][i-]+mp[i][i];
} for(int i=;i<=n;i++)
{
for(int j=;j<=i;j++)
{
if(i==) dp[i][j]=mp[i][j];
else if(j==) dp[i][j]=dp[i-][j]+mp[i][j];
else if(j==i) dp[i][j]=dp[i-][j-]+mp[i][j];
else dp[i][j]=max(dp[i-][j-]+mp[i][j],dp[i-][j]+mp[i][j]);
}
}
int ans=;
for(int i=;i<=n;i++)
{
if(ans<dp[n][i])
ans=dp[n][i];
}
cout<<ans<<endl;
}
return ;
}

The Triangle_DP的更多相关文章

随机推荐

  1. java SE学习之线程同步(详细介绍)

           java程序中可以允许存在多个线程,但在处理多线程问题时,必须注意这样一个问题:               当两个或多个线程同时访问同一个变量,并且一些线程需要修改这个变量时,那么这个 ...

  2. S2 第二章数据库的实现

    实现增删改查代码 1 select * from student --增加数据 insert into student (name,banji,xuehao) values(,) --修改数据 upd ...

  3. hdu5878 I Count Two Three(二分+ 打表)

    题目链接:hdu5878 I Count Two Three 题意:给出一个整数n, 找出一个大于等于n的最小整数m, 使得m可以表示为2^a * 3^b * 5^c * 7^d​​. 题解:打表预处 ...

  4. 3.7 嵌入式SQL

    可以放入所有高级语言中去,如C 因为,SQL是过程性语句,需要高级语言的非过程性处理集合的分类处理 一.一般形式 所有的SQL语句都必须加前缀EXEC SQL SQL语句完成结束标志(:或END EX ...

  5. if条件语句

    第四天 XMind 思维导图复习之前知识 数据类型-变量常量-运算符(表达式)-语句(顺序.分支.循环)-数组-函数 1.if语句格式 if(表达式) { 语句 } 注意: 1.如果,表达式成立,只执 ...

  6. MongoDB Replica Set 选举过程

    Replica Set 选举过程 心跳检测 假设我们有三个节点的replica sets:X,Y和Z节点.在replica sets结构中,这三个节点每2秒会各自向其它两个节点发送一个心跳检测请求.比 ...

  7. Ubuntu用户相关基本命令

    Linux是一个用户权限管理得很严格的系统,Ubuntu作为最受欢迎的桌面发行版,提供了简单易用的图形界面工具来管理用户,但是命令行工具往往更强大,用得熟练的话效率会更高.用户管理命令常用的有如下几个 ...

  8. java8Lambda详解

    [移步] http://blog.csdn.net/ioriogami/article/details/12782141/

  9. Unity中的Path对应各平台中的Path

    OS: Application.dataPath :                    Application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.a ...

  10. Android Phonebook编写联系人UI加载及联系人保存流程(三)

    2014-01-07 09:54:13  将百度空间里的东西移过来. 本文从点击“添加联系人”Button开始,分析新建联系人页面UI是如何加载,以及新的联系人信息是如何保存的,借此,我们一探Phon ...