#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define RFOR(i,p,n) for(int i=(p);i>=(n);--i)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define gc getchar_unlocked
#define pc putchar_unlocked
#define pn putchar_unlocked('\n')
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef V<int> vi;
typedef V<ll> vll;
typedef const int ci;
typedef const ll cll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf = 2.1e9;
cll infll = 4.5e18;
int I(){
int z;
int c = gc();
while(c < '0' || c > '9') c = gc();
for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0';
return z;
}
void O(ll a){
static int tab[20];
int it = 0;
do{
tab[it++] = int('0'+a%10);
} while(a /= 10);
while(it) pc(tab[--it]);
}
ll sign(cll a){
return (a>0)-(a<0);
}
void answer(){
auto wczytaj = [](){
ci n = I();
int znak = gc()=='(' ? 1 : -1;
vll ret;
REP(_, n){
ci a = I();
ret.eb(a*znak);
znak *= -1;
}
return ret;
};
vll a = wczytaj();
ci n = ssize(a);
vll b = wczytaj();
ci m = ssize(b);
vll a_pref(n+1, 0);
REP(i, n) a_pref[i+1] = a_pref[i]+a[i];
ll wyn = 0;
REP(poc, m) FOR(ilepoc, 1, abs(b[poc])){
ll teraz = 0;
vll dp = a_pref;
FOR(i, 1, n) chmin(dp[i], dp[i-1]);
auto dod = [&](cll wart){
teraz += wart;
FOR(i, 0, n) chmin(dp[i], a_pref[i]+teraz);
FOR(i, 1, n) chmax(dp[i], min(dp[i-1], a_pref[i]+teraz));
};
vll stare_dp;
ll stare_teraz;
auto rozwaz = [&](cll wart){
ll ile = -sign(wart)*(a_pref[n]+teraz);
if(1 <= ile && ile <= abs(wart)){
stare_dp = dp;
stare_teraz = teraz;
dod(sign(wart)*ile);
if(teraz+a_pref[n] == 0) wyn += dp[n]>=0;
dp = stare_dp;
teraz = stare_teraz;
}
dod(wart);
};
rozwaz(sign(b[poc])*ilepoc);
FOR(kon, poc+1, m-1) rozwaz(b[kon]);
}
O(wyn), pn;
}
int main(){
int tt = 1;
while(tt--) answer();
}
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 | #include <bits/stdc++.h> #define FOR(i,p,k) for(int i=(p);i<=(k);++i) #define REP(i,n) FOR(i,0,(n)-1) #define RFOR(i,p,n) for(int i=(p);i>=(n);--i) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define ssize(x) int((x).size()) #define fi first #define se second #define V vector #define pb push_back #define eb emplace_back #define C const #define gc getchar_unlocked #define pc putchar_unlocked #define pn putchar_unlocked('\n') using namespace std; typedef long long ll; typedef __int128_t lll; typedef V<int> vi; typedef V<ll> vll; typedef const int ci; typedef const ll cll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<ll, int> pli; typedef pair<int, ll> pil; void chmin(auto &a, auto b){a=min(a,b);} void chmax(auto &a, auto b){a=max(a,b);} ci inf = 2.1e9; cll infll = 4.5e18; int I(){ int z; int c = gc(); while(c < '0' || c > '9') c = gc(); for(z = 0; c >= '0' && c <= '9'; c = gc()) z = 10*z+c-'0'; return z; } void O(ll a){ static int tab[20]; int it = 0; do{ tab[it++] = int('0'+a%10); } while(a /= 10); while(it) pc(tab[--it]); } ll sign(cll a){ return (a>0)-(a<0); } void answer(){ auto wczytaj = [](){ ci n = I(); int znak = gc()=='(' ? 1 : -1; vll ret; REP(_, n){ ci a = I(); ret.eb(a*znak); znak *= -1; } return ret; }; vll a = wczytaj(); ci n = ssize(a); vll b = wczytaj(); ci m = ssize(b); vll a_pref(n+1, 0); REP(i, n) a_pref[i+1] = a_pref[i]+a[i]; ll wyn = 0; REP(poc, m) FOR(ilepoc, 1, abs(b[poc])){ ll teraz = 0; vll dp = a_pref; FOR(i, 1, n) chmin(dp[i], dp[i-1]); auto dod = [&](cll wart){ teraz += wart; FOR(i, 0, n) chmin(dp[i], a_pref[i]+teraz); FOR(i, 1, n) chmax(dp[i], min(dp[i-1], a_pref[i]+teraz)); }; vll stare_dp; ll stare_teraz; auto rozwaz = [&](cll wart){ ll ile = -sign(wart)*(a_pref[n]+teraz); if(1 <= ile && ile <= abs(wart)){ stare_dp = dp; stare_teraz = teraz; dod(sign(wart)*ile); if(teraz+a_pref[n] == 0) wyn += dp[n]>=0; dp = stare_dp; teraz = stare_teraz; } dod(wart); }; rozwaz(sign(b[poc])*ilepoc); FOR(kon, poc+1, m-1) rozwaz(b[kon]); } O(wyn), pn; } int main(){ int tt = 1; while(tt--) answer(); } |
English