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)