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. hdu---(Tell me the area)(几何/三角形面积以及圆面积的一些知识)

    Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdu-----(4514)湫湫系列故事——设计风景线(树形DP+并查集)

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  3. 重载Array类的contains方法

    var allFilters = self.filtersContainer?.filters ?? [OpalFilter]() if let sorter = filtersContainer?. ...

  4. HTTP协议(待完善)

    注:以物流做形象类比以便更好理解HTTP协议 一.HTTP是什么? HTTP的定义 HTTP( Hypertext Transfer Protocol, 超文本传输协议) 是在万维网上进行通信时所使用 ...

  5. Octopus系列之更新历史记录

    更新笔记历史 2015.2.3 更新了产品价格的计算方法     --采用了通用化的一个处理[支持各个国家货币]更新产品价格,增加两组价格:一组用来进行前台的展示:一组用来进行后台的计算更新了产品分类 ...

  6. iOS 消息推送原理及实现Demo

    一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...

  7. Android 解压html压缩数据

    public static String unzipHTML(String s){ int endPos = s.indexOf("\r\n\r\n"); if(endPos< ...

  8. 4通用Makefile编写

    a.c #include<stdio.h> #include "a.h" int main() { printf("hello world\n"); ...

  9. uboot启动 及命令分析(3)

    u-boot命令 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd_tbl_s { char     *name;   /* ...

  10. Exif的Orientation信息说明

    EXIF Orientation 参数让你随便照像但都可以看到正确方向的照片而无需手动旋转(前提要图片浏览器支持,Windows 自带的不支持) 这个参数在佳能.尼康相机照的照片是自带的,但我的奥林巴 ...