malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   struct Q {     int vs, i;   };   const int N = 4e5 + 5, P = 400, D = 26;   int to[N][D], tos[N][D], ver[N], ans[N], ct[N], ancv[N], ancc[N], p[N * 2];   int cv = 1, cvs = 1;   vector<Q> qr[N];   vector<int> sum[N];   int go(int v, int c) {     if (to[v][c] == -1) {       ancv[cv] = v;       ancc[cv] = c;       to[v][c] = cv++;     }     return to[v][c];   }   void dfs(int node) {     for (int i = 0, v = node, vs = 0; v > 0 && i < P; i++) {       vs = tos[vs][ancc[v]];       if (vs == -1) break;       ct[vs]++;       v = ancv[v];     }     for (Q q : qr[node]) {       ans[q.i] = ct[q.vs];     }     for (int i = 0; i < D; i++) {       if (to[node][i] != -1) {         dfs(to[node][i]);       }     }     for (int i = 0, v = node, vs = 0; v > 0 && i < P; i++) {       vs = tos[vs][ancc[v]];       if (vs == -1) break;       ct[vs]--;       v = ancv[v];     }   }   int calc(string &s, int sz) {     int ans = 0;     fill(p, p + s.size(), 0);     for (int i = 1; i < s.size(); i++) {       int x = p[i - 1];       while (x > 0 && s[x] != s[i]) {         x = p[x - 1];       }       if (s[x] == s[i]) x++;       p[i] = x;       if (x == sz) ans++;     }     return ans;   }   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n;     memset(to, -1, sizeof(to));     memset(tos, -1, sizeof(tos));     for (int i = 0; i < n; i++) {       int t;       cin >> t;       if (t == 1) {         char c;         cin >> c;         ver[i] = go(0, c -  a );       } else {         int j;         char c;         cin >> j >> c;         ver[i] = go(ver[j - 1], c -  a );       }     }     cin >> m;     for (int i = 0; i < m; i++) {       int j;       string t;       cin >> j >> t;       j--;       if (t.size() < P) {         reverse(t.begin(), t.end());         int v = 0;         for (char c : t) {           c -=  a ;           if (tos[v][c] == -1) {             tos[v][c] = cvs++;           }           v = tos[v][c];         }         qr[ver[j]].push_back({v, i});       } else {         string s;         int v = ver[j];         while (v > 0) {           s.push_back(ancc[v] +  a );           v = ancv[v];         }         reverse(s.begin(), s.end());         s = t +  #  + s;         ans[i] = calc(s, t.size());       }     }     dfs(0);     for (int i = 0; i < m; i++) {       cout << ans[i] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using LL = long long;   using ll = long long;   using pii = pair<int, int>;   const int N = 2123456;   const int INF = int(1e9);   LL x;   LL C[25][25];   int main() {     for (int i = 0; i <= 20; ++i) {       C[i][0] = 1;       for (int j = 1; j <= i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1];     }     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int T;     cin >> T;     while (T--) {       cin >> x;       vector<int> cnt(25);       int n = 0;       for (int i = 2;; ++i) {         if (x == 0) break;         ++n;         ++cnt[x % i];         x /= i;       }       LL ans = 1;       int alr = 0;       for (int i = n; i >= 0; --i) {         if (cnt[i] == 0) continue;         ans *= C[min(n - i + 1, n) - alr][cnt[i]];         alr += cnt[i];       }       if (cnt[0]) {         LL tot = 1;         alr = 1;         for (int i = n; i >= 1; --i) {           if (cnt[i] == 0) continue;           int cur = min(n - i + 1, n) - alr;           if (cur < cnt[i]) {             tot = 0;             break;           }           tot *= C[cur][cnt[i]];           alr += cnt[i];         }         ans -= tot;       }       cout << ans - 1 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int x[33333];   int C[33333];   int BB[33333];   int seen[33333];   int seena[33333];   int main() {     scanf( %d , &n);     int cc = 0;     for (int i = 0; i < 2 * n; i++) {       scanf( %d , &x[i]);       C[x[i]]++;       BB[x[i]]++;     }     int ca = 0, cb = 0;     vector<int> res(2 * n, 0);     for (int i = 0; i < 2 * n; i++) {       if (C[x[i]] == 1) {         cc++;         if (cc % 2) {           res[i] = 1;           ca++;         } else {           res[i] = 2;           cb++;         }       } else {         if (seena[x[i]] == 0) {           res[i] = 1;           ca++;         } else if (seena[x[i]] == 1) {           res[i] = 2;           cb++;         }         seena[x[i]]++;       }     }     for (int i = 0; i < 2 * n; i++) {       if (res[i] == 0) {         if (ca < n) {           res[i] = 1;           ca++;         } else           res[i] = 2;       }     }     int can = 0;     vector<int> A, B;     for (int i = 0; i < 2 * n; i++) {       if (res[i] == 1)         A.push_back(x[i]);       else         B.push_back(x[i]);     }     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) {         int nn = A[i] * 100 + B[j];         if (!seen[nn]) can++;         seen[nn] = 1;       }     printf( %d n , can);     printf( %d , res[0]);     for (int i = 1; i < 2 * n; i++) {       printf(  %d , res[i]);     }     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 5000 << 2;   int times[5001], cnt[5001], n, a[5001];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) scanf( %d , a + i);     for (int i = n - 1; i >= 0; i--) {       memset(cnt, 0, sizeof(cnt));       int res = a[i];       times[res]++;       cnt[res]++;       for (int j = i - 1; j >= 0; j--) {         cnt[a[j]]++;         if (cnt[a[j]] > cnt[res] || cnt[a[j]] == cnt[res] && a[j] < res)           res = a[j];         times[res]++;       }     }     for (int i = 1; i <= n; i++) printf( %d%c , times[i], (i < n ?     :   n ));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 5e5 + 5;   long long n, k;   long long ans, a, b;   char s[maxn], t[maxn];   int main() {     cin >> n >> k;     scanf( %s%s , s + 1, t + 1);     for (int i = 1; i <= n; ++i) {       a = a << 1 | (s[i] -  a );       b = b << 1 | (t[i] -  a );       if (b - a >= k) {         ans += (n - i + 1) * k;         break;       }       ans += (b - a + 1);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long s, xo;     cin >> s >> xo;     if ((s - xo) % 2 != 0 || (s < xo)) {       cout << 0 << endl;       return 0;     }     if ((((s - xo) / 2) & xo) != 0) {       cout << 0 << endl;       return 0;     }     if (xo == 0) {       cout << 1 << endl;       return 0;     }     long long f = 1;     long long ans = 0;     long long nx = xo;     while (xo != 0) {       if ((f & xo) != 0) {         xo -= f;         ans++;       }       f = f * 2;     }     ans = pow(2, ans);     if (s == nx) ans -= 2;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 999999999;   const double EPSILON = 0.00000001;   const long long MOD = 1000000007;   bool used[200005];   struct interval {     int first, second;     int idx;   };   bool operator<(const interval &self, const interval &other) {     return self.first < other.first;   }   int main() {     int x, n;     cin >> x >> n;     vector<interval> intervals;     for (int i = 0; i < n; i++) {       int a, l;       scanf( %d %d , &a, &l);       intervals.push_back((interval){a, a + l - 1, i + 1});     }     memset(used, false, sizeof(used));     sort(intervals.begin(), intervals.end());     int i = 0, prev = 0;     int end = intervals[0].first - 1;     int best = 0;     int cnt = 0;     while (i < n) {       while (i < n and intervals[i].first <= end + 1) {         if (intervals[i].second > intervals[best].second) {           best = i;         }         i++;       }       if (used[best]) {         if (i < n) end = intervals[i].first - 1;       } else {         end = intervals[best].second;         used[best] = true;         cnt++;       }     }     cout << n - cnt << endl;     bool first = true;     for (int i = 0; i < n; i++) {       if (not used[i]) {         if (not first) putchar(   );         printf( %d , intervals[i].idx);         first = false;       }     }     if (cnt != n) cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     int sum = 0, d = 0, diff = 0;     int start = 97;     cin >> s;     for (int i = 0; i < s.size(); i++) {       diff = abs((int)s[i] - start);       if (diff > 13) {         d = 26 - diff;         sum += d;       } else {         sum += diff;       }       start = (int)s[i];     }     cout << sum;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[1502][26];   string s;   int n;   int len(int w, char c) {     if (dp[w][c -  a ] != -1) {       return dp[w][c -  a ];     }     int l = 0;     int m = 1;     int sum = 0;     int tail = 0;     for (int i = 0; i < n; i++) {       if (s[i] == c) {         l++;       } else {         if (sum < w) {           sum++;           l++;         } else {           while (s[tail] == c && tail < i) {             tail++;           }           tail++;           l = i - tail + 1;         }       }       if (l > m) {         m = l;       }     }     return dp[w][c -  a ] = m;   }   int main() {     cin >> n;     int q;     cin >> s;     for (int i = 0; i < 26; i++) {       for (int j = 0; j <= n; j++) {         dp[j][i] = -1;       }     }     int w;     char c;     cin >> q;     for (int i = 0; i < q; i++) {       scanf( %d %c , &w, &c);       printf( %d n , len(w, c));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   ostream &operator<<(ostream &o, vector<T> v) {     if (v.size() > 0) o << v[0];     for (unsigned i = 1; i < v.size(); i++) o <<     << v[i];     return o << endl;   }   template <typename U, typename V>   ostream &operator<<(ostream &o, pair<U, V> p) {     return o <<  (  << p.first <<  ,   << p.second <<  )  ;   }   template <typename T>   istream &operator>>(istream &in, vector<T> &v) {     for (unsigned i = 0; i < v.size(); i++) in >> v[i];     return in;   }   int main() {     ios_base::sync_with_stdio(false);     int n, m, p;     cin >> n >> m >> p;     string s;     cin >> s;     list<char> seq((s).begin(), (s).end());     string c;     cin >> c;     auto it = seq.begin();     for (int(i) = 0; (i) < (int)(p - 1); ++(i)) it++;     for (int(i) = 0; (i) < (int)(m); ++(i)) {       if (c[i] ==  R ) {         it++;       } else if (c[i] ==  L )         it--;       else {         char b = *it;         if (b ==  ( ) {           int cnt = 1;           auto beg = it;           it++;           while (cnt > 0) {             b = *it;             if (b ==  ) )               cnt--;             else               cnt++;             it++;           }           seq.erase(beg, it);           if (it == seq.end()) it--;         } else {           auto oit = it;           oit++;           int cnt = 1;           while (cnt > 0) {             it--;             b = *it;             if (b ==  ) )               cnt++;             else               cnt--;           }           seq.erase(it, oit);           it = oit;           if (it == seq.end()) it--;         }       }     }     string res;     for (char c : seq) res += c;     cout << res << endl;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d, case1, case2, case3, case4;     cin >> a >> b >> c >> d;     case1 = abs(pow(a, 2) + pow(b, 2) - pow(c, 2)) - 2 * a * b;     case2 = abs(pow(a, 2) + pow(b, 2) - pow(d, 2)) - 2 * b * a;     case3 = abs(pow(a, 2) + pow(c, 2) - pow(d, 2)) - 2 * a * c;     case4 = abs(pow(b, 2) + pow(c, 2) - pow(d, 2)) - 2 * b * c;     if ((case1 < 0) || (case2 < 0) || (case3 < 0) || (case4 < 0)) {       cout <<  TRIANGLE ;     } else if ((case1 == 0) || (case2 == 0) || (case3 == 0) || (case4 == 0)) {       cout <<  SEGMENT ;     } else       cout <<  IMPOSSIBLE ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   int cnt[10000001] = {};   int par[10000001], ra[10000001];   void dsuInit() {     for (int i = 0; i < (int)(10000001); ++i) par[i] = i, ra[i] = cnt[i];   }   int dsuParent(int v) {     if (v == par[v]) return v;     return par[v] = dsuParent(par[v]);   }   void dsuMerge(int u, int v) {     u = dsuParent(u);     v = dsuParent(v);     if (u == v) return;     if (ra[u] < ra[v]) swap(u, v);     par[v] = u;     ra[u] += ra[v];   }   int gcd(int a, int b) {     while (b) a %= b, swap(a, b);     return a;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < (int)(n); ++i) {       int x;       scanf( %d , &x);       ++cnt[x];     }     dsuInit();     for (int p = 1; p <= 3200; ++p) {       for (int q = p + 1; 2 * p * q <= 10000000; q += 2) {         if (gcd(p, q) != 1) continue;         long long x = (long long)q * q - p * p;         long long y = 2 * p * q;         long long z = p * p + (long long)q * q;         if (x <= 10000000 && y <= 10000000 && cnt[x] && cnt[y]) dsuMerge(x, y);         if (x <= 10000000 && z <= 10000000 && cnt[x] && cnt[z]) dsuMerge(x, z);         if (y <= 10000000 && z <= 10000000 && cnt[y] && cnt[z]) dsuMerge(y, z);       }     }     int ans = 0;     for (int i = 0; i < (int)(10000001); ++i)       if (cnt[i] && dsuParent(i) == i) {         if (ra[i] == cnt[i])           ans += cnt[i];         else           ++ans;       }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   map<long long, long long> pos, icon;   void read() {     cin >> n >> m >> k;     for (long long i = 1; i <= n; i++) {       long long temp;       cin >> temp;       pos[temp] = i;       icon[i] = temp;     }   }   void solve() {     long long ans = 0;     for (long long i = 1; i <= m; i++) {       long long queri;       cin >> queri;       ans += -(pos[queri] % k == 0) + pos[queri] / k;       long long temp = icon[pos[queri]];       if (pos[queri] != 1)         icon[pos[queri]] = icon[pos[queri] - 1], icon[pos[queri] - 1] = temp,         pos[icon[pos[queri]]] = pos[queri],         pos[icon[pos[queri] - 1]] = pos[queri] - 1;     }     cout << ans + m;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     read(), solve();   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     char g[s.length()];     int j = 0;     for (int i = 2; i < s.length(); i++) {       if (s[i] == s[i - 1] && s[i - 1] == s[i - 2] && s[i] !=  0 ) {         s[i - 2] =  0 ;         char cc = s[i];         while (i + 1 < s.length() && s[i + 1] == cc) {           s[i + 1] =  0 ;           i++;         }       }     }     for (int i = 0; i < s.length(); i++) {       if (s[i] !=  0 ) g[j] = s[i], j++;     }     for (int i = 3; i < j; i++) {       if (g[i] == g[i - 1] && g[i - 2] == g[i - 3]) {         g[i] =  0 ;       }     }     for (int i = 0; i < j; i++)       if (g[i] !=  0 ) cout << g[i];   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, j;     cin >> n;     if (n == 1) {       cout << 0 << endl;       return 0;     }     if (n == 2) {       cout <<  1 n1 2 n ;       return 0;     }     cout << (n / 2) * (n / 2 + n % 2) << endl;     for (i = 1; i <= n / 2; i++)       for (j = n / 2 + 1; j <= n; j++) cout << i <<     << j << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long> translate;   long long get(long long x) {     return lower_bound(translate.begin(), translate.end(), x) - translate.begin();   }   int main() {     ios_base::sync_with_stdio(0);     long long n;     cin >> n;     if (n == 1) {       cout << 1 << endl;       return 0;     }     long long a[n];     for (long long i = 0; i < n; i++) {       cin >> a[i];       translate.push_back(a[i]);     }     translate.push_back(0);     sort(translate.begin(), translate.end());     translate.resize(unique(translate.begin(), translate.end()) -                      translate.begin());     long long b[n];     for (long long i = 0; i < n; i++) {       b[i] = get(a[i]);     }     long long d[n][translate.size()];     fill(d[0], d[n - 1] + translate.size(), INT_MAX);     for (long long i = 0; i < n - 1; i++) {       long long benefit = 0;       for (long long j = 0; j <= b[i]; j++) {         if (i == 0) {           d[i][j] = translate[j] + (int)(j < b[i]);         }         if (d[i][benefit] + translate[j] - translate[benefit] >= d[i][j]) {           benefit = j;         }         long long temp = j;         if (j > b[i + 1]) temp = b[i + 1];         d[i + 1][temp] =             min(d[i + 1][temp],                 d[i][benefit] + max(translate[temp] - translate[benefit], 0ll) +                     (int)(temp != b[i + 1]));       }       if (b[i + 1] > b[i]) {         for (long long j = b[i] + 1; j <= b[i + 1]; j++) {           long long znach = d[i][benefit] + translate[j] - translate[benefit] +                             (int)(j != b[i + 1]);           d[i + 1][j] = min(d[i + 1][j], znach);         }       }     }     cout << *min_element(d[n - 1], d[n - 1] + translate.size());   }
#include <bits/stdc++.h>   using namespace std;   const string st =  AHIMOTUVWXY ;   string s;   int main() {     ios::sync_with_stdio(0);     cin >> s;     int n = s.size();     for (int i = 0; i <= n - 1; ++i)       if (st.find(s[i], 0) == -1) {         cout <<  NO ;         return 0;       }     string s1 = s;     reverse(s1.begin(), s1.end());     if (s1 == s)       cout <<  YES ;     else       cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   int main() {     char s[10000];     int a, b, i;     scanf( %d n , &b);     for (i = 0; i < b; i++) {       gets(s);       a = strlen(s);       if (a > 10)         printf( %c%d%c n , s[0], a - 2, s[a - 1]);       else         printf( %s n , s);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool forms(long long a, long long b, long long c) {     if ((a * a) + (b * b) == (c * c)) return true;     return false;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t, n;     cin >> t;     while (t--) {       cin >> n;       cout << n / 2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   using namespace std;   const int inf = 0x3f3f3f3f;   inline long long read() {     register long long x = 0, f = 1;     register char c = getchar();     for (; !isdigit(c); c = getchar())       if (c ==  - ) f = -1;     for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);     return x * f;   }   void write(long long x) {     if (x < 0) x = -x, putchar( - );     if (x >= 10) write(x / 10);     putchar(x % 10 +  0 );   }   void writeln(long long x) {     write(x);     puts(  );   }   const int N = 505050;   struct Ans {     int a, b, c, d;   } ans[N];   int n, x, y, cnt, f[N], fa[2][N];   vector<int> e[2][N];   int get(int x) { return f[x] == x ? x : f[x] = get(f[x]); }   void dfs(int id, int x, int f) {     for (int i = 0; i < e[id][x].size(); i++)       if (e[id][x][i] != f) {         fa[id][e[id][x][i]] = x;         dfs(id, e[id][x][i], x);       }   }   void dfs2(int x) {     for (int i = 0; i < e[0][x].size(); i++)       if (e[0][x][i] != fa[0][x]) {         int u = e[0][x][i];         dfs2(u);         if (u != fa[1][x] && x != fa[1][u])           ans[++cnt] = (Ans){u, x, get(u), fa[1][get(u)]};       }   }   int main() {     scanf( %d , &n);     for (int id = 0; id < 2; id++)       for (int i = 1; i < n; i++) {         scanf( %d%d , &x, &y);         e[id][x].push_back(y);         e[id][y].push_back(x);       }     dfs(0, 1, 0);     dfs(1, 1, 0);     f[1] = 1;     for (int i = 2; i <= n; i++) {       int u = fa[1][i];       if (fa[0][i] == u || fa[0][u] == i)         f[i] = u;       else         f[i] = i;     }     dfs2(1);     printf( %d n , cnt);     for (int i = 1; i <= cnt; i++)       printf( %d %d %d %d n , ans[i].a, ans[i].b, ans[i].c, ans[i].d);   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   int chkmax(T& a, T b) {     if (b > a) {       a = b;       return 1;     }     return 0;   }   template <class T>   int chkmin(T& a, T b) {     if (b < a) {       a = b;       return 1;     }     return 0;   }   template <class iterator>   void output(iterator begin, iterator end, ostream& out = cerr) {     while (begin != end) {       out << (*begin) <<    ;       begin++;     }     out << endl;   }   template <class T>   void output(T x, ostream& out = cerr) {     output(x.begin(), x.end(), out);   }   void fast_io() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);   }   const int mx = 1e6 + 228;   int n;   vector<vector<int> > g;   long long t[mx], h[mx];   void read() {     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> t[i];     }     for (int i = 0; i < n; ++i) {       cin >> h[i];     }     g.resize(n);     for (int i = 0; i < n - 1; ++i) {       int u, v;       cin >> u >> v;       u--;       v--;       g[u].push_back(v);       g[v].push_back(u);     }   }   const long long INF = 1e18;   long long dp[mx][2][2];   long long sum_dp, dist0;   int ch_eq, e_to_v, e_from_v;   vector<long long> dist, delta;   void calc_stuff(int v, int pv) {     e_to_v = 0;     e_from_v = 0;     sum_dp = 0;     ch_eq = 0;     dist.clear();     delta.clear();     dist0 = 0;     for (auto v1 : g[v]) {       if (pv != v1) {         if (h[v1] > h[v]) {           e_from_v++;           sum_dp += dp[v1][1][0];         }         if (h[v1] < h[v]) {           e_to_v++;           sum_dp += dp[v1][0][1];         }         if (h[v1] == h[v]) {           ch_eq++;           dist0 += dp[v1][0][1];           delta.push_back(dp[v1][1][0] - dp[v1][0][1]);         }       }     }     sort(delta.begin(), delta.end());     dist.push_back(dist0);     for (int i = 0; i < ch_eq; ++i) {       dist0 += delta[i];       dist.push_back(dist0);     }   }   void calc_dp(int v, int down_e, int up_e) {     int tot_to_v = e_to_v + down_e;     int tot_from_v = e_from_v + up_e;     dp[v][down_e][up_e] = INF;     for (int i = 0; i <= ch_eq; ++i) {       chkmin(dp[v][down_e][up_e],              sum_dp + dist[i] +                  (long long)t[v] *                      (long long)max(tot_from_v + i, tot_to_v + ch_eq - i));     }   }   void calc_dp(int v, int pv) {     for (auto v1 : g[v]) {       if (v1 != pv) {         calc_dp(v1, v);       }     }     calc_stuff(v, pv);     if (v == pv) {       calc_dp(v, 0, 0);     } else {       if (h[pv] <= h[v]) {         calc_dp(v, 1, 0);       } else {         dp[v][1][0] = INF;       }       if (h[pv] >= h[v]) {         calc_dp(v, 0, 1);       } else {         dp[v][0][1] = INF;       }     }   }   signed main() {     fast_io();     read();     calc_dp(0, 0);     cout << dp[0][0][0] <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   void ans(int n, int p) {     if (n == 5) {       assert(p == 0);       for (int i = int(1); i <= int(n); i++) {         printf( %d %d n , i, i + 1 > n ? i + 1 - n : i + 1);         printf( %d %d n , i, i + 2 > n ? i + 2 - n : i + 2);       }       return;     }     int prevMaxP = (n - 1) * (n - 2) / 2 - 2 * (n - 1);     if (prevMaxP < p) {       for (int i = int(0); i < int(2 + p - prevMaxP); i++)         printf( %d %d n , 1 + i, n);       ans(n - 1, prevMaxP);     } else {       for (int i = int(0); i < int(2); i++) printf( %d %d n , 1 + i, n);       ans(n - 1, p);     }   }   int main() {     ios_base::sync_with_stdio(false);     int t;     cin >> t;     for (int i = int(0); i < int(t); i++) {       int n, p;       cin >> n >> p;       ans(n, p);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, f[1000005];   long long ans, las;   int gets() {     int v = 0;     char ch;     bool f = 0;     while (!isdigit(ch = getchar()))       if (ch ==  - ) f = 1;     v = ch - 48;     while (isdigit(ch = getchar())) v = (v << 1) + (v << 3) + ch - 48;     return f ? -v : v;   }   int main() {     n = gets();     for (int i = 1; i <= n; i++) {       f[i] = gets();       long long tmp = min(las, (long long)f[i] / 2);       las -= tmp;       f[i] -= tmp * 2;       ans += tmp;       tmp = f[i] / 3;       ans += tmp;       las += f[i] % 3;     }     cout << ans << endl;   }