[VJ][DP]Max Sum Plus Plus

Max Sum Plus Plus

Description

Now I think you have got an AC in Ignatius.L‘s "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.

Given a consecutive number sequence S 1, S 2, S3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (1 ≤ i ≤ j ≤ n).

Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(i m, jm) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j xis not allowed).

But I`m lazy, I don‘t want to write a special-judge module, so you don‘t have to output m pairs of i and j, just output the maximal summation of sum(i x, j x)(1 ≤ x ≤ m) instead. ^_^

Input

Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n
Process to the end of file.

Output

Output the maximal summation described above in one line.

Examples

Input

1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Output

6
8

正确解法:

dp太难了,妈妈救我。

题目的意思是,在n个数字中找到m个不相交的序列,求这序列和最大。

设前i个序列中最后一位数字的下标为j的序列和为 f[i][j]

f[i][j]=max(f[i][j-1],f[i-1][k])+a[j]; (i-1<=k< j)

在找 f[i][j] 时,最后一位数字下标为j时,要么这个数字跟着前一个序列 f[i][j-1]+a[j],要么这个数字新开一个序列 找一个比 i-1大的 比 j 小的序列的最大数加上a[j]。

原文地址:https://www.cnblogs.com/Kaike/p/10008784.html

时间: 2024-11-02 08:57:10

[VJ][DP]Max Sum Plus Plus的相关文章

HDU1024——DP——Max Sum Plus Plus

Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive number sequ

HDU1003——DP——Max Sum

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

HDU 1024 DP Max Sum Plus Plus

题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题. kuangbin专题. dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j 注意可以换成一维数组.第一维i没有影响,我们要求dp[m][n],所以i维可以省去,每次保留前j-1个数之中最大的部分. 1 #include<iostream> 2 #include<string> 3 #include<algorith

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b

hdu1024 Max Sum Plus Plus (Dp)

hdu1024 Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive numbe

HDU 1024:Max Sum Plus Plus(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24675    Accepted Submission(s): 8478 Problem Description Now I think you have g

Hdu 1024 Max Sum Plus Plus (dp)

题目链接: Hdu 1024 Max Sum Plus Plus 题目描述: 给出n个数,问m段连续子序列的和相加最大是多少? 解题思路: dp[i][j]表示把前i个元素(包括第i个),分成j段的最大和.状态转移方程就是dp[i][j] = max (dp[i-1][j] + arr[j],  max( dp[k][j-1]) + arr[j]),其中0<k<i.(第i个元素是保存在第j段,还是自己单独成段) 由于1<=n<=1000,000.n*n的数组肯定会爆炸,所以要对方程

hdu1003 1024 Max Sum&amp;Max Sum Plus Plus【基础dp】

dp是竞赛中常见的问题,也是我的弱项orz,更要多加练习.看到邝巨巨的dp专题练习第一道是Max Sum Plus Plus,所以我顺便把之前做过的hdu1003 Max Sum拿出来又做了一遍 HDU 1003 Max Sum 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目描述:给一个整数序列,求其中的一段连续子序列,使它的和最大值以及这个序列的起始下标 思路:简单的dp,抓住“连续”来入手.构造pair<int,int> dp[