malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 100;   int main() {     long long n, m, a[MAXN], st[MAXN];     scanf( %I64d %I64d , &n, &m);     st[0] = 1;     for (int i = 1; i <= n; i++) st[i] = st[i - 1] * 2;     int l = 0;     int r = n;     for (int i = 1; i < n; i++) {       if (m > st[n - i - 1]) {         a[r - 1] = i;         r--;         m -= st[n - i - 1];       } else {         a[l] = i;         l++;       }     }     a[l] = n;     for (int i = 0; i < n; i++) printf( %I64d  , a[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Edge {     int v;     Edge* next;   };   const int N = 100010;   int n, k;   bool vis[N];   int dis[N], prt[N], deg[N];   Edge* g[N];   void adde(int u, int v);   void dfs(int u);   void pred(int u, int r);   int main() {     scanf( %d%d , &n, &k);     for (int rep = 1; rep < n; ++rep) {       int u, v;       scanf( %d%d , &u, &v);       adde(u, v);       adde(v, u);     }     dfs(1);     int u = max_element(dis + 1, dis + n + 1) - dis;     memset(vis, 0, sizeof(vis));     dis[u] = 0;     prt[u] = 0;     dfs(u);     int m = max_element(dis + 1, dis + n + 1) - dis;     if (dis[m] != k * 2) {       puts( No );       return 0;     }     int cnt = k;     while (cnt--) m = prt[m];     memset(vis, 0, sizeof(vis));     pred(m, k);     puts( YES );     return 0;   }   void pred(int u, int r) {     vis[u] = true;     if (r == 0) {       if (deg[u]) {         puts( NO );         exit(0);       }       return;     }     if (deg[u] < 3) {       puts( NO );       exit(0);     }     for (Edge* p = g[u]; p; p = p->next)       if (!vis[p->v]) {         --deg[p->v];         pred(p->v, r - 1);       }   }   void dfs(int u) {     vis[u] = true;     for (Edge* p = g[u]; p; p = p->next)       if (!vis[p->v]) {         prt[p->v] = u;         dis[p->v] = dis[u] + 1;         dfs(p->v);       }   }   void adde(int u, int v) {     static Edge pool[N * 2];     static Edge* p = pool;     p->v = v;     p->next = g[u];     ++deg[u];     g[u] = p;     ++p;   }
#include <bits/stdc++.h>   using namespace std;   void solve() {     long long n;     cin >> n;     long long arr[n + 1];     long long sum = 0;     for (long long i = 1; i <= n; i++) {       cin >> arr[i];       sum += arr[i];     }     if (sum % n != 0) {       cout << -1 <<   n ;       return;     }     vector<pair<long long, pair<long long, long long>>> ans;     for (long long i = 2; i <= n; i++) {       long long brak = (i - arr[i] % i) % i;       ans.push_back(make_pair(1, make_pair(i, brak)));       arr[1] -= brak;       arr[i] += brak;       ans.push_back(make_pair(i, make_pair(1, arr[i] / i)));       arr[1] += arr[i];       arr[i] = 0;     }     for (long long i = 2; i <= n; i++) {       ans.push_back(make_pair(1, make_pair(i, sum / n)));     }     cout << ans.size() <<   n ;     for (auto x : ans) {       cout << x.first <<     << x.second.first <<     << x.second.second <<   n ;     }   }   int32_t main() {     {       ios_base::sync_with_stdio(false);       cin.tie(NULL);     }     long long t = 1;     cin >> t;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void trace_() { cerr <<   n ; }   template <typename T1, typename... T2>   void trace_(T1 t1, T2... t2) {     cerr <<     << t1;     trace_(t2...);   }   const long long mxN = 1e3 + 5;   long long n, a, b;   vector<long long> g[mxN];   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     ;     cin >> n;     vector<pair<long long, long long> > v1, v2;     for (long long i = 0; i < n - 3; i++) {       cin >> a >> b;       if (a > b) swap(a, b);       if (a != 1) v1.emplace_back(a, b);     }     sort(v1.begin(), v1.end(),          [](pair<long long, long long> a, pair<long long, long long> b) {            if (a.first == b.first) return a.second > b.second;            return a.first < b.first;          });     for (long long i = 0; i < n - 3; i++) {       cin >> a >> b;       if (a > b) swap(a, b);       g[a].push_back(b);     }     for (long long i = 2; i <= n; i++) {       sort(g[i].begin(), g[i].end());       reverse(g[i].begin(), g[i].end());       for (long long j = 1; j < (long long)(g[i].size()); j++)         v2.emplace_back(1, g[i][j]);       if ((long long)(g[i].size()) >= 1) v2.emplace_back(1, i + 1);     }     cout << (long long)(v1.size()) + (long long)(v2.size()) <<   n ;     for (pair<long long, long long> i : v1)       cout << i.first <<     << i.second <<   n ;     reverse(v2.begin(), v2.end());     for (pair<long long, long long> i : v2)       cout << i.first <<     << i.second <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, a[21], b[21], dp[42][42], dp2[82][42];   int main() {     cin >> n >> m;     for (int i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < n; i++) cin >> b[i];     memset(dp, 0, sizeof(dp));     memset(dp2, 0, sizeof(dp2));     dp[0][1] = a[0 % n];     dp2[0][42] = a[0 % n];     dp2[0][40] = b[0 % n];     for (int i = 1; i < n * 2; i++) {       for (int j = 0; j <= i + 1; j++) {         if (j == i + 1)           dp[i][j] = dp[i - 1][j - 1] + a[i % n];         else if (j == 0)           dp[i][j] = dp[i - 1][j + 1] + b[i % n];         else           dp[i][j] =               min(dp[i - 1][j + 1] + b[i % n], dp[i - 1][j - 1] + a[i % n]);       }     }     for (int i = 1; i < n * 2; i++) {       for (int j = 41 - i - 1; j <= 41 + i + 1; j++) {         if (j == 41 + i + 1)           dp2[i][j] = dp2[i - 1][j - 1] + a[i % n];         else if (j == 41 - i - 1)           dp2[i][j] = dp2[i - 1][j + 1] + b[i % n];         else           dp2[i][j] =               min(dp2[i - 1][j + 1] + b[i % n], dp2[i - 1][j - 1] + a[i % n]);       }     }     cout << 1ll * (m - 2) / 2ll * dp2[2 * n - 1][41] + dp[2 * n - 1][0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline bool check(int x) {     for (; x; x /= 10)       if (x % 10 != 4 && x % 10 != 7) return 0;     return 1;   }   const int maxn = 100100;   int head[maxn], nxt[maxn << 1], ver[maxn << 1], val[maxn << 1], tot;   inline void addedge(int a, int b, int c) {     nxt[++tot] = head[a];     ver[tot] = b;     val[tot] = c;     head[a] = tot;     nxt[++tot] = head[b];     ver[tot] = a;     val[tot] = c;     head[b] = tot;   }   int size[maxn], sum[maxn];   int n, m;   inline void getsize(int x, int fat) {     size[x] = 1;     sum[x] = 0;     for (int i = head[x]; i; i = nxt[i]) {       int y = ver[i];       if (y == fat) continue;       getsize(y, x);       size[x] += size[y];       sum[x] += val[i] ? size[y] : sum[y];     }   }   long long ans = 0;   inline void getans(int x, int fat, int upv) {     ans += 1ll * (upv + sum[x]) * (upv + sum[x] - 1);     for (int i = head[x]; i; i = nxt[i]) {       int y = ver[i];       if (y == fat) continue;       getans(y, x, val[i] ? (size[1] - size[y]) : (upv + sum[x] - sum[y]));     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i < n; i++) {       int a, b, c;       scanf( %d%d%d , &a, &b, &c);       addedge(a, b, check(c));     }     getsize(1, 0);     getans(1, 0, 0);     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 5;   vector<int> AdjList[maxn];   bool vis[maxn];   int child[maxn];   char ans[maxn];   int n;   void dfs(int s, int p = -1) {     child[s] = 1;     for (int u : AdjList[s]) {       if (vis[u]) continue;       if (u == p) continue;       dfs(u, s);       child[s] += child[u];     }   }   int find_cen(int s, int p, int sz) {     for (int u : AdjList[s]) {       if (vis[u]) continue;       if (u == p) continue;       if (child[u] * 2 > sz) return find_cen(u, s, sz);     }     return s;   }   void centroid(int u, char c) {     dfs(u, u);     int cen = find_cen(u, u, child[u]);     ans[cen] = c;     vis[cen] = 1;     for (int v : AdjList[cen]) {       if (vis[v]) continue;       centroid(v, c + 1);     }   }   signed main() {     cin >> n;     for (int i = 1; i < n; i++) {       int u, v;       cin >> u >> v;       AdjList[u].push_back(v);       AdjList[v].push_back(u);     }     centroid(1,  A );     for (int i = 1; i <= n; i++) {       cout << ans[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   const double eps = 1e-9;   const long long INF = 1000000000;   const int MAXN = 128;   int points[] = {0, 25, 18, 15, 12, 10, 8, 6, 4, 2, 1};   int t, n;   map<string, vector<int> > places;   map<string, int> score;   struct T {     string name;     vector<int> place;     int score;   };   bool cmp1(const T& a, const T& b) {     if (a.score != b.score) return a.score > b.score;     for (int i = 1; i <= 55; ++i)       if (a.place[i] != b.place[i]) return a.place[i] > b.place[i];     return a.name < b.name;   }   bool cmp2(const T& a, const T& b) {     if (a.place[1] != b.place[1]) return a.place[1] > b.place[1];     if (a.score != b.score) return a.score > b.score;     for (int i = 2; i <= 55; ++i)       if (a.place[i] != b.place[i]) return a.place[i] > b.place[i];     return a.name < b.name;   }   int main() {     cin >> t;     for (int tt = 0; tt < t; ++tt) {       cin >> n;       for (int i = 1; i <= n; ++i) {         string s;         cin >> s;         int kk = 0;         if (i <= 10) kk = points[i];         score[s] += kk;         if (places.find(s) == places.end()) places[s] = vector<int>(60);         places[s][i]++;       }     }     map<string, int>::iterator it;     vector<T> a;     for (it = score.begin(); it != score.end(); ++it) {       a.push_back(T());       a.back().name = it->first;       a.back().place = places[it->first];       a.back().score = it->second;     }     sort((a).begin(), (a).end(), cmp1);     cout << a[0].name << endl;     sort((a).begin(), (a).end(), cmp2);     cout << a[0].name << endl;   }
#include <bits/stdc++.h>   using namespace std;   long long a[300005];   int sign(long long x) { return x > 0 ? 1 : -1; }   struct Node {     int l, r, lx, mx, rx;     void up(Node L, Node R) {       lx = L.lx;       rx = R.rx;       mx = max(L.mx, R.mx);       if (a[L.r] && a[R.l] && sign(a[L.r]) >= sign(a[R.l])) {         mx = max(mx, L.rx + R.lx);         if (L.mx == L.r - L.l + 1) lx = L.mx + R.lx;         if (R.mx == R.r - R.l + 1) rx = R.mx + L.rx;       }       mx = max({mx, lx, rx});     }   } seg[300005 * 4];   void update(int qx, int c, int l, int r, int i) {     seg[i] = {l, r};     if (l == r) {       a[qx] += c;       if (a[qx] != 0)         seg[i] = {l, r, 1, 1, 1};       else         seg[i] = {l, r, 0, 0, 0};       return;     }     int mid = (l + r) / 2;     if (qx <= mid)       update(qx, c, l, mid, i * 2);     else       update(qx, c, mid + 1, r, i * 2 + 1);     seg[i].up(seg[i * 2], seg[i * 2 + 1]);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; ++i) scanf( %I64d , &a[i]);     for (int i = 1; i < n; ++i) a[i] = a[i + 1] - a[i];     for (int i = 1; i < n; ++i) update(i, 0, 1, n - 1, 1);     int Q;     scanf( %d , &Q);     while (Q--) {       int l, r, x;       scanf( %d%d%d , &l, &r, &x);       if (l > 1) update(l - 1, x, 1, n - 1, 1);       if (r < n) update(r, -x, 1, n - 1, 1);       printf( %d n , seg[1].mx + 1);     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 100010;   const int INF = 1000000;   int n;   stack<int> killers;   int main() {     cin >> n;     int values[n + 1], life[n + 1];     for (int i = 1; i <= n; ++i) {       cin >> values[i];     }     life[0] = INF;     values[0] = 0;     killers.push(0);     for (int i = 1; i <= n; ++i) {       life[i] = 1;       while (killers.size() && values[i] > values[killers.top()]) {         life[i] = max(life[i], life[killers.top()] + 1);         killers.pop();       }       killers.push(i);     }     int sol = 0;     for (int i = 1; i <= n; ++i) {       if (life[i] < INF) {         sol = max(sol, life[i]);       }     }     cout << sol << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int arr[n];     for (int i = 0; i < n; ++i) {       cin >> arr[i];     }     for (int j = 0; j < n; ++j) {       if (arr[j] == 0)         if (j == (n - 1))           cout <<  EASY  << endl;         else           continue;       else {         cout <<  HARD  << endl;         break;       }     }     return 0;   };
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, j, k, l, m, n;     int a[1001];     char s[1000], c;     while (scanf( %d%d , &n, &m) != EOF) {       memset(a, 0, sizeof(a));       while (m--) {         scanf( %s , s);         scanf( %s , s);         scanf( %s , s);         c = s[0];         scanf( %s , s);         scanf( %d , &k);         if (c ==  r ) {           for (i = 1; i <= k; i++) a[i] = 1;         } else {           for (i = k; i <= n; i++) a[i] = 1;         }       }       k = 0;       for (i = 1; i <= n; i++)         if (a[i] == 0) k++;       if (k == 0)         printf( -1 n );       else         printf( %d n , k);     }   }
#include <bits/stdc++.h>   using namespace std;   char str[100];   int L;   bool ok(int n) {     if (n == 0) return true;     set<string> S;     for (int i = 0; i + n <= L; i++) {       string temp(str + i, str + i + n);       if (!S.insert(temp).second) return true;     }     return false;   }   int main() {     scanf( %s , str);     L = strlen(str);     int low = 0, high = L;     while (high - low > 1) {       int mid = (low + high) / 2;       if (ok(mid)) {         low = mid;       } else {         high = mid;       }     }     if (ok(high)) {       printf( %d n , high);     } else {       printf( %d n , low);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a[100000];   map<long long, long long> mp;   map<long long, long long> mp1;   int main() {     cin.sync_with_stdio(false);     long long i, j, k, l, c, d, e, f, g, h, v, b[10000], aa[1009], bb[1000], n, m,         r, t;     long long x1, x2, y1, y2, x3, x4, x5, x6;     string s, s1, s2 =  fedabc , s3, ss[10000];     while (cin >> n) {       cin >> s[0];       c = 0;       if (n % 4 == 1 || n % 4 == 2) {         c = c + 16 * (n / 4);         i = (n / 4) * 4 + 1;         for (i;; i = i + 3) {           if (i == n) {             for (k = 0; k < 6; k++) {               c++;               if (s[0] == s2[k]) break;             }             cout << (c);             return 0;           } else {             c += 6 + 1;             for (j = i + 1; j <= (i + 1); j++) {               if (j == n) {                 for (k = 0; k < 6; k++) {                   c++;                   if (s[0] == s2[k]) break;                 }                 cout << (c);                 return 0;               }               c += 3;               i = j;             }           }         }       } else {         m = n - 1;         c = c + 16 * (m / 4);         i = (m / 4) * 4 + 3;         {           for (i;; i = i + 3) {             if (i == n) {               for (k = 0; k < 6; k++) {                 c++;                 if (s[0] == s2[k]) break;               }               cout << (c);               return 0;             } else {               c += 6 + 1;               for (j = i + 1; j <= (i + 1); j++) {                 if (j == n) {                   for (k = 0; k < 6; k++) {                     c++;                     if (s[0] == s2[k]) break;                   }                   cout << (c);                   return 0;                 }                 c += 3;                 i = j;               }             }           }         }       }     }   }
#include <bits/stdc++.h>   using namespace std;   const unsigned long long hash1 = 201326611;   const int inf = 0x3f3f3f3f;   const int _inf = 0xc0c0c0c0;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const long long _INF = 0xc0c0c0c0c0c0c0c0;   const long long mod = (int)1e9 + 7;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   long long ksm(long long a, long long b, long long mod) {     int ans = 1;     while (b) {       if (b & 1) ans = (ans * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return ans;   }   long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); }   void exgcd(long long a, long long b, long long &x, long long &y, long long &d) {     if (!b) {       d = a;       x = 1;       y = 0;     } else {       exgcd(b, a % b, y, x, d);       y -= x * (a / b);     }   }   inline int read() {     int date = 0, m = 1;     char ch = 0;     while (ch !=  -  && (ch< 0  | ch>  9 )) ch = getchar();     if (ch ==  - ) {       m = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       date = date * 10 + ch -  0 ;       ch = getchar();     }     return date * m;   }   const int MAX_N = 150025;   int C[MAX_N << 1], arr[MAX_N], b[MAX_N];   void add(int x, int v) {     for (; x < MAX_N; x += x & (-x)) C[x] += v;   }   int get(int x) {     int res = 0;     for (; x; x -= x & (-x)) res += C[x];     return res;   }   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       for (int i = 1; i <= 2 * n + 2; ++i) C[i] = 0;       for (int i = 1; i <= n; ++i) scanf( %d , &arr[i]), b[i] = arr[i];       sort(b + 1, b + 1 + n);       int sz = unique(b + 1, b + 1 + n) - b - 1;       int ans = 0;       for (int i = n; i >= 1; --i) {         arr[i] = lower_bound(b + 1, b + 1 + sz, arr[i]) - b;         ans += min(get(arr[i]), 1);         add(arr[i] + 1, 1);       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   stack<int> S;   int a[1005], n;   bool judge() {     for (int i = 0; i < n; i++)       if (a[i] != i) return 0;     return 1;   }   int main() {     cin >> n;     for (int i = 0; i < n; i++) scanf( %d , &a[i]);     for (int i = 0; i <= n; i++) {       if (judge()) {         cout <<  Yes  << endl;         return 0;       }       for (int j = 0; j < n; j += 2) a[j] = (a[j] + 1) % n;       for (int j = 1; j < n; j += 2) a[j] = (a[j] - 1 + n) % n;     }     cout <<  No  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string c =  ROYGBIV ;     vector<char> s;     for (int i = 0; i < n; i++) {       if (i <= 6)         s.push_back(c[i]);       else {         s.push_back(s[i - 4]);       }     }     for (int i = 0; i < n; i++) cout << s[i];   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   inline bool uax(T &x, T y) {     return (y > x) ? x = y, true : false;   }   template <typename T>   inline bool uin(T &x, T y) {     return (y < x) ? x = y, true : false;   }   template <typename T>   void kek(T ans) {     cout << ans << endl;     exit(0);   }   const int MOD = (int)1e9 + 7;   const long long INF = (long long)1e18 + 42;   int main() {     cin.tie(nullptr)->sync_with_stdio(false);     int n;     cin >> n;     vector<vector<int>> a(n);     vector<set<pair<int, int>>> b(3);     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       a[x].push_back(i);     }     vector<int> team(3, 0);     vector<int> ans;     for (int i = 0; i < n; ++i) {       for (int j : a[i]) {         b[i % 3].emplace(i, j);       }       if (b[i % 3].empty()) kek( Impossible );       auto it = b[i % 3].upper_bound({i - team[i % 3], n});       if (it == b[i % 3].begin()) kek( Impossible );       --it;       ans.push_back(it->second);       uax(team[i % 3], i - it->first);       b[i % 3].erase(it);     }     cout <<  Possible n ;     for (int i : ans) cout << 1 + i <<    ;     cout <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   long long n, a, b;   long long tinh(long long n) {     long long l = 0, r = 1000001;     while (l < r) {       long long m = (l + r + 1) / 2;       long long t = m * m * m;       if (t > n)         r = m - 1;       else         l = m;     }     return l;   }   int main() {     scanf( %d , &n);     for (int i = (1); i <= (n); i++) {       scanf( %lld %lld , &a, &b);       long long res = tinh(a * b);       if (res * res * res == a * b && a % res == 0 && b % res == 0) {         printf( YES n );       } else         printf( NO n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using vi = vector<int>;   const int LOG = 17;   const int inf = 1e9;   template <typename T>   struct RMQ {     vector<vector<T>> spr;     RMQ(const vector<T>& v) : spr(1, v) {       for (int j = 1; (1 << j) <= (int)v.size(); ++j) {         spr.push_back(vector<T>((int)v.size() - (1 << j) + 1));         for (int i = 0; i < (int)spr[j].size(); ++i) {           spr[j][i] = min(spr[j - 1][i], spr[j - 1][i + (1 << (j - 1))]);         }       }     }     T get(int l, int r) {       int d = 31 - __builtin_clz(r - l + 1);       return min(spr[d][l], spr[d][r - (1 << d) + 1]);     }   };   int main() {     cin.tie(0)->sync_with_stdio(0);     int n, m, q;     cin >> n >> m >> q;     vi v(n), g(n);     for (int i = 0; i < n; ++i) cin >> v[i], v[i]--;     for (int i = 0; i < n; ++i) g[v[i]] = v[(i + 1) % n];     vi a(m);     for (int i = 0; i < m; ++i) cin >> a[i], a[i]--;     vector<vi> spr(LOG + 1, vi(m));     vi nxt(n, -1);     for (int i = m - 1; i >= 0; --i) {       nxt[a[i]] = i;       spr[0][i] = nxt[g[a[i]]];     }     for (int j = 0; j < LOG; ++j) {       for (int i = 0; i < m; ++i) {         if (spr[j][i] == -1)           spr[j + 1][i] = -1;         else           spr[j + 1][i] = spr[j][spr[j][i]];       }     }     vi res(m);     for (int i = 0; i < m; ++i) {       int k = i;       for (int j = LOG; j >= 0; --j) {         if ((n - 1) & (1 << j)) {           k = spr[j][k];           if (k == -1) break;         }       }       res[i] = k == -1 ? inf : k;     }     RMQ<int> rmq(res);     string ans;     for (int i = 0; i < q; ++i) {       int l, r;       cin >> l >> r;       --l, --r;       int a = rmq.get(l, r);       if (a <= r)         ans.push_back( 1 );       else         ans.push_back( 0 );     }     cout << ans <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const long double EPS = 1e-15;   const long double PI = 3.14159265358979323846;   inline int lg2(long long n) {     int h = 0;     while ((1ll << h) < n) ++h;     return h;   }   struct configio {     configio() {       cin.tie(nullptr);       ios_base::sync_with_stdio(false);     }   } cnfio;   int diff(int a, int b, int mod) { return ((a - b) % mod + mod) % mod; }   struct candy {     int h, m;   };   int main() {     int n, x;     cin >> n >> x;     vector<candy> v1[2];     for (int i = 0; i < n; ++i) {       int t, h, m;       cin >> t >> h >> m;       v1[t].push_back({h, m});     }     long long ans = 0;     for (int type = 0; type <= 1; ++type) {       vector<candy> v[2];       v[0] = v1[0];       v[1] = v1[1];       int cur_ans = 0;       long long cur_x = x;       int cur_type = type;       while (true) {         int id = -1;         for (int i = 0; i < v[cur_type].size(); ++i) {           if (v[cur_type][i].h <= cur_x) {             if (id == -1 || (v[cur_type][id].m < v[cur_type][i].m)) {               id = i;             }           }         }         if (id == -1) {           break;         }         cur_ans++;         cur_x += v[cur_type][id].m;         v[cur_type].erase(v[cur_type].begin() + id);         cur_type ^= 1;       }       ans = max(ans, (long long)cur_ans);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 4e3 + 5;   long long n, m, a[N], f[N][N];   long long solve(long long l, long long r) {     if (l > r) return 0;     long long p = 0;     for (long long i = l; i <= r; i++)       if (a[i] < a[p]) p = i;     long long ls = solve(l, p - 1), rs = solve(p + 1, r);     memset(f[p], -0x3f, sizeof(f[p]));     for (long long i = 0; i <= p - l; i++)       for (long long j = 0; j <= r - p; j++) {         f[p][i + j] = max(f[p][i + j], f[ls][i] + f[rs][j] - 2 * i * j * a[p]);         f[p][i + j + 1] =             max(f[p][i + j + 1], f[ls][i] + f[rs][j] - 2 * i * j * a[p] -                                      2 * (i + j) * a[p] + (m - 1) * a[p]);       }     return p;   }   signed main() {     cin >> n >> m;     a[0] = 1e18;     for (long long i = 1; i <= n; i++) cin >> a[i];     long long p = solve(1, n);     cout << f[p][m] << endl;   }