-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkadanes-twist.cpp
204 lines (176 loc) · 3.82 KB
/
kadanes-twist.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = 1000000007;
#define pii pair<int, int>
#define N 100005
int inf = 1e18;
struct less_than_key
{
bool operator()(const pii &a, const pii &b)
{
return (a.first - a.second) > (b.first - b.second);
}
};
int gcd(int a, int b)
{
if (a < b)
return gcd(b, a);
if (b == 0)
return a;
return gcd(a % b, b);
}
int mult(int a, int b) // O(1)
{
return ((a % mod) * (b % mod)) % mod;
}
int exponent(int a, int b)
{
int ans = 1;
while (b > 0)
{
if (b % 2 == 1)
{
ans = (ans * a) % mod;
}
a = (a * a) % mod;
b /= 2;
}
return ans;
}
int inverse(int a) { return exponent(a, mod - 2); }
int fact[N];
int invFact[N];
void init()
{
fact[0] = 1;
for (int i = 1; i < N; i++)
{
fact[i] = mult(i, fact[i - 1]);
}
invFact[N - 1] = inverse(fact[N - 1]);
for (int i = N - 2; i >= 0; i--)
{
invFact[i] = mult(invFact[i + 1], i + 1);
}
}
int nCr(int n, int r)
{
return mult(fact[n], mult(invFact[n - r], invFact[r]));
}
int add(int a, int b)
{ // O(1)
int ans = (a + b);
if (ans >= mod)
ans -= mod;
if (ans < 0)
ans += mod;
return ans;
}
// int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<int>> dirs;
void pr(vector<int> &v)
{
cout << "Printing vector---\n";
for (auto x : v)
{
cout << x << " ";
}
cout << "\n";
}
int n, m;
vector<int> arr[51];
struct max_sum_value
{
int best = -1;
int prebest;
int suffbest;
int tot;
void build(vector<int>& v)
{
int n = v.size();
tot = 0;
for (int i = 0; i < n; i++) {
tot += v[i];
}
// cout<<"tot = "<<tot<<"\n";
prebest = v[0];
int pref_sum = v[0];
for (int i = 1; i < n; i++)
{
pref_sum += v[i];
prebest = max(prebest, pref_sum);
}
suffbest = v[n - 1];
int suff_sum = v[n - 1];
for (int i = n - 2; i >= 0; i--)
{
suff_sum += v[i];
suffbest = max(suffbest, suff_sum);
}
best = tot;
int last_best = 0;
for (int i = 0; i < n; i++)
{
last_best = max(last_best+v[i], v[i]);
best = max(best, last_best);
}
// cout<<"best from function = "<<best<<"\n";
}
void print() {
printf("tot = %lld, prebest = %lld, suffbest = %lld, best = %lld\n", tot, prebest, suffbest, best);
}
};
// max_sum_value converted[51];
vector<max_sum_value> converted(51);
max_sum_value merge(max_sum_value a, max_sum_value b) {
max_sum_value ans;
ans.best = max({a.best, b.best, a.suffbest + b.prebest});
ans.prebest = max(a.prebest, a.tot+b.prebest);
ans.suffbest = max(b.suffbest, b.tot+a.suffbest);
ans.tot = a.tot+b.tot;
return ans;
}
void solve()
{
cin>>n>>m;
for(int i=1;i<=n;i++) {
int l; cin>>l;
arr[i].clear();
for(int j=0;j<l;j++) {
int x;
cin>>x;
arr[i].push_back(x);
}
converted[i].build(arr[i]);
// converted[i].print();
// printf("best at i=%lld = %lld\n",i,converted[i].tot);
}
int final_pos[m];
for(int j=0;j<m;j++) {
cin>>final_pos[j];
}
max_sum_value final_ans = converted[final_pos[0]];
for(int j=1;j<m;j++) {
final_ans = merge(final_ans, converted[final_pos[j]]);
}
cout<<final_ans.best<<"\n";
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
// init();
while (t--)
{
solve();
}
// for(tt=1;tt<=t;tt++){
// cin>>s;
// cout<<rec(0,1,1)<<"\n";
// }
return 0;
}