Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) x.begin(), x.end()
- #define rall(x) x.rbegin(), x.rend()
- #define F first
- #define S second
- #define pb push_back
- #define ll long long
- #define ld long double
- #define int long long
- #pragma GCC optimize("Ofast")
- #pragma GCC target("sse3,sse4")
- //#define TASK ""
- using namespace std;
- const int MAXN = 200001;
- const int INF = 1e9;
- ll power(ll x, ll n, ll mod = 1e18 + 7) {
- if (n == 0) return 1ll;
- if (n & 1ll) return power(x, n - 1ll, mod) * x % mod;
- ll tmp = power(x, n >> 1ll, mod);
- return (tmp * tmp) % mod;
- }
- int gcd (int a, int b) {
- if (b == 0) return a;
- return gcd (b, a % b);
- }
- vector<int> getPref(vector<int> &a) {
- vector<int> ans(a.size());
- int sum = 0;
- for (int i = a.size() - 1; i >= 0; i--) {
- if (a[i] == 1) sum++;
- ans[i] = sum;
- }
- return ans;
- }
- vector<int> max_cnt(vector<int> &a, vector<int> &pref) {
- int sum = 0;
- vector<int> ans(count(all(a), 0) + 1);
- if (ans.size() != 0)
- ans[0] = count(all(a), 1);
- for (int i = 0; i < a.size(); i++) {
- if (a[i] == 0) sum++;
- ans[sum] = max(ans[sum], pref[i]);
- }
- return ans;
- }
- signed main() {
- #ifndef LOCAL
- #ifdef TASK
- freopen(TASK".in", "r", stdin);
- freopen(TASK".out", "w", stdout);
- #endif
- #endif
- #ifdef LOCAL
- //freopen("/Users/alekseygolub/Desktop/ะก++/ABS/ABS/input.txt", "r", stdin);
- #endif
- iostream::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- srand(924653523);
- // == SOLUTION == //
- int n;
- cin >> n;
- vector<int> a, b;
- for (int i = 0; i < n; i++) {
- int x;
- cin >> x;
- a.pb(x);
- }
- int m;
- cin >> m;
- for (int i = 0; i < m; i++) {
- int x;
- cin >> x;
- b.pb(x);
- }
- if (n * m == 0) {
- cout << 0 << '\n';
- return 0;
- }
- vector<int> a_pref = getPref(a);
- vector<int> b_pref = getPref(b);
- vector<int> x = max_cnt(a, a_pref);
- vector<int> y = max_cnt(b, b_pref);
- int SZ = min(x.size(), y.size());
- int ans = 0;
- for (int i = 0; i < SZ; i++) {
- ans = max(ans, i + min(x[i], y[i]));
- }
- cout << ans << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement