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. Thread 总结

    进程:是一个正在执行的程序 每一个进程执行都有一个执行顺序.该顺序是一个执行路劲,后者叫一个控制单元. 线程:就是进程中的一个独立控制单元. 线程在控制着进程的执行 一个进程中至少有个一个线程 Jav ...

  2. 快速集成iOS基于RTMP的视频推流

    前言 这篇blog是iOS视频直播初窥:<喵播APP>的一个补充. 因为之前传到github上的项目中没有集成视频的推流.有很多朋友简信和微博上问我推流这部分怎么实现的. 所以, 我重新集 ...

  3. 转:怎样在VMware ESXi上 克隆虚拟机

    Cloning virtual machines on VMware ESXi 翻译自http://www.dedoimedo.com/computers/vmware-esxi-clone-mach ...

  4. BZOJ3942 [Usaco2015 Feb]Censoring

    维护一个栈...如果栈顶出现了要被删除的字符串就全删掉就好了,判断的话...kmp就行了 /****************************************************** ...

  5. MVC1-5直接访问静态页面

    MVC模式下默认是无法访问站点内静态页面,昨日百度找了半天试了半天才试成功. 默认在Views文件外的静态页面可以访问,若要访问Views里的静态页面则需要修改View文件夹中的web.config: ...

  6. input[type=text]点击之后无边框, 一进页面就显示光标

    1.input[type=text]点击之后无边框 :outline:none; 2. 一进页面就显示光标: <script Language="javascript"> ...

  7. System.Web.Optimization找不到引用

    在程序包管理控制程序中录入:Install-Package Microsoft.AspNet.Web.Optimization,安装即可.

  8. 告别硬编码-发个获取未导出函数地址的Dll及源码

    还在为找内核未导出函数地址而苦恼嘛? 还在为硬编码通用性差而不爽吗? 还在为暴搜内核老蓝屏而痛苦吗? 请看这里: 最近老要用到内核未导出的函数及一些结构,不想再找特征码了,准备到网上找点符号文件解析的 ...

  9. ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设 ...

  10. Achieving High Availability and Scalability - ARR and NLB

    Achieving High Availability and Scalability: Microsoft Application Request Routing (ARR) for IIS 7.0 ...