-
Notifications
You must be signed in to change notification settings - Fork 0
/
maximum_sum.cpp
67 lines (58 loc) · 1.28 KB
/
maximum_sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<bits/stdc++.h>
typedef long long ll;
const int P = 1e9+7;
using namespace std;
void solve() {
ll n, k;
cin >> n >> k;
vector<ll> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
ll S = 0, sum = 0;
ll cur = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
cur += a[i];
cur = max(cur, 0ll);
S = max(S, cur);
}
sum = (sum % P + P) % P;
S = S % P;
int t = 1;
for (int i = 0; i < k; i++) {
t = t * 2 % P;
}
int ans = (sum + S * t - S + P) % P;
cout << ans << '\n';
}
// understand modulo properties
void solve2(vector<ll>arr,ll k){
ll max_sum = 0;
ll sum =0,cur =0;
for(int i= 0;i<arr.size();i++){
sum+=arr[i];
cur+=arr[i];
cur = max(cur ,0ll);
max_sum = max(max_sum,cur);
}
sum = (sum % P + P )% P;
ll t = pow(2,k) - 1;
max_sum = max_sum % P;
t = ((sum + (t*max_sum)) % P + P) % P;
cout<<t<<endl;
}
int main(){
int t;
cin>>t;
while(t--){
// ll s,k;
// cin>>s>>k;
// vector<ll>arr(s);
// for(int i = 0;i<s;i++)cin>>arr[i];
// solve2(arr,k);
solve();
}
}
/*
const int P = 1e9 + 7;
*/