P1982 children's number, P1982 children's number
Description
There are n Children in a column. Each child has a number in his hand. This number can be positive or negative. Specify each
The child's feature value is equal to the number (at least one) of the children listed above him (including himself ).
The maximum value of the sum of digits in a friend's hand.
As a teacher of these children, you need to give each child a score, which is defined as follows: the first small
A friend's score is his feature value, and other children's scores are all the children (not himself) listed before him ),
The children's score plus the maximum value of its feature value.
Calculate the maximum score of all children, and maintain the maximum value in the output. After modulo the absolute value of p
Output.
Input/Output Format
Input Format:
The input file is number. in.
The first line contains two positive integers n and p, which are separated by a space.
The second row contains n numbers. Each two integers are separated by a space to indicate the numbers on each hand.
Output Format:
The output file name is number. out.
The output contains only one row and an integer, indicating the result of the maximum score modulo p.
Input and Output sample input sample #1:
5 997 1 2 3 4 5
Output sample #1:
21
Input example #2:
5 7 -1 -1 -1 -1 -1
Output sample #2:
-1
Description
Case 1:
The children's feature values are 1, 3, 6, 10, and 15, and the scores are 1, 2, 5, 11, 21, and the maximum value is 21.
The modulo of 997 is 21.
Case 2:
The children's feature values are-1,-1,-1,-1,-1, and scores are-1,-2,-2,-2,-2, and the maximum values are respectively.
The modulo of-1 to 7 is-1, and the output is-1.
For 50% of data, the absolute values of 1 ≤ n ≤ 1,000 and 1 ≤ p ≤ 1,000 cannot exceed 1000;
For 100% of data, 1≤n ≤ 1,000,000, 1≤p ≤ 10 ^ 9, the absolute values of other numbers do not exceed 10 ^ 9
We use dptz to represent the maximum feature value.
Use dpfs to represent the maximum score
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 # include <queue> 6 # include <algorithm> 7 # define lli long int 8 using namespace std; 9 const lli MAXN = 1000001; 10 void read (lli & n) 11 {12 char c = '+'; lli x = 0, flag = 1; 13 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag =-1 ;} 14 while (c> = '0' & c <= '9') {x = x * 10 + c-48; c = getchar ();} 15 n = x * flag; 16} 17 lli n, mod; 18 lli dptz [MAXN]; 19 lli dpfs [MAXN]; 20 lli a [MAXN]; 21 lli now = 0; // the current maximum field and 22 lli ans =-1270000; 23 int main () 24 {25 read (n); read (mod ); 26 for (lli I = 1; I <= n; I ++) 27 read (a [I]); 28 dptz [1] = a [1]; 29 for (lli I = 1; I <= n; I ++) 30 {31 now + = a [I]; 32 dptz [I] = now; 33 if (now <0) 34 now = 0; 35} 36 for (lli I = 2; I <= n; I ++) 37 dptz [I] = max (dptz [I], dptz [I-1]); 38 dpfs [1] = dptz [1]; 39 dpfs [2] = dpfs [1] + dptz [1]; 40 bool flag = 0; 41 for (lli I = 3; I <= n; I ++) 42 {43 dpfs [I] = dpfs [I-1]; 44 if (dptz [I-1]> 0) 45 dpfs [I] + = dptz [I-1]; 46 if (dpfs [I]> dpfs [1]) flag = 1; 47 if (flag = 1) 48 dpfs [I] = dpfs [I] % mod; 49} 50 if (flag = 1) 51 printf ("% lld", dpfs [n]); 52 else 53 printf ("% lld", dpfs [1]); 54 return 0; 55}