-
Notifications
You must be signed in to change notification settings - Fork 0
/
INOI1602.cpp
236 lines (180 loc) · 4.51 KB
/
INOI1602.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Krishnaya vaasudevaya
// Devaki nandanayacha
// Nanda gopakumaraya
// Govindaya namoh namaha
// Jay ganeshji, Jay Guruji, Jay Ram-bhakt Hanumanji
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define ff first
#define ss second
#define pb emplace_back
#define eb emplace
// Competion only
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned ll;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
const int inf = 1e9 + 7;
const ll llinf = 1e18 + 7;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll fastPow(ll a, ll b)
{
ll res = 1;
while(b) {
if(b & 1)
res *= a;
a *= a, b >>= 1;
}
return res;
}
ll fastPow(ll a, ll b, ll m)
{
ll res = 1;
a %= m;
while(b) {
if(b & 1)
(res *= a) %= m;
(a *= a) %= m, b >>= 1;
}
return res;
}
ll modinv(ll x, ll m)
{
return fastPow(x, m - 2, m);
}
ll moddiv(ll numerator, ll denominator, ll m) {
return numerator * modinv(denominator, m);
}
template<typename T>
constexpr ostream &operator<<(ostream &stream, const vector<T> &other) {
stream << '[';
size_t n = other.size();
if(n > 0) {
for(size_t i = 0; i < n - 1; i++)
stream << other[i] << ", ";
stream << other[n - 1];
}
return stream << ']';
}
template<typename T>
constexpr ostream &operator<<(ostream &stream, const set<T> &other) {
stream << '{';
size_t n = other.size();
if(n > 0) {
auto it = other.begin();
for(size_t i = 0; i < n-1; i++)
stream << *it << ", ", ++it;
stream << *it;
}
return stream << '}';
}
template<typename T>
constexpr ostream &operator<<(ostream &stream, const multiset<T> &other) {
stream << '{';
size_t n = other.size();
if(n > 0) {
auto it = other.begin();
for(size_t i = 0; i < n-1; i++)
stream << *it << ", ", ++it;
stream << *it;
}
return stream << '}';
}
template<typename T>
constexpr ostream &operator<<(ostream &stream, const ordered_set<T> &other) {
stream << '{';
size_t n = other.size();
if(n > 0) {
auto it = other.begin();
for(size_t i = 0; i < n-1; i++)
stream << *it << ", ", ++it;
stream << *it;
}
return stream << '}';
}
template<typename T1, typename T2>
constexpr ostream &operator<<(ostream &stream, const map<T1, T2> &other) {
stream << '{';
size_t n = other.size();
if(n > 0) {
auto it = other.begin();
for(size_t i = 0; i < n-1; i++)
stream << (*it).first << ':' << (*it).second << ", ", ++it;
stream << (*it).first << ':' << (*it).second;
}
return stream << '}';
}
template<typename T1, typename T2>
constexpr ostream &operator<<(ostream &stream, const multimap<T1, T2> &other) {
stream << '{';
size_t n = other.size();
if(n > 0) {
auto it = other.begin();
for(size_t i = 0; i < n-1; i++)
stream << (*it).first << ':' << (*it).second << ", ", ++it;
stream << (*it).first << ':' << (*it).second;
}
return stream << '}';
}
template<typename T1, typename T2>
constexpr ostream &operator<<(ostream &stream, const unordered_map<T1, T2> &other) {
stream << '{';
size_t n = other.size();
if(n > 0) {
auto it = other.begin();
for(size_t i = 0; i < n-1; i++)
stream << (*it).first << ':' << (*it).second << ", ", ++it;
stream << (*it).first << ':' << (*it).second;
}
return stream << '}';
}
template<typename T1, typename T2>
constexpr ostream &operator<<(ostream &stream, const pair<T1, T2> &other) {
return stream << '(' << other.first << ", " << other.second << ')';
}
template<typename T>
istream &operator>>(istream &stream, vector<T> &other) {
for(auto &e : other)
stream >> e;
return stream;
}
template<typename T1, typename T2>
istream &operator>>(istream &stream, pair<T1, T2> &other) {
stream >> other.first >> other.second;
return stream;
}
vi a, b;
int n, k;
vector<vi> dp;
int solve(int i, int j) {
if(i >= j) // for length <= 0, no way to match brackets
return 0;
if(dp[i][j] == -inf) {
int res = 0;
for(int e = i+1; e <= j; e++)
if(b[i] == b[e] - k)
res = max(res, a[i] + solve(i + 1, e - 1) + a[e] + solve(e + 1, j));
dp[i][j] = max(solve(i + 1, j), res);
}
return dp[i][j];
}
int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k, a.resize(n), b.resize(n);
cin >> a >> b;
dp = vector<vi>(n, vi(n, -inf));
cout << solve(0, n-1) << '\n';
return 0;
}