Hdoj Max Sum plus plus "DP"

Source: Internet
Author: User

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 has got an AC in IGNATIUS.L ' s "Max Sum" problem. To is a brave acmer, we always challenge ourselves to more difficult problems. Now you is faced with a more difficult problem.

Given a consecutive number sequence S1, S2, S3, S4 ... Sx... Sn (1≤x≤n≤1,000,000, -32768≤sx≤32767). We define a function sum (i, j) = Si + ... + Sj (1≤i≤j≤n).

Now given a integer m (M > 0), your task is to find m pairs of I and J which make sum (I1, J1) + sum (i2, J2) + sum (i3, J3) + ... + sum (IM, JM) maximal (IX≤IY≤JX or IX≤JY≤JX is not allowed).

But I ' m lazy, I don't want to the write a Special-judge module, so you don ' t has to output m pairs of I and j, just output th e maximal summation of sum (ix, JX) (1≤x≤m) instead. ^_^

Input

Each test case would begin with a integers m and n, followed by n integers S1, S2, S3 ... Sn.
Process to the end of file.

Output

Output the maximal summation described above in one line.

Sample Input

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

Sample Output

6
8

Hint

Huge input, scanf and dynamic programming is recommended.

Author

Jgshining (Aurora Dazzle)
Test instructions: Find M continuous subsequence in the given n number, so that the sum of each sub-sequence is maximal.
Problem Solving Links
Code (not too understand, when to understand the time to fill in a detailed explanation):

#include <cstdio>#include <iostream>#include <cstring>Const intM =1e6+5;#define LL __int64using namespace STD; LL Pre[m], num[m];inlinell Max (ll A, ll B) {returnA > B? A:B;}voidDP (intMintN) {LL temp;//temp is dp[i][j]     for(inti =1; I <= m; + + i) {temp =0; for(intj =1; J <= I;        + + j) Temp + = Num[j]; Pre[n] = temp; for(intj = i+1; J <= N; + + j) {temp = max (temp, pre[j-1]) +num[j];//pre[j-1] is Max (Dp[i-1][t]) (i-1 <= T <= i-1);pre[j-1] = Pre[n];        Pre[n] = max (temp, pre[n]); }    }}intMain () {intN, M; while(scanf("%d%d", &m, &n) = =2){ for(inti =1; I <= N; + + i) {scanf("%i64d", num+i); Pre[i] =0; } DP (M, n);printf("%i64d\n", Pre[n]); }return 0;}

Hdoj Max Sum plus plus "DP"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.