Max Sum Plus plus-hdu1024 (DP)

Source: Internet
Author: User

Problem Descriptionnow I Think you have got a 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 S 1+ t 2+ t 3+ t 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 a integer m (M > 0), your task is to find m pairs of I and J which make sum (i 1J 1) + SUM (i 2J 2) + SUM (i 3J 3) + ... + sum (i mJ m) Maximal (i x≤i y≤j xOr I x≤j y≤j xis 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 (i xJ x) (1≤x≤m) instead. ^_^

Inputeach test case would begin with a integers m and n, followed by n integers S 1+ t 2+ t 3... S N.
Process to the end of file.

Outputoutput the maximal summation described above in one line.

Sample INPUT1 3 1 2 32 6-1 4-2 3-2 3

Sample OUTPUT6 8

#include <stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<algorithm>#include<iostream>using namespacestd;#defineN 1000010#defineINF 0XFFFFFFFintDp[n];intM[n];intMax;intA[n];intMain () {intM,n;  while(SCANF ("%d%d", &m,&n)! =EOF) {         for(intI=1; i<=n;i++) {scanf ("%d",&A[i]); Dp[i]=0; M[i]=0; } dp[0]=0; m[0]=0;  for(intI=1; i<=m;i++) {Max=-INF;  for(intj=i;j<=n;j++) {Dp[j]=max (dp[j-1]+a[j],m[j-1]+A[j]); M[j-1]=Max; Max=Max (max,dp[j]); }} printf ("%d\n", Max); }    return 0;}

Max Sum Plus plus-hdu1024 (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.