malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   struct Q {     int l;     int r;     int num;     Q(int _l, int _r, int _num) : l(_l), r(_r), num(_num) {}   };   int cnt[100100];   int res[100100];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<int> comp = a;     sort(comp.begin(), comp.end());     comp.resize(unique(comp.begin(), comp.end()) - comp.begin());     for (int i = 0; i < n; i++) {       a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();     }     int K = sqrt(n);     int B = n / K + 1;     vector<vector<Q>> blocks(B);     for (int i = 0; i < m; i++) {       int l, r;       cin >> l >> r;       l--;       r--;       blocks[l / K].emplace_back(l, r, i);     }     for (int b = 0; b < B; b++) {       sort(blocks[b].begin(), blocks[b].end(),            [&](Q x, Q y) { return x.r < y.r; });     }     for (int b = 0; b < B; b++) {       if (blocks[b].empty()) continue;       memset(cnt, 0, sizeof(cnt));       int ans = 0;       for (int i = blocks[b][0].l; i <= blocks[b][0].r; i++) {         if (cnt[a[i]] == comp[a[i]]) {           ans--;         }         cnt[a[i]]++;         if (cnt[a[i]] == comp[a[i]]) {           ans++;         }       }       int pl = blocks[b][0].l;       int pr = blocks[b][0].r;       res[blocks[b][0].num] = ans;       for (int q = 1; q < (int)blocks[b].size(); q++) {         for (int i = pr + 1; i <= blocks[b][q].r; i++) {           if (cnt[a[i]] == comp[a[i]]) {             ans--;           }           cnt[a[i]]++;           if (cnt[a[i]] == comp[a[i]]) {             ans++;           }         }         pr = blocks[b][q].r;         for (int i = blocks[b][q].l; i < pl; i++) {           if (cnt[a[i]] == comp[a[i]]) {             ans--;           }           cnt[a[i]]++;           if (cnt[a[i]] == comp[a[i]]) {             ans++;           }         }         for (int i = pl; i < blocks[b][q].l; i++) {           if (cnt[a[i]] == comp[a[i]]) {             ans--;           }           cnt[a[i]]--;           if (cnt[a[i]] == comp[a[i]]) {             ans++;           }         }         pl = blocks[b][q].l;         res[blocks[b][q].num] = ans;       }     }     for (int i = 0; i < m; i++) {       cout << res[i] <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q, ans;   map<int, int> boss[100010];   map<pair<int, int>, int> mp;   vector<pair<int, int> > vt[100010];   int findf(int x, int col) {     if (!boss[col].count(x) || x == boss[col][x]) {       boss[col][x] = x;       return x;     }     return boss[col][x] = findf(boss[col][x], col);   }   void dsu(int x, int y, int col) {     x = findf(x, col);     y = findf(y, col);     if (x != y) {       boss[col][y] = x;     }     return;   }   int main() {     int i, j, a, b, c;     cin >> n >> m;     for (i = 0; i < m; i++) {       cin >> a >> b >> c;       a--, b--, c--;       dsu(a, b, c);     }     for (i = 0; i < m; i++) {       for (map<int, int>::iterator it = boss[i].begin(); it != boss[i].end();            it++) {         a = it->first;         b = it->second;         vt[a].push_back(make_pair(i, b));       }     }     for (i = 0; i < n; i++) {       for (j = 0; j < vt[i].size(); j++) {         vt[i][j].second = findf(i, vt[i][j].first);       }     }     cin >> q;     for (i = 0; i < q; i++) {       cin >> a >> b;       a--, b--;       if (vt[a].size() >= vt[b].size()) {         swap(a, b);       }       if (mp.count(make_pair(a, b))) {         cout << mp[make_pair(a, b)] << endl;         continue;       }       ans = 0;       for (j = 0; j < vt[a].size(); j++) {         if (binary_search(vt[b].begin(), vt[b].end(), vt[a][j])) {           ans++;         }       }       mp[make_pair(a, b)] = ans;       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   template <typename T>   using PQ = priority_queue<T>;   template <typename T>   using GPQ = priority_queue<T, vector<T>, greater<T>>;   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> p);   template <class T>   ostream& operator<<(ostream& os, vector<T> v) {     os <<  [ ;     for (auto vv : v) os << vv <<  , ;     return os <<  ] ;   }   template <class T>   ostream& operator<<(ostream& os, set<T> v) {     os <<  [ ;     for (auto vv : v) os << vv <<  , ;     return os <<  ] ;   }   template <class L, class R>   ostream& operator<<(ostream& os, pair<L, R> p) {     return os <<  (  << p.first <<  ,  << p.second <<  ) ;   }   template <typename T>   inline T sq(T a) {     return a * a;   }   template <typename T>   T gcd(T a, T b) {     if (a > b) return gcd(b, a);     return a == 0 ? b : gcd(b % a, a);   }   template <typename T, typename U>   T mypow(T b, U n) {     if (n == 0) return 1;     if (n == 1) return b;     if (n % 2 == 0) {       return mypow(b * b, n / 2);     } else {       return mypow(b, n - 1) * b;     }   }   ll pcnt(ll b) { return __builtin_popcountll(b); }   template <typename T>   T iceil(T n, T d) {     return (n + d - 1) / d;   }   map<char, int> hand = {{ R , 0}, { S , 1}, { P , 2}};   class BIT {    public:     explicit BIT(int N, int v) : V_NUM(N) {       data.resize(N);       fill(data.begin(), data.end(), v);     }     ll query(int i) {       ll ret = 0;       while (i > 0) {         ret += data[i];         i -= (i & -i);       }       return ret;     }     void update(int i, int v) {       while (i < V_NUM) {         data[i] += v;         i += (i & -i);       }     }     int_fast32_t V_NUM;     vector<int> data;   };   int main() {     int N, Q;     string S;     cin >> N >> Q >> S;     S =  $  + S;     vector<BIT> bit(3, BIT(N + 1, 0));     vector<set<int>> pos(3);     for (int i = 1; i <= N; ++i) {       bit[hand[S[i]]].update(i, 1);       pos[hand[S[i]]].insert(i);     }     for (int q = 0; q <= Q; ++q) {       int lose = 0;       int emp = 0;       for (int h = 0; h < 3; ++h) {         if (pos[h].empty()) ++emp;       }       if (emp == 1) {         for (int h = 0; h < 3; ++h) {           if (pos[h].empty() || pos[(h + 1) % 3].empty()) continue;           lose = pos[(h + 1) % 3].size();         }       } else if (emp == 0) {         for (int h = 0; h < 3; ++h) {           int w = (h + 1) % 3, l = (h + 2) % 3;           int wpos = *(pos[w].begin()), lpos = *(pos[l].begin());           lose += max(0LL, bit[h].query(wpos) - bit[h].query(lpos));           wpos = *(--pos[w].end()), lpos = *(--pos[l].end());           lose += max(0LL, bit[h].query(lpos) - bit[h].query(wpos));         }       }       cout << N - lose <<   n ;       if (q == Q) break;       int p;       char c;       cin >> p >> c;       bit[hand[S[p]]].update(p, -1);       pos[hand[S[p]]].erase(p);       bit[hand[c]].update(p, 1);       pos[hand[c]].insert(p);       S[p] = c;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int n = s.length();     vector<vector<int> > pos( z  -  a  + 1);     for (int i = 0; i < n; i++) pos[s[i] -  a ].push_back(i);     int good = 0;     for (int c = 0; c <=  z  -  a ; c++) {       if (pos[c].empty()) continue;       int c_good = 0;       for (int k = 0; k < n; k++) {         int cnt[ z  -  a  + 1] = {};         for (int i : pos[c]) cnt[s[(i + k) % n] -  a ]++;         int cur_c_good = 0;         for (int d = 0; d <=  z  -  a ; d++) cur_c_good += (cnt[d] == 1);         c_good = max(c_good, cur_c_good);       }       good += c_good;     }     cout.precision(10);     cout << fixed;     cout << (double)good / n;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const double PI = acos(-1.0);   const int MAXN = 2e5 + 5;   long long int freq[MAXN];   long long int pref_sum(int l, int r) {     return (l ? freq[r] - freq[l - 1] : freq[r]);   }   void cp() {     int n;     long long int k;     cin >> n >> k;     vector<long long int> arr(n);     memset(freq, 0, sizeof(freq));     for (int i = 0; i < n; i++) cin >> arr[i], freq[arr[i]]++;     for (int i = 1; i < MAXN; i++) freq[i] += freq[i - 1];     sort(arr.begin(), arr.end());     int need = arr[0];     int ans = 0;     long long int cost = 0;     for (int i = 2e5; i >= need; i--) {       long long int cnt = pref_sum(i + 1, MAXN - 1);       long long int total = cost + cnt;       if (total > k) {         ans++;         cost = cnt;       } else         cost += cnt;     }     if (cost) {       ans++;     }     cout << ans <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     int t;     t = 1;     while (t--) {       cp();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 35005;   struct segtree {     segtree *left, *right;     int start, end, v, m;     int lazy;     segtree(int start, int end) : start(start), end(end), v(0), lazy(0) {       if (start == end) {         left = right = NULL;       } else {         m = (start + end) / 2;         left = new segtree(start, m);         right = new segtree(m + 1, end);       }     }     void propagate() {       if (lazy) {         v += lazy;         if (left != NULL) left->lazy += lazy;         if (right != NULL) right->lazy += lazy;         lazy = 0;       }     }     void update(int a, int b, int val) {       if (b < a) return;       propagate();       if (a == start && b == end) {         lazy += val;         propagate();       } else {         if (b <= m)           left->update(a, b, val);         else if (a > m)           right->update(a, b, val);         else           left->update(a, m, val), right->update(m + 1, b, val);         left->propagate();         right->propagate();         v = max(left->v, right->v);       }     }     int query(int a, int b) {       if (b < a) return 1;       propagate();       if (a == start && b == end) return v;       if (b <= m) return left->query(a, b);       if (a > m) return right->query(a, b);       return max(left->query(a, m), right->query(m + 1, b));     }   };   int dp[MAX][51];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     int n, k;     cin >> n >> k;     vector<int> v(n + 1);     for (int i = 1; i <= n; i++) {       cin >> v[i];     }     set<int> diff;     for (int i = 1; i <= n; i++) {       diff.insert(v[i]);       dp[i][1] = diff.size();     }     for (int i = 2; i <= k; i++) {       segtree cur(1, n);       for (int j = 1; j <= n; j++) {         cur.update(j, j, dp[j][i - 1]);       }       vector<int> last(n + 1, 1);       for (int j = 1; j <= n; j++) {         cur.update(last[v[j]], j - 1, 1);         dp[j][i] = cur.query(1, j - 1);         last[v[j]] = j;       }     }     cout << dp[n][k] <<   n ;   }
#include <cstdio>   #include <cstring>   #include <algorithm>   #define rep(a,b,c) for(int a=b;a<=c;a++)   #define per(a,b,c) for(int a=b;a>=c;a--)   #define int long long   const int inf=0x3f3f3f3f;   template<class T>inline void read(T &x) {    T f=1;x=0;char s=getchar();    while(s< 0 ||s> 9 ){if(s== - )f=-1;s=getchar();}    while(s>= 0 &&s<= 9 ){x=x*10+s- 0 ;s=getchar();}    x*=f;   }   const int N=1e6+1;   struct Node {    int i,j,val;   }a[N];   int n,du[N],idx;   char s[10];   inline bool cmp(Node a,Node b) { return a.val==b.val?a.j<b.j:a.val>b.val; }   inline void solve() {    read(n);    rep(i,1,n) read(du[i]);    rep(i,1,n-1) rep(j,i+1,n) {     if(du[i]>du[j]) a[++idx].i=i,a[idx].j=j,a[idx].val=du[i]-du[j];     else a[++idx].i=j,a[idx].j=i,a[idx].val=du[j]-du[i];    }    std::sort(a+1,a+idx+1,cmp);    rep(i,1,idx) {     printf( ? %lld %lld n ,a[i].i,a[i].j);     fflush(stdout);     scanf( %s ,s+1);     if(s[1]== Y ) {      printf( ! %lld %lld n ,a[i].i,a[i].j);      fflush(stdout);      return;     }    }    puts( ! 0 0 );    fflush(stdout);   }   signed main() {    //int TEST; read(TEST); while(TEST--)    solve();   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long int t, a, b, diff, n;     cin >> t;     while (t--) {       cin >> a >> b;       diff = abs(a - b);       n = ceil((-1 + sqrt(1 + 8 * diff)) / 2);       if (((n + 1) / 2) % 2 != diff % 2) {         if (n % 2)           n += 2;         else           n++;       }       if (n == -0) n = 0;       cout << n <<   n ;     }   }
#include <bits/stdc++.h>   template <typename T, typename U>   static inline void amin(T &x, U y) {     if (y < x) x = y;   }   template <typename T, typename U>   static inline void amax(T &x, U y) {     if (x < y) x = y;   }   using namespace std;   const int N = 2e5 + 5;   int par[N], r[N];   int find(int a) {     if (par[a] < 0)       return a;     else       return (par[a] = find(par[a]));   }   void merge(int a, int b) {     a = find(a);     b = find(b);     if (a == b) return;     if (r[a] > r[b]) {       par[b] = a;       r[a] += r[b];     } else {       par[a] = b;       r[b] += r[a];     }   }   void solve() {     int n, m, i, j;     cin >> n >> m;     for (i = 1; i < n + 1; i++) par[i] = -1, r[i] = 1;     for (i = 0; i < m; i++) {       int a, b;       cin >> a >> b;       merge(a, b);     }     long long int ans = 1;     for (i = 1; i < n + 1; i++) {       if (r[i] > 1 && par[i] < 0) ans = ans * pow(2, r[i] - 1);     }     cout << ans;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   struct Solution {     int n, k;     std::vector<std::vector<int>> as;     std::vector<int> pows;     int64_t calcPair(int ind1, int ind2) {       std::vector<int> left;       left.reserve(as[ind1].size());       for (int v : as[ind1]) {         left.push_back((int64_t(k - v) * pows[ind2 + 1]) % k);       }       std::sort(left.begin(), left.end());       int64_t result = 0;       size_t i1 = 0;       size_t j1 = 0;       std::vector<int>& right = as[ind2];       while (i1 < left.size() && j1 < right.size()) {         if (left[i1] < right[j1]) {           i1++;           continue;         }         if (left[i1] > right[j1]) {           j1++;           continue;         }         size_t i2 = i1;         while (i2 < left.size() && left[i2] == left[i1]) i2++;         size_t j2 = j1;         while (j2 < right.size() && right[j2] == right[j1]) j2++;         result += (i2 - i1) * int64_t(j2 - j1);         i1 = i2;         j1 = j2;       }       if (ind1 == ind2) {         for (int v : right) {           if ((v + int64_t(v) * pows[ind2 + 1]) % k == 0) {             result--;           }         }       }       return result;     }     void run(std::istream& in, std::ostream& out) {       in >> n >> k;       as.assign(10, std::vector<int>());       pows.resize(11);       pows[0] = 1;       for (int i = 1; i <= 10; i++) {         pows[i] = (pows[i - 1] * int64_t(10)) % k;       }       for (int i = 0; i < n; i++) {         int a;         in >> a;         int aa = a % k;         int len = 1;         while (a >= 10) {           a /= 10;           len++;         }         as[len - 1].push_back(aa);       }       for (size_t i = 0; i < as.size(); i++) {         std::sort(as[i].begin(), as[i].end());       }       int64_t ans = 0;       for (int i = 0; i < 10; i++) {         for (int j = 0; j < 10; j++) {           ans += calcPair(i, j);         }       }       out << ans <<   n ;     }   };   int main() {     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     Solution().run(std::cin, std::cout);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int pai[1000010];   int find(int x) {     if (pai[x] == x) return x;     return pai[x] = find(pai[x]);   }   void join(int a, int b) { pai[find(a)] = find(b); }   int main() {     int n, m, q;     cin >> n >> m >> q;     for (int i = 1; i <= n + m; i++) pai[i] = i;     for (int i = 0; i < q; i++) {       int a, b;       cin >> a >> b;       b += n;       join(a, b);     }     int comps = 0;     for (int i = 1; i <= n + m; i++) comps += pai[i] == i;     cout << comps - 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     long long k;     cin >> n;     cin >> k;     vector<long long> d(n);     vector<long long> afterd(n);     for (long long i = 0; i < n; i++) cin >> d[i];     for (long long i = 0; i < n; i++) cin >> afterd[i];     vector<pair<long long, long long> > utility(n);     for (long long i = 0; i < n; i++) {       utility[i].first = d[i] - afterd[i];       utility[i].second = i;     }     sort(utility.begin(), utility.end());     set<long long> u2;     long long p = 0;     while (k > 0 || utility[p].first < 0) {       u2.insert(utility[p].second);       p += 1;       k -= 1;     }     long long cost = 0;     for (long long i = 0; i < n; i++) {       if (u2.find(i) != u2.end())         cost += d[i];       else         cost += afterd[i];     }     cout << cost;   }   int32_t main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 10005;   const int P = 998244353;   struct edge {     int u, v, len;     inline edge() {}     inline edge(int _u, int _v, int _len) {       u = _u;       v = _v;       len = _len;     }   } a[N], Q[N], res[N];   struct node {     int l, r;     vector<int> E;     node *lson, *rson;   } pool[3 * N], *root;   int n, m;   int OscarDaGumert;   int top = -1;   int Index, cnt;   bitset<N> cur;   bitset<N> ans;   void B_T(node *now, int l, int r) {     now->l = l;     now->r = r;     if (l == r) return;     int lr = (l + r) >> 1, rl = lr + 1;     now->lson = &pool[++top];     B_T(now->lson, l, lr);     now->rson = &pool[++top];     B_T(now->rson, rl, r);   }   void change(node *now, int l, int r, int id) {     if (now->l == l && now->r == r) {       now->E.push_back(a[id].len);       return;     }     int lr = (now->l + now->r) >> 1, rl = lr + 1;     if (r < rl)       change(now->lson, l, r, id);     else if (l > lr)       change(now->rson, l, r, id);     else {       change(now->lson, l, lr, id);       change(now->rson, rl, r, id);     }   }   inline void query(node *now) {     bitset<N> tmp = cur;     int len = now->E.size();     for (int i = 0; i < len; i++) {       cur |= (cur << now->E[i]);     }     ans |= cur;     if (now->l != now->r) {       query(now->lson);       query(now->rson);     }     cur = tmp;   }   void init() {     int x1, x2, x3;     scanf( %d%d , &n, &m);     for (int i = 1; i <= m; i++) {       scanf( %d%d%d , &x1, &x2, &x3);       a[i] = edge(x1, x2, x3);     }     root = &pool[++top];     B_T(root, 1, n);     for (int i = 1; i <= m; i++) change(root, a[i].u, a[i].v, i);     cur[0] = 1;     query(root);     int cnt = 0;     for (int i = 1; i <= n; i++) {       if (ans[i] == 1) {         cnt++;       }     }     printf( %d n , cnt);     for (int i = 1; i <= n; i++) {       if (ans[i] == 1) {         printf( %d  , i);       }     }   }   int main(int argc, char **argv) {     init();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   int p[55];   string t;   void getp() {     int j = 0;     p[0] = p[1] = 0;     for (int i = 1; i < t.length(); ++i) {       while (j && t[j] != t[i]) j = p[j];       if (t[j] == t[i]) j++;       p[i + 1] = j;     }   }   void build() {     getp();     string ans = t;     int cnt = 1;     while (cnt < k) {       for (int i = p[t.length()]; i < t.length(); ++i) ans.append(1, t[i]);       cnt++;     }     cout << ans;   }   int main() {     cin >> n >> k >> t;     build();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, u, r;   vector<long long> k;   vector<int> a, b, p, tmp;   vector<vector<int> > v;   vector<int> w;   void read();   int main() {     ios_base::sync_with_stdio(false);     read();     v.push_back(a);     w.push_back(1);     long long s = 0;     if (u & 1)       s = -1e15;     else {       for (int i = 0; i < n; ++i) s += (a[i] * k[i]);     }     int uu = (u < 29 ? u : u - 2);     for (int i = 0; i < uu; ++i) {       int lim = v.size();       for (int j = 0; j < lim; ++j) {         if (w[j]) {           v.push_back(v[j]);           w.push_back(0);           for (int h = 0; h < n; ++h) v.back()[h] ^= b[h];           for (int h = 0; h < n; ++h) tmp[h] = v[j][p[h]] + r;           swap(v[j], tmp);         } else {           w[j] = 1;           for (int h = 0; h < n; ++h) tmp[h] = v[j][p[h]] + r;           swap(v[j], tmp);         }       }       if ((u - i) & 1) {         for (int i = 0; i < v.size(); ++i) {           long long ss = 0;           for (int j = 0; j < n; ++j) ss += (v[i][j] * k[j]);           s = max(s, ss);         }       }     }     if (uu < u) {       for (int i = 0; i < v.size(); ++i) {         long long ss = 0;         for (int j = 0; j < n; ++j) tmp[j] = (v[i][p[j]] ^ b[p[j]]) + r;         for (int j = 0; j < n; ++j) ss += (tmp[j] * k[j]);         s = max(s, ss);         ss = 0;         for (int j = 0; j < n; ++j) tmp[j] = ((v[i][p[j]] + r) ^ b[j]);         for (int j = 0; j < n; ++j) ss += (tmp[j] * k[j]);         s = max(s, ss);         ss = 0;         for (int j = 0; j < n; ++j) tmp[j] = v[i][p[p[j]]] + r + r;         for (int j = 0; j < n; ++j) ss += (tmp[j] * k[j]);         s = max(s, ss);       }     }     cout << s << endl;     return 0;   }   void read() {     cin >> n >> u >> r;     a.resize(n);     b.resize(n);     k.resize(n);     p.resize(n);     tmp.resize(n);     for (int i = 0; i < n; ++i) cin >> a[i];     for (int i = 0; i < n; ++i) cin >> b[i];     for (int i = 0; i < n; ++i) cin >> k[i];     for (int i = 0; i < n; ++i) cin >> p[i], --p[i];   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = 2005;   int d[maxN][maxN], c[maxN][maxN], D[maxN], C[maxN], dx[maxN][maxN],       cx[maxN][maxN];   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, k;     cin >> n >> k;     char cur;     for (int i = 1; i <= n; ++i)       for (int j = 1; j <= n; ++j) {         cin >> cur;         d[i][j] = d[i][j - 1] + (cur ==  W );         c[i][j] = c[i - 1][j] + (cur ==  W );       }     for (int i = 1; i <= n; ++i) {       D[i] = D[i - 1] + (d[i][n] == n);       C[i] = C[i - 1] + (c[n][i] == n);     }     for (int i = 1; i <= n; ++i)       for (int j = k; j <= n; ++j)         dx[i][j] = dx[i - 1][j] + (d[i][n] - d[i][j] + d[i][j - k] == n - k);     for (int i = k; i <= n; ++i)       for (int j = 1; j <= n; ++j)         cx[i][j] = cx[i][j - 1] + (c[n][j] - c[i][j] + c[i - k][j] == n - k);     int ans = 0;     for (int i = k; i <= n; ++i)       for (int j = k; j <= n; ++j)         ans = max(ans, D[n] - D[i] + D[i - k] + dx[i][j] - dx[i - k][j] + C[n] -                            C[j] + C[j - k] + cx[i][j] - cx[i][j - k]);     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, a[500010], x, sz;   long long ans;   int main() {     scanf( %d , &n);     for (int step = 1; step <= n; step++) {       scanf( %d , &x);       while (a[sz - 1] > a[sz] && a[sz] <= x && sz >= 2) {         ans += min(a[sz - 1], x);         sz--;       }       a[++sz] = x;     }     sort(a + 1, a + sz + 1);     for (int i = 1; i <= sz - 2; i++) ans += a[i];     printf( %I64d , ans);   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<int> v;   vector<int> ans_lis;   int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     cin >> n;     for (int i = 1; i <= n; i++) {       int x;       cin >> x;       v.emplace_back(x);     }     sort(v.begin(), v.end());     for (int i = (int)v.size() - 1; i >= 0; i--) {       if (i % 2 == 0) {         ans_lis.insert(ans_lis.begin(), v[i]);       } else         ans_lis.emplace_back(v[i]);     }     bool flag = true;     if (ans_lis[0] >= ans_lis.back() + ans_lis[1]) flag = false;     if (ans_lis.back() >= ans_lis[0] + ans_lis[(int)ans_lis.size() - 2])       flag = false;     for (int i = 1; i + 1 < (int)ans_lis.size() && flag; i++) {       if (ans_lis[i] >= ans_lis[i + 1] + ans_lis[i - 1]) flag = false;     }     if (!flag)       cout <<  NO  << endl;     else {       cout <<  YES  << endl;       for (int i = 0; i < (int)ans_lis.size(); i++) cout << ans_lis[i] <<    ;       cout << endl;     }     return 0;   }