forked from Sahiljawale/CodeChef
/
Maximum_Subarray.cpp
75 lines (61 loc) · 1.19 KB
/
Maximum_Subarray.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
68
69
70
71
72
73
74
75
#include<bits/stdc++.h>
typedef long long ll;
#define mod 1000000007
#define inf 1e9
#define rep(i,n) for(int i=0;i<n;i++)
#define w(t) ll tt;cin>>tt;while(tt--)
#define pb push_back
#define endl "\n"
#define vll vector<ll>
#define pi pair<ll,ll>
#define all(arr) arr.begin(),arr.end()
using namespace std;
void init_code(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
void solve(){
int n; cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
int m; cin>>m;
vector<int> b(m);
for(int i=0;i<m;i++) cin>>b[i];
vector<int> tempa= a;
for(int i=0;i<m;i++){
if(b[i]>0){
a.push_back(b[i]);
tempa.insert(tempa.begin(),b[i]);
}
}
long long sum=0, maxsum=INT_MIN;
for(int i=0;i<a.size();i++){
sum+=a[i];
maxsum=max(maxsum,sum);
if(sum<0){
sum=0;
}
}
sum=0;
long long maxi=INT_MIN;
for(int i=0;i<tempa.size();i++){
sum+=tempa[i];
maxi=max(maxi,sum);
if(sum<0){
sum=0;
}
}
cout<<max(maxi,maxsum)<<endl;
}
int main(){
init_code();
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;cin>>t;
while(t--){
solve();
}
}