Max Sum Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 932 Accepted Submission (s): 462
Problem Description
Given an integer sequence consisting of n positive integers
A1 a2 a3...
Calculate the sum of the consecutive integers (l1, l2, l3.. lm) that are not overlapped in the m segment in sequence.
Input
The first line is an integer n (0 ≤ n ≤ 1000). n = 0 indicates that the input ends.
The first number in the second row is m (1 ≤ m ≤ 20 ),
In the second row, there are m integers l1, l2. .. lm.
The third row is n integers a1, a2, a2....
Output
Output the maximum value of the integer and the m segment.
Sample Input
3
2 1 1
1 2 3
4
2 1 2
1 2 3 5
0 [cpp]
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define N 1010 # define M 21 int f [2] [N], a [N], B [M]; int main () {int I, j, k, l, n, m, T; while (scanf ("% d", & n) {scanf ("% d", & m ); a [0] = B [0] = 0; for (I = 1; I <= m; I ++) scanf ("% d ", & B [I]); for (I = 1; I <= n; I ++) {scanf ("% d", & k ); a [I] = a [I-1] + k;} // Since the input is a positive integer, and is increasing, f [I] [j] = max (f [I] [j], f [I-1] [j-B [I] + sum [j-B [I] + 1, j] memset (f, 0, sizeof (f )); k = 0; l = 1; for (I = 1; I <= m; I ++) {for (j = B [I]; j <= n; j ++) {// place group I f [l] [j] = max (f [l] [j], f [l] [J-1]); f [l] [j] = max (f [l] [j], f [k] [j-B [I]-a [j-B [I] + a [j]);} l ^ = 1; k ^ = 1;} printf ("% d \ n", f [k] [n]);} return 0 ;#include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
# Define N 1010
# Define M 21
Int f [2] [N], a [N], B [M];
Int main (){
Int I, j, k, l, n, m, T;
While (scanf ("% d", & n ){
Scanf ("% d", & m );
A [0] = B [0] = 0;
For (I = 1; I <= m; I ++) scanf ("% d", & B [I]);
For (I = 1; I <= n; I ++ ){
Scanf ("% d", & k );
A [I] = a [I-1] + k;
}
// Because the input is a positive integer and the input is incremental, f [I] [j] = max (f [I] [j], f [I-1] [j-B [I] + sum [j-B [I] + 1, j]
Memset (f, 0, sizeof (f ));
K = 0; l = 1;
For (I = 1; I <= m; I ++ ){
For (j = B [I]; j <= n; j ++) {// place the j-th in Group I
F [l] [j] = max (f [l] [j], f [l] [J-1]);
F [l] [j] = max (f [l] [j], f [k] [j-B [I]-a [j-B [I] + a [j]);
}
L ^ = 1; k ^ = 1;
}
Printf ("% d \ n", f [k] [n]);
}
Return 0;
}