B. Maximum Sum
time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output
You have an array
a
a
a of
n
n
n integers.
You perform exactly k k k operations on it. In one operation, you select any contiguous subarray of the array a a a (possibly empty) and insert the sum of this subarray anywhere in the array.
Your task is to find the maximum possible sum of the array after k k k such operations.
As this number can be very large, output the answer modulo 1 0 9 + 7 10^9 + 7 109+7.
Reminder: the remainder of a number x x x modulo p p p is the smallest non-negative y y y such that there exists an integer q q q and x = p ⋅ q + y x = p \cdot q + y x=p⋅q+y.
Input
Each test consists of several test cases. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1≤t≤104) — the number of test cases. Then follows the description of the test cases.
The first line of each test case contains two integers n n n and k k k ( 1 ≤ n , k ≤ 2 ⋅ 1 0 5 1 \le n, k \le 2 \cdot 10^5 1≤n,k≤2⋅105) — the length of the array a a a and the number of operations, respectively.
The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,…,an ( − 1 0 9 ≤ a i ≤ 1 0 9 -10^9 \le a_i \le 10^9 −109≤ai≤109) — the array a a a itself.
It is guaranteed that the sum of the values of n n n and k k k for all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2⋅105.
Output
For each test, output a single integer — the maximum sum of the array that can be obtained after k k k operations modulo 1 0 9 + 7 10^9 + 7 109+7.
Example
inputCopy
12
2 2
-4 -7
3 3
2 2 8
1 7
7
5 1
4 -2 8 -12 9
7 4
8 14 -9 6 0 -1 3
7 100
5 3 -8 12 -5 -9 3
6 1000
-1000000000 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000
2 1
1000000000 8
5 4
0 0 0 0 0
6 10
48973 757292 58277 -38574 27475 999984
7 1
-1000 1000 -1000 1000 -1000 1000 -1000
10 10050
408293874 -3498597 7374783 295774930 -48574034 26623784 498754833 -294875830 283045804 85938045
outputCopy
999999996
96
896
17
351
716455332
42
2
0
897909241
0
416571966
Note
In the first test case, it is advantageous to take an empty subarray of the array twice and insert the sum of the empty subarray (zero) anywhere, then the sum of the resulting array will be ( − 4 ) + ( − 7 ) + 0 + 0 = − 11 (-4) + (-7) + 0 + 0 = -11 (−4)+(−7)+0+0=−11, modulo 1 0 9 + 7 10^9 + 7 109+7 this is 999 999 996 999\,999\,996 999999996.
In the second test case, it is advantageous to take the sum of the entire array three times and place it anywhere in the array, then one of the possible sequences of actions: [ 2 , 2 , 8 2, 2, 8 2,2,8] → \rightarrow → [ 2 , 2 , 8 , 12 2, 2, 8, 12 2,2,8,12] → \rightarrow → [ 2 , 2 , 8 , 12 , 24 2, 2, 8, 12, 24 2,2,8,12,24] → \rightarrow → [ 2 , 2 , 8 , 12 , 24 , 48 2, 2, 8, 12, 24, 48 2,2,8,12,24,48], the sum of the final array is 2 + 2 + 8 + 12 + 24 + 48 = 96 2 + 2 + 8 + 12 + 24 + 48 = 96 2+2+8+12+24+48=96.
In the fourth test case, it is advantageous to take a subarray of the array consisting of the first three numbers (i.e. consisting of the numbers 4 , − 2 4, -2 4,−2 and 8 8 8) and insert its sum at the beginning of the array, thereby obtaining the array [ 10 , 4 , − 2 , 8 , − 12 , 9 10, 4, -2, 8, -12, 9 10,4,−2,8,−12,9], the sum of this array is 17 17 17.
In the seventh test case, it will always be advantageous for us to take an empty subarray of the array. In this case, the sum of the resulting array will not differ from the sum of the original. The answer will be the sum of the original array, taken modulo — 42 42 42, because ( − 6 ⋅ ( 1 0 9 + 7 ) + 42 = − 6 000 000 000 ) (-6 \cdot (10^9 + 7) + 42 = -6\,000\,000\,000) (−6⋅(109+7)+42=−6000000000).
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
ll t;
ll a[N], num[N];
int main()
{
cin >> t;
while(t --){
ll n , k, s = 0, res = 0, sum = 0;
cin >> n >> k;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
sum = ( a[i] + sum ) % MOD;
}
for(int i = 1; i <= n; i ++)
{
if(s < 0) s = a[i];
else s += a[i];
res = max(res, s);
}
// cout << res << "flk" << endl;
while(k --){
while(sum < 0) {
sum += MOD;
}
sum = (sum + res) % MOD;
res = (res * 2) % MOD;
}
cout << (sum) % MOD << endl;
}
return 0;
}
标签:10,12,sum,Codeforces,Round,test,Div,include,array
From: https://blog.csdn.net/2301_80882026/article/details/136961915