-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmincov.cpp
61 lines (46 loc) · 1.13 KB
/
mincov.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
const int INF = 1e9;
void solve() {
ll n;
cin >> n;
vi a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
vector<vi> dp(n + 1, vi(2e3 + 1, INF));
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= 2e3; j++) {
if(dp[i][j] == INF) continue;
ll l = j;
ll r = dp[i][j];
// move left
ll newl = l - a[i];
newl = max(newl, 0LL);
dp[i + 1][newl] = min(dp[i + 1][newl], r + a[i]);
// move right
ll newr = r - a[i];
newr = max(newr, 0LL);
if (l + a[i] <= 2e3) dp[i + 1][l + a[i]] = min(dp[i + 1][l + a[i]], newr);
}
}
ll best = INF;
for (int i = 0; i <= 2e3; i++) {
best = min(best, dp[n][i] + i);
}
cout << best << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while(t--) solve();
return 0;
}