malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int n, dist[8][8], dis[8][100010], lst[8], mask[100010], cnt[8][300], ans;   long long res;   char s[100010];   bool use[8];   queue<int> q;   int main() {     scanf( %d , &n);     scanf( %s , s);     for (int c = 0; c < 8; c++) lst[c] = -1;     for (int i = 0; i < 8; i++)       for (int j = 0; j < 8; j++) dist[i][j] = 0x3f3f3f3f;     for (int i = 0; i < n; i++) {       int now = s[i] -  a ;       lst[now] = i;       for (int c = 0; c < 8; c++)         if (lst[c] != -1)           dist[c][now] = dist[now][c] = min(dist[now][c], i - lst[c]);     }     for (int k = 0; k < 8; k++)       for (int i = 0; i < 8; i++)         for (int j = 0; j < 8; j++)           dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j] + 1);     for (int c = 0; c < 8; c++)       for (int i = 0; i < n; i++) dis[c][i] = 0x3f3f3f3f;     for (int c = 0; c < 8; c++) {       for (int i = 0; i < 8; i++) use[i] = false;       for (int i = 0; i < n; i++)         if (s[i] == c +  a ) q.push(i), dis[c][i] = 0;       while (!q.empty()) {         int u = q.front();         q.pop();         if (u > 0 && dis[c][u - 1] > dis[c][u] + 1) {           dis[c][u - 1] = dis[c][u] + 1;           q.push(u - 1);         }         if (u < n - 1 && dis[c][u + 1] > dis[c][u] + 1) {           dis[c][u + 1] = dis[c][u] + 1;           q.push(u + 1);         }         if (!use[s[u] -  a ]) {           for (int i = 0; i < n; i++)             if (s[i] == s[u] && dis[c][i] > dis[c][u] + 1) {               dis[c][i] = dis[c][u] + 1;               q.push(i);             }           use[s[u] -  a ] = true;         }       }     }     for (int i = 0; i < n; i++) {       for (int j = max(i - 15, 0); j < i; j++) {         int p = i - j;         for (int c = 0; c < 8; c++) {           if (p > dis[c][i] + dis[c][j] + 1) p = dis[c][i] + dis[c][j] + 1;         }         if (p == ans) res++;         if (p > ans) ans = p, res = 1;       }       if (i >= 16) cnt[s[i - 16] -  a ][mask[i - 16]]++;       for (int c = 0; c < 8; c++) {         for (int opt = 0; opt < 256; opt++)           if (cnt[c][opt]) {             int p = 0x3f3f3f3f;             for (int j = 0; j < 8; j++) {               if (p > dis[j][i] + 1 + dist[j][c] + ((opt >> j) & 1))                 p = dis[j][i] + 1 + dist[j][c] + ((opt >> j) & 1);             }             if (p == ans) res += cnt[c][opt];             if (p > ans) ans = p, res = cnt[c][opt];           }       }       for (int c = 0; c < 8; c++)         mask[i] |= ((dis[c][i] - dist[c][s[i] -  a ]) << c);     }     cout << ans <<     << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 4000010;   long long f[MAXN], sum[MAXN], MOD;   int main() {     int n;     scanf( %d %lld , &n, &MOD);     f[n] = 1;     sum[n] = 1;     for (int i = n - 1; i; --i) {       f[i] = (f[i] + sum[i + 1]) % MOD;       for (int j = 2; j * i <= n; ++j) {         int l = j * i, r = min(n, i * j + j - 1);         f[i] += sum[l] - sum[r + 1];         f[i] = (f[i] % MOD + MOD) % MOD;       }       sum[i] = (sum[i + 1] + f[i]) % MOD;     }     printf( %lld n , f[1]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 998244353;   template <class T>   inline void read(T &res) {     char c;     T flag = 1;     while ((c = getchar()) <  0  || c >  9 )       if (c ==  - ) flag = -1;     res = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) res = res * 10 + c -  0 ;     res *= flag;   }   long long quick(long long a, long long b) {     long long ans = 1 % mod;     while (b) {       if (b & 1) ans = (ans % mod * a % mod) % mod;       a = (a % mod * a % mod) % mod;       b >>= 1;     }     return ans % mod;   }   int yy[1005000], m1[1005000];   vector<int> vv[1005000];   int main() {     int n;     read(n);     map<int, int> mm;     int cutt = 0;     for (int i = 1; i <= n; i++) {       int m;       read(m);       for (int j = 1; j <= m; j++) {         int x;         read(x);         if (mm[x] == 0) yy[cutt++] = x;         mm[x]++;         vv[i].push_back(x);       }     }     for (int i = 0; i < cutt; ++i) {       m1[i] = quick(mm[yy[i]], mod - 2);     }     long long ans = 0;     long long sss = quick(cutt, mod - 2);     long long s1 = quick(n, mod - 2);     for (int i = 1; i <= n; i++) {       int y = vv[i].size();       long long s2 = quick(y, mod - 2);       for (int j = 0; j < vv[i].size(); j++) {         int x = vv[i][j];         int a = mm[x];         ans = ((ans % mod) % mod +                ((s1 * s2 % mod) % mod * (a * s1 % mod) % mod) % mod) %               mod;       }     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long INF = 1ll << 60, INF2 = 1ll << 58;   queue<long long> q;   vector<long long> ll, rr, g;   long long lft[525000], rght[525000];   int main() {     int n, r = 0, ans = 0;     long long k, len = INF;     scanf( %d %I64d , &n, &k);     ll.resize(n), rr.resize(n);     for (int i = 0; i < n; ++i) {       scanf( %I64d %I64d , &ll[i], &rr[i]);       len = min(len, rr[i] - ll[i]);     }     g.reserve(525000);     q.push(4), q.push(7);     while (!q.empty()) {       long long x = q.front();       q.pop();       g.push_back(x);       if (x > INF2) continue;       q.push(x * 10ll + 4ll), q.push(x * 10ll + 7ll);     }     for (int i = 0; i < g.size(); ++i) lft[i] = INF, rght[i] = INF;     sort(rr.begin(), rr.end());     sort(ll.rbegin(), ll.rend());     long long cnt = 0, cost = 0, last = 4ll;     for (int i = 0; i < g.size() && cost <= k; ++i) {       cost += cnt * (g[i] - last);       while (cnt < n && rr[cnt] <= g[i]) cost += g[i] - rr[cnt], cnt++;       if (cost > k || cost < 0) break;       lft[i] = cost;       last = g[i];     }     cost = 0, cnt = 0, last = g.back();     for (int i = g.size() - 1; i >= 0; --i) {       cost += cnt * (last - g[i]);       while (cnt < n && ll[cnt] >= g[i]) cost += ll[cnt] - g[i], cnt++;       if (cost > k || cost < 0) break;       rght[i] = cost;       last = g[i];     }     for (int i = 0; i < g.size(); ++i) {       r = max(i, r);       while (r < g.size() && g[r] - g[i] <= len && lft[r] + rght[i] <= k)         ans = max(ans, r - i + 1), r++;     }     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100005];   int w[100005];   int x[100005];   int main() {     int n;     scanf( %d , &n);     int i;     for (i = 0; n > i; i++) {       scanf( %d , &a[i]);     }     w[0] = a[0];     for (i = 1; n > i; i++) {       w[i] = w[i - 1] + a[i];     }     int k;     string s;     cin >> s;     unsigned int max = 0;     unsigned int sum = 0;     for (i = 0; n > i; i++) {       if (s[i] ==  1 ) {         sum += a[i];       }       x[i] = sum;     }     int temp;     max = sum;     for (i = 1; n > i; i++) {       if (s[i] ==  1 ) {         temp = sum - x[i] + w[i - 1];         if (temp > max) {           max = temp;         }       }     }     printf( %d , max);   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 2147483647;   const int N = 100005;   int n, i, tab[N], res, act, a, was[N];   int main() {     scanf( %d , &n);     for (i = 0; i < n; i++) scanf( %d , &tab[i]);     res = 0;     act = 0;     for (i = 0; i < n; i++) {       scanf( %d , &a);       was[a] = 1;       if (a != tab[act])         res++;       else {         act++;         while (act < n && was[tab[act]]) act++;       }     }     printf( %d n , res);     return 0;   }
#include <bits/stdc++.h>   int main() {     int n;     std::cin >> n;     long long sum = 0;     int j;     for (int i = 2; i <= n / 2; i++) {       j = 2;       while (i * j <= n) {         sum += j;         j++;       }     }     sum = sum * 4;     std::cout << sum;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200005;   int n;   int A[N];   int main() {     int t;     scanf( %d , &t);     while (t--) {       scanf( %d , &n);       n *= 2;       for (int i = 1; i <= n; i++) {         scanf( %d , &A[i]);       }       sort(A + 1, A + 1 + n);       n /= 2;       printf( %d n , A[n + 1] - A[n]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1.12e5;   const int LEN = 6.4 * N;   int len;   int c[N], sz[N];   int s[LEN], sa[LEN], *rk, ht[LEN], v[2][LEN], ct[LEN];   long long co[LEN];   void build_sa(int n, int m) {     int *x = v[0], *y = v[1];     fill_n(ct, m, 0);     for (int i = (0); i != (n); ++i) ++ct[x[i] = s[i]];     for (int i = (1); i != (m); ++i) ct[i] += ct[i - 1];     for (int i = (n - 1); i != (-1); --i) sa[--ct[x[i]]] = i;     for (int k = 1; k < n; k <<= 1) {       int p = 0;       for (int i = (n - k); i != (n); ++i) y[p++] = i;       for (int i = (0); i != (n); ++i)         if (sa[i] >= k) y[p++] = sa[i] - k;       fill_n(ct, m, 0);       for (int i = (0); i != (n); ++i) ++ct[x[y[i]]];       for (int i = (1); i != (n); ++i) ct[i] += ct[i - 1];       for (int i = (n - 1); i != (-1); --i) sa[--ct[x[y[i]]]] = y[i];       swap(x, y);       x[sa[0]] = 0;       p = 1;       for (int i = (1); i != (n); ++i)         x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k]                        ? p - 1                        : p++;       if ((m = p) == n) {         rk = x;         break;       }     }     int h = 0;     ht[0] = 0;     for (int i = (0); i != (n); ++i) {       if (!rk[i]) {         h = 0;         continue;       }       while (s[i + h] == s[sa[rk[i] - 1] + h]) ++h;       ht[rk[i]] = h;       if (h) --h;     }   }   int ls[LEN], rs[LEN];   int min(int a, int b) { return a < b ? a : b; }   int max(int a, int b) { return a > b ? a : b; }   typedef int (*func)(int, int);   struct BIT {     func f;     int t[LEN], v;     void init(func _f, int _v) {       f = _f;       v = _v;       fill_n(t, len + 1, v);     }     void add(int i, int x) {       for (; i <= len; i += i & -i) t[i] = f(t[i], x);     }     int qry(int i) {       int ret = v;       for (; i; i ^= i & -i) ret = f(ret, t[i]);       return ret;     }   } bit;   int main() {     int n;     scanf( %d  , &n);     len = 0;     for (int i = (0); i != (n); ++i) {       int ch;       int la = len;       while ((ch = getchar()) !=   n ) s[len++] = n + ch -  a ;       sz[i] = len - la;       s[len++] = i;     }     long long ans = 0;     for (int i = (0); i != (n); ++i) scanf( %d , c + i);     build_sa(len, n + 26);     int la = 0;     for (int i = (0); i != (n); ++i) {       if (ht[rk[la]] != sz[i] && ht[rk[la] + 1] != sz[i])         ans = max(ans, sz[i] * 1LL * c[i]);       la += sz[i] + 1;     }     int idx = n - 1;     for (int i = (len - 1); i != (-1); --i) co[rk[i]] = c[idx = min(idx, s[i])];     for (int i = (1); i != (len); ++i) co[i] += co[i - 1];     bit.init(max, -1);     for (int i = (0); i != (len); ++i) {       ls[i] = bit.qry(ht[i]);       bit.add(ht[i] + 1, i);     }     bit.init(min, len);     for (int i = (len - 1); i != (-1); --i) {       rs[i] = bit.qry(ht[i]);       bit.add(ht[i] + 1, i);     }     for (int i = (0); i != (len); ++i)       if (ht[i]) ans = max(ans, ht[i] * (co[rs[i] - 1] - co[ls[i] - 1]));     printf( %lld n , ans);   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   long long tree[400020][21], lazy[400020][21], arr[100005];   void build(int l, int r, int node, int i) {     if (l == r) {       tree[node][i] = 0;       if (arr[l] & (1 << i)) {         tree[node][i] = 1;       }       return;     }     int mid = (l + r) >> 1;     build(l, mid, 2 * node, i);     build(mid + 1, r, 2 * node + 1, i);     tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];     return;   }   void update(int l, int r, int ql, int qr, int node, int i) {     if (lazy[node][i]) {       tree[node][i] = (r - l + 1) - tree[node][i];       if (l != r) {         lazy[2 * node][i] ^= 1;         lazy[2 * node + 1][i] ^= 1;       }       lazy[node][i] = 0;     }     if (l > qr || r < ql) return;     if (ql <= l && r <= qr) {       tree[node][i] = (r - l + 1) - tree[node][i];       if (l != r) {         lazy[2 * node][i] ^= 1;         lazy[2 * node + 1][i] ^= 1;       }       lazy[node][i] = 0;       return;     }     int mid = (l + r) >> 1;     update(l, mid, ql, qr, 2 * node, i);     update(mid + 1, r, ql, qr, 2 * node + 1, i);     tree[node][i] = tree[2 * node][i] + tree[2 * node + 1][i];     return;   }   long long query(int l, int r, int ql, int qr, int node, int i) {     if (lazy[node][i]) {       tree[node][i] = (r - l + 1) - tree[node][i];       if (l != r) {         lazy[2 * node][i] ^= 1;         lazy[2 * node + 1][i] ^= 1;       }       lazy[node][i] = 0;     }     if (l > qr || r < ql) return 0;     if (ql <= l && r <= qr) return tree[node][i];     int mid = (l + r) >> 1;     return query(l, mid, ql, qr, 2 * node, i) +            query(mid + 1, r, ql, qr, 2 * node + 1, i);   }   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n;     cin >> n;     for (int i = 0; i < n; ++i) {       cin >> arr[i];     }     for (int i = 0; i < 20; ++i) {       build(0, n - 1, 1, i);     }     int q;     cin >> q;     while (q--) {       int id, l, r;       long long x;       cin >> id;       if (id == 1) {         cin >> l >> r;         l--, r--;         long long ans = 0;         for (int i = 0; i < 20; i++)           ans += (query(0, n - 1, l, r, 1, i)) * (1LL << i);         cout << ans <<   n ;       } else {         cin >> l >> r >> x;         l--, r--;         for (int i = 0; i < 20; ++i) {           if (x & (1LL << i)) {             update(0, n - 1, l, r, 1, i);           }         }       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     scanf( %d , &n);     while (n--) {       int c, sum;       scanf( %d %d , &c, &sum);       if (sum % c == 0)         cout << c * (sum / c * sum / c) << endl;       else {         int temp = sum / c;         long long int total = (temp * temp) * c;         int mod = sum % c;         total += mod * ((temp + 1) * (temp + 1));         total -= mod * (temp * temp);         cout << total << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e3 + 10;   const long double inf = 1e9 + 10;   bool use(long double &f, long double w, long double a);   int a[N], b[N];   int main() {     int n, m;     cin >> n >> m;     for (int i = 1; i <= n; ++i) {       cin >> a[i];     }     for (int i = 1; i <= n; ++i) {       cin >> b[i];     }     b[n + 1] = b[1];     long double lo = 0, hi = inf;     for (int t = 0; t < 60; ++t) {       long double mid = (lo + hi) / 2;       long double f = mid;       bool ok = true;       for (int i = 1; i <= n; ++i) {         if (!use(f, m, a[i])) {           ok = false;           break;         }         if (!use(f, m, b[i + 1])) {           ok = false;           break;         }       }       if (ok) {         hi = mid;       } else {         lo = mid;       }     }     if (lo > 1e9) {       cout << -1 << endl;     } else {       cout << fixed << setprecision(10) << lo << endl;     }   }   bool use(long double &f, long double w, long double a) {     if (f * a < f + w) {       return false;     }     f -= (f + w) / a;     return true;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     long long int n, m;     long long int b = 1;     while (t--) {       cin >> m >> n;       while (true) {         if ((m | b) <= n)           m = m | b;         else           break;         b <<= 1;       }       cout << m <<   n ;       b = 1;     }   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const long long int LINF = 0x3f3f3f3f3f3f3f3fll;   const long double pi = acos(-1);   const int MOD = 1e9 + 7;   int n, k;   long long int g[41];   long long int dp[(1 << 20)];   long long int dp2[(1 << 20)];   int t1, t2;   long long int Adam_Sendler() {     t1 = n / 2;     t2 = n - t1;     long long int r = 0;     for (long long int mask = 1; mask < (1ll << t1); mask++) {       for (long long int j = 0; j < t1; j++)         if (mask & (1ll << j)) {           long long int outra = mask - (1ll << j);           long long int r1 = __builtin_popcountll(dp[mask]);           long long int r2 = __builtin_popcountll(dp[outra]);           if (r2 > r1) dp[mask] = dp[outra];         }       bool click = true;       for (long long int j = 0; j < t1; j++)         if ((1ll << j) & mask)           if (((g[j] ^ mask) & mask)) click = false;       if (click) dp[mask] = mask;       long long int r1 = __builtin_popcountll(dp[mask]);       r = max(r, r1);     }     for (long long int mask = 1; mask < (1ll << t2); mask++) {       for (long long int j = 0; j < t2; j++)         if (mask & (1ll << j)) {           long long int outra = mask - (1ll << j);           long long int r1 = __builtin_popcountll(dp2[mask]);           long long int r2 = __builtin_popcountll(dp2[outra]);           if (r2 > r1) dp2[mask] = dp2[outra];         }       bool click = true;       for (long long int j = 0; j < t2; j++) {         if ((1ll << j) & mask) {           long long int m1 = g[j + t1];           long long int cara = mask << t1;           if ((m1 ^ cara) & cara) {             click = false;           }         }       }       if (click) {         dp2[mask] = mask;       }       long long int r1 = __builtin_popcountll(dp2[mask]);       if (r1 == 0)         cerr <<  mask               <<   ==   << mask << endl;       r = max(r, r1);     }     for (long long int mask = 0; mask < (1ll << t1); mask++) {       long long int tudo = (1ll << n) - 1;       for (long long int j = 0; j < t1; j++)         if ((1ll << j) & mask) tudo &= g[j];       tudo >>= t1;       long long int x = __builtin_popcountll(dp[mask]);       long long int y = __builtin_popcountll(dp2[tudo]);       r = max(r, x + y);     }     return r;   }   int main() {     scanf( %d%d , &n, &k);     for (int i = 0; i < n; i++) {       g[i] |= (1ll << i);       for (int j = 0; j < n; j++) {         int x;         scanf( %d , &x);         if (x) {           g[i] |= (1ll << j);         }       }     }     int m = Adam_Sendler();     cout << fixed << setprecision(10);     cout << (k * k * (m - 1)) / (2.0 * m) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long min(long long a, long long b) { return a < b ? a : b; }   int ceil(int a, int b) { return (a + b - 1) / b; }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     int n, k;     cin >> n >> k;     string s;     cin >> s;     if (!k) {       cout << s;       return 0;     }     if (s.length() == 1) {       cout << 0;       return 0;     }     int cnt = 0;     if (s[0] !=  1 ) s[0] =  1 , cnt++;     for (int i = 1; i <= s.length(); i++) {       if (cnt == k) break;       if (s[i] !=  0 ) s[i] =  0 , cnt++;     }     cout << s;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3100;   const int inf = 1e9 + 100;   struct node {     int a, b;   } l[maxn];   int s[maxn * 2 + 10], dp[maxn][maxn * 2 + 10], to[maxn];   int cmp(node a, node b) {     if (a.b != b.b)       return a.b < b.b;     else {       return a.a > b.a;     }   }   int main() {     int num, T, top, n;     scanf( %d , &T);     while (T--) {       scanf( %d , &n);       top = 0;       for (int i = 1; i <= n; i++) {         scanf( %d%d , &l[i].a, &l[i].b);         s[++top] = l[i].a;         s[++top] = l[i].b;       }       sort(s + 1, s + top + 1);       num = unique(s + 1, s + top + 1) - s - 1;       sort(l + 1, l + n + 1, cmp);       for (int i = 1; i <= n; i++) {         l[i].a = lower_bound(s + 1, s + num + 1, l[i].a) - s;         l[i].b = lower_bound(s + 1, s + num + 1, l[i].b) - s;       }       for (int i = 1; i <= n; i++) {         to[i] = 0;         for (int j = i - 1; j >= 1; j--) {           if (l[j].b < l[i].a) {             to[i] = j;             break;           }         }       }       for (int i = 1; i <= n; i++) {         for (int j = 1; j <= num; j++) {           dp[i][j] = 0;         }       }       for (int i = 1; i <= n; i++) {         for (int j = l[i].b; j >= 1; j--) {           dp[i][j] = dp[i - 1][j];           if (l[i].a >= j) {             dp[i][j] = max(dp[i][j], dp[to[i]][j] + dp[i - 1][l[i].a] + 1);           }         }       }       printf( %d n , dp[n][1]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 3000 + 10;   int n, a[N], dt[N], st[N][14], pos[N][14], mark[N], lg[N];   pair<int, int> nd[N];   bool cmp1(const pair<int, int> &a, const pair<int, int> &b) {     return a.first > b.first;   }   bool unfix(int l, int r) { return l > r; }   int query(int l, int r) {     int lg1 = lg[r - l + 1];     return max(st[l][lg1], st[r - (1 << lg1) + 1][lg1]);   }   bool ok(int a, int b, int c, int d, int e, int f) {     if (a != d) return d > a;     if (b != e) return e > b;     if (c != f) return f > c;     return 1;   }   int posquery(int l, int r) {     int lg1 = lg[r - l + 1];     if (st[l][lg1] > st[r - (1 << lg1) + 1][lg1])       return pos[l][lg1];     else       return pos[r - (1 << lg1) + 1][lg1];   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]), nd[i] = {a[i], i};     sort(nd + 1, nd + n + 1, cmp1);     for (int i = 1; i <= n; i++) a[i] = nd[i].first;     for (int i = n; i >= 1; i--)       dt[i] = a[i] - a[i + 1], st[i][0] = dt[i], pos[i][0] = i;     st[n][0] = dt[n];     pos[n][0] = n;     for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;     int LG = lg[n];     for (int j = 1; j <= LG; j++) {       for (int i = 1; i <= n; i++) {         st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);         if (st[i][j - 1] > st[i + (1 << (j - 1))][j - 1])           pos[i][j] = pos[i][j - 1];         else           pos[i][j] = pos[i + (1 << (j - 1))][j - 1];       }     }     int ans1 = 0, ans2 = 0, ans3 = 0;     int pos1 = 0, pos2 = 0, pos3 = 0;     for (int i = 1; i <= n; i++) {       for (int j = i + 1; j <= n; j++) {         int cnta = i, cntb = j - i;         if (cnta > 2 * cntb || cntb > 2 * cnta) continue;         if (unfix(max((cnta + 1) / 2, (cntb + 1) / 2),                   min(min(2 * cnta, 2 * cntb), n)))           continue;         int l = max(j + 1, j + max((cnta + 1) / 2, (cntb + 1) / 2)),             r = min(j + min(2 * cnta, 2 * cntb), n);         if (unfix(l, r)) continue;         int gap1 = a[i] - a[i + 1], gap2 = a[j] - a[j + 1], gap3 = query(l, r);         if (ok(ans1, ans2, ans3, gap1, gap2, gap3)) {           ans1 = gap1;           ans2 = gap2;           ans3 = gap3;           pos1 = i;           pos2 = j;           pos3 = posquery(l, r);         }       }     }     for (int i = 1; i <= n; i++) {       if (i <= pos1)         mark[nd[i].second] = 1;       else if (i <= pos2)         mark[nd[i].second] = 2;       else if (i <= pos3)         mark[nd[i].second] = 3;       else         mark[nd[i].second] = -1;     }     for (int i = 1; i <= n; i++) cout << mark[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int oo = 1000111000;   int n, k, a[30300], f[222][30300][2], ans[222][30300];   int main() {     int x;     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> x;       a[i] = a[i - 1] + x;     }     for (int i = 0; i <= k; i++)       for (int j = 0; j <= n; j++) f[i][j][0] = f[i][j][1] = -oo;     for (int i = 1; i <= k; i++)       for (int j = 1; j <= n; j++) {         ans[i][j] = ans[i][j - 1];         f[i][j][0] = f[i][j - 1][0];         f[i][j][1] = f[i][j - 1][1];         for (int jj = 0; jj < j; jj++) {           int sum = a[j] - a[jj];           int best = max(f[i - 1][jj][1] + sum, f[i - 1][jj][0] - sum);           if (i == 1) best = 0;           ans[i][j] = max(ans[i][j], best);           f[i][j][0] = max(f[i][j][0], best + sum);           f[i][j][1] = max(f[i][j][1], best - sum);         }       }     cout << ans[k][n] << endl;   }