forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmububa.cpp
72 lines (54 loc) · 1.32 KB
/
mububa.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> v;
vector<ll> sumr;
vector<ll> suml;
vector<unordered_map<ll,ll>> memo;
ll solve(ll start, ll need) {
if(start >= v.size()) return 0;
if(memo[start].count(need) > 0) {
return memo[start][need];
}
// looking for need + suml[start]
auto it = lower_bound(suml.begin(), suml.end(), need+suml[start]);
if(it == suml.end()) return 0;
ll next = (it - suml.begin());
ll have = suml[next] - suml[start];
ll ans = 0;
for(ll i = next; i < v.size(); i++) {
if((sumr[i] / have) < ans) {
break;
}
ans = max(ans, 1+solve(i, have));
have += v[i];
}
if(next == v.size()) {
if(have >= need) ans = max(ans,(ll)1);
}
memo[start][need] = ans;
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll n;
cin >> n;
memo.resize(n);
for(auto& i : memo) {
i.reserve(3000);
}
v.resize(n);
for(auto& i : v) cin >> i;
sumr.resize(n);
sumr[n-1] = 0;
for(ll i = n-2; i >= 0; i--) {
sumr[i] = sumr[i+1] + v[i];
}
suml.resize(n+1);
suml[0] = 0;
for(ll i = 0; i < n; i++) {
suml[i+1] = suml[i] + v[i];
}
cout << solve(0, 1) << endl;
}