Shi realized that he was almost out of money, even renting Shitalian lands. Shi was walking on a street, while thinking of a way to recover his fortune. In his way, he passed by a jewelry store. The owner of the store was a Shitalian man suspected of committing minor crimes, as cutting bushes and stealing old bread. Shi hated people who cut bushes, so he decided to rob the store to take revenge and make some money.

The store has n jewels, put in a row, one after another. Each jewel i can be sold in the black market for a value vi. Shi wants to steal as much as possible, but if he steals everything, the owner will notice, so if Shi steals the i-th jewel, he can't steal the i - 1-th, i - 2-th, i + 1-th and i + 2-th jewels.

Using the best strategy possible, calculate the sum of the jewels values that Shi can obtain.

Input

The input begins with an integer n (1 ≤ n ≤ 106), indicating the number of jewels in the store. The next line containsn integers. The i-th integer vi (1 ≤ vi ≤ 103) is the value of the i-th jewel.

Output

Output the maximum value that Shi can get.

Sample test(s)
input
4
1 2 3 4
output
5
input
6
1 2 4 0 3 0
output
5
input
7
2 10 12 24 29 69 0
output
81
input
10
15 1 6 3 7 100 9 15 80 95
output
210

题意:n个数字,每次最少隔两个取一个,求取得数的最大和

分析:dp,想法一、s[i]表示取第i个时最大和为多少,那就取不了i-1、i-2,可以取i-3、i-4、i-5,.....,当取i-6时,可取i-3,显然取了更划算,同理,i-7、i-8在算s[i-4]、s[i-5]时考虑过了,s[i]=a[i]+max{s[i-3],s[i-4],s[i-5]}

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s[],tem,ans=;
int main(){
scanf("%d",&n);
for(int i=;i<n+;i++)
{
scanf("%d",&tem);
s[i]=max(s[i-],max(s[i-],s[i-]))+tem;
ans=max(ans,s[i]);
}
printf("%d",ans);
return ;
}

想法二、也是dp,s[i]表示前i个的最大和为多少,s[i]=max{s[i-1],s[i-3]+第i个}

#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s[],tem;
int main(){
scanf("%d",&n);
for(int i=;i<n+;i++)
scanf("%d",&tem),s[i]=max(s[i-],tem+s[i-]);
printf("%d",s[n+]);
return ;
}

  

【Gym 100733D】Little thief Shi(取数,DP)的更多相关文章

  1. NOIP2007矩阵取数[DP|高精度]

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij均为非负整数.游戏规则如下: 1.每次取数时须从每行各取走一个元素,共n个.m次后取完矩阵所有元素: 2. ...

  2. 51Nod 1083 矩阵取数问题(矩阵取数dp,基础题)

    1083 矩阵取数问题 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 一个N*N矩阵中有不同的正整数,经过这个格子,就能获得相应价值的奖励,从左上走到右下,只能向下 ...

  3. [JZYZOJ 1288][洛谷 1005] NOIP2007 矩阵取数 dp 高精度

    https://www.luogu.org/problem/show?pid=1005   dp好想,高精度练手题,有点不舒服的是前后取数位置的计算,代码量太少才会写题这么慢,noip之前虽然重点放在 ...

  4. NOIP2000方格取数[DP]

    题目描述 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放 人数字0.如下图所示(见样例): A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 ...

  5. P1006 传纸条 (方格取数dp)

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个mm行nn列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运 ...

  6. luogu 1004 方格取数 dp

    题目链接 题意 设有N*N的方格图(N<=9),我们将其中的某些方格中填入正整数,而其他的方格中则放入数字0.如下图所示: A 0 0 0 0 0 0 0 0 0 0 13 0 0 6 0 0 ...

  7. neu1458 方格取数 dp解法

    题意: 有N * N个格子,每一个格子里有正数或者0,从最左上角往最右下角走,仅仅能向下和向右,一共走两次(即从左上角走到右下角走两趟),把全部经过的格子的数加起来,求最大值SUM,且两次假设经过同一 ...

  8. hiho 1617 - 方格取数 - dp

    题目链接 描述 给定一个NxN的方格矩阵,每个格子中都有一个整数Aij.小Hi和小Ho各自选择一条从左上角格子到右下角格子的路径,要求路径中每一步只能向右或向下移动,并且两条路径不能相交(除了左上右下 ...

  9. Vijos1451圆环取数[环形DP|区间DP]

    背景 小K攒足了路费来到了教主所在的宫殿门前,但是当小K要进去的时候,却发现了要与教主守护者进行一个特殊的游戏,只有取到了最大值才能进去Orz教主…… 描述 守护者拿出被划分为n个格子的一个圆环,每个 ...

  10. HDU 1565&1569 方格取数系列(状压DP或者最大流)

    方格取数(2) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

随机推荐

  1. 认识ASP.NET Windows身份认证

    本文摘自:细说ASP.NET Windows身份认证 Forms身份认证虽然使用广泛,不过,如果是在 Windows Active Directory 的环境中使用ASP.NET, 那么使用Windo ...

  2. springbootAdmin+eureka集群+swagger

    请移步githubb下载源码.知识共享.(https://github.com/yivvonllh) 或者直接git下载(https://github.com/yivvonllh/spring-clo ...

  3. 使用Topshelf管理Windows服务

    目的:以控制台方式开发Windows服务程序,调试部署方便. https://www.cnblogs.com/itjeff/p/8316244.html https://www.cnblogs.com ...

  4. 在 Linux 上搭建IntelliJ IDEA license server服务器

    IntelliJIDEALicenseServer_linux_amd64 ,把该文件传到服务器的某个目录,我是放在了/var/local/software目录下 sudo chmod +x ./In ...

  5. Luogu P1439 【模板】最长公共子序列

    又是模板题呵,但这次的难度有点增加. 先看题目第一个想到DP的经典算法,要O(n^2),然后想其它的算法. 其实我们衢州市一次联考有一题很像这题,不过还要难一点. 思想是离散化+最长不下降子序列(在这 ...

  6. Flutter - TabBar导航栏切换后,状态丢失

    上一篇讲到了 Flutter - BottomNavigationBar底部导航栏切换后,状态丢失 里面提到了TabBar,这儿专门再写一下吧,具体怎么操作,来不让TabBar的状态丢失.毕竟大家99 ...

  7. Nginx挂载维护页或返回自定义响应信息

    在服务停机升级或者服务暂不可用时,往往希望能够返回给用户更为明确和友好的响应信息.可以通过修改nginx配置文件,达到返回自定义信息的效果.有如下几种配置方式: (1)Nginx接收到的所有请求,都返 ...

  8. Redis Cluster日常操作命令梳理

    在之前的一篇文章已经介绍了Redis Cluster及其部署,下面说下Redis Cluster日常操作命令: 一.以下命令是Redis Cluster集群所独有的,执行下面命令需要先登录redis: ...

  9. KETTLE集群搭建

    KETTLE集群搭建 说明: 本文档基于kettle5.4 一.集群的原理与优缺点 1.1集群的原理 Kettle集群是由一个主carte服务器和多个从carte服务器组成的,类似于master-sl ...

  10. 《Linux内核设计与实现》第十八章读书笔记

    1.内核中的bug 内核中的bug表现得不像用户级程序中那么清晰——因为内核.用户以及硬件之间的交互会很微妙: 从隐藏在源代码中的错误到展现在目击者面前的bug,往往是经历一系列连锁反应的事件才可能触 ...