malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int dp[2][100010], bit, n;   long long a[100010];   inline int comp(long long x, long long y) {     return (x % (1ll << (bit + 1))) > (y % (1ll << (bit + 1)));   }   int main() {     long long mx = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %lld , &a[i]);       mx = max(mx, a[i]);     }     int cur = 0;     memset(dp, 0x3f, sizeof(dp));     dp[0][0] = 0;     for (int i = 0; i < 63; i++) {       cur ^= 1;       int cnt1 = 0, cnt2 = 0;       memset(dp[cur], 0x3f, sizeof(dp[cur]));       for (int j = 0; j < n; j++) {         if (a[j] & (1ll << i)) cnt2++;       }       for (int j = 0; j <= n; j++) {         if (dp[cur ^ 1][j] != 0x3f3f3f3f) {           dp[cur][j + cnt2] =               min(dp[cur][j + cnt2], dp[cur ^ 1][j] + j - cnt1 + cnt2);           dp[cur][cnt1] =               min(dp[cur][cnt1], dp[cur ^ 1][j] + cnt1 + (n - j - cnt2));         }         if (a[j] & (1ll << i)) {           cnt1++;           cnt2--;         }       }       if ((1ll << i) > mx) break;       bit = i;       sort(a, a + n, comp);     }     printf( %d n , dp[cur][0]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k, ar[1000005], br[1000005];   int main() {     scanf( %lld %lld , &n, &k);     long long min_ = 0, max_ = 0;     for (int i = 1; i < n + 1; i++) {       ar[i] = br[i] = i;       min_ += i;       max_ += max((long long)i, n - i + 1);     }     if (k < min_) {       cout <<  -1 ;       return 0;     } else {       long long tot = min_;       long long t = n - 1;       k -= min_;       for (int i = 1; i < n + 1; i++) {         if (i >= n + 1 - i) break;         if (k > t) {           tot += t;           min_ += t;           swap(br[i], br[n + 1 - i]);         } else {           tot += k;           min_ += k;           swap(br[i], br[i + k]);           break;         }         k -= t;         t -= 2;       }       cout << tot <<   n ;     }     for (int i = 1; i < n + 1; i++) cout << ar[i] <<    ;     cout <<   n ;     for (int i = 1; i < n + 1; i++) cout << br[i] <<    ;     cout <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, a, n2, m2;     cin >> n >> m >> a;     if ((a * (n / a)) == n)       n2 = n / a;     else       n2 = (n / a) + 1;     if ((a * (m / a)) == m)       m2 = m / a;     else       m2 = (m / a) + 1;     cout << n2 * m2 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     sort(v.begin(), v.end());     int out = n;     int cur = n / 2;     for (int i = 0; i < n / 2 && cur < n; i++) {       while (true) {         if (v[i] * 2 <= v[cur]) {           out--, cur++;           break;         } else {           cur++;           if (cur == n) break;         }       }     }     cout << out << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long powmod(long long a, long long b) {     long long res = 1;     a %= 1000000007;     for (; b; b >>= 1) {       if (b & 1) res = res * a % 1000000007;       a = a * a % 1000000007;     }     return res;   }   struct node {     int x, y;   } f[2203456], ans[2203456];   bool cmp(node x, node y) { return x.x == y.x ? x.y > y.y : x.x < y.x; }   int main() {     int n, k;     cin >> n >> k;     int tot = 0;     for (int i = 0; i < n; i++) {       int a, b;       scanf( %d%d , &a, &b);       f[tot].x = a, f[tot].y = 1;       tot++;       f[tot].x = b, f[tot].y = -1;       tot++;     }     sort(f, f + tot, cmp);     int cnt = 0, ansi = 0, start = 0;     for (int i = 0; i < tot; i++) {       if (cnt == k - 1 && f[i].y == 1) {         start = f[i].x;       }       if (cnt == k && f[i].y == -1) {         ans[ansi].x = start;         ans[ansi].y = f[i].x;         ansi++;       }       cnt += f[i].y;     }     cout << ansi << endl;     for (int i = 0; i < ansi; i++) {       printf( %d %d n , ans[i].x, ans[i].y);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, t;     cin >> n >> t;     long long arr[n];     for (int i = 0; i < n; i++) {       cin >> arr[i];     }     sort(arr, arr + n);     float mxdif = INT_MIN;     for (int i = 1; i < n; i++) {       if (arr[i] - arr[i - 1] > mxdif) {         mxdif = arr[i] - arr[i - 1];       }     }     double z = (mxdif + 0.000) / 2;     double maxcnr = max(arr[0] - 0, t - arr[n - 1]);     printf( %.9lf , max(z, maxcnr));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long memo[4][1001][1001];   long long mp[1001][1001];   int n, m;   long long REC(int x, int y, int st) {     if (x >= n || y >= m || x < 0 || y < 0) return 0;     if (st == 0 && x == n - 1 && y == m - 1) return mp[x][y];     if (st == 1 && x == 0 && y == 0) return mp[x][y];     if (st == 2 && x == 0 && y == m - 1) return mp[x][y];     if (st == 3 && x == n - 1 && y == 0) return mp[x][y];     if (~memo[st][x][y]) return memo[st][x][y];     long long a, b;     if (st == 0) {       a = REC(x + 1, y, st) + mp[x][y];       b = REC(x, y + 1, st) + mp[x][y];       return memo[st][x][y] = a >= b ? a : b;     } else if (st == 1) {       a = REC(x - 1, y, st) + mp[x][y];       b = REC(x, y - 1, st) + mp[x][y];       return memo[st][x][y] = a >= b ? a : b;     } else if (st == 2) {       a = REC(x - 1, y, st) + mp[x][y];       b = REC(x, y + 1, st) + mp[x][y];       return memo[st][x][y] = a >= b ? a : b;     } else if (st == 3) {       a = REC(x + 1, y, st) + mp[x][y];       b = REC(x, y - 1, st) + mp[x][y];       return memo[st][x][y] = a >= b ? a : b;     }   }   long long check(int x, int y) {     long long a, b, aa, bb, res = 0, tmp;     b = memo[3][x + 1][y];     a = memo[1][x][y - 1];     bb = memo[2][x - 1][y];     aa = memo[0][x][y + 1];     tmp = aa + bb + a + b;     res = tmp >= res ? tmp : res;     b = memo[3][x][y - 1];     a = memo[1][x - 1][y];     bb = memo[2][x][y + 1];     aa = memo[0][x + 1][y];     tmp = aa + bb + a + b;     res = tmp >= res ? tmp : res;     return res;   }   int main() {     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) {         cin >> mp[i][j];       }     }     memset(memo, -1, sizeof memo);     REC(0, 0, 0);     REC(n - 1, m - 1, 1);     REC(n - 1, 0, 2);     REC(0, m - 1, 3);     long long maxi = 0;     for (int i = 1; i < n - 1; i++) {       for (int j = 1; j < m - 1; j++) {         long long tmp = check(i, j);         maxi = tmp >= maxi ? tmp : maxi;       }     }     cout << maxi << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = 1000000007;   const int NMAX = 5005;   const int INF = 2e5;   int n;   vector<int> arr;   vector<vector<int>> dp(NMAX, vector<int>(NMAX, INF));   int rec(int i, int j) {     if (dp[i][j] != INF) return dp[i][j];     if (i == j) return dp[i][j] = 0;     if (arr[i] == arr[j]) return dp[i][j] = 1 + rec(i + 1, j - 1);     return dp[i][j] = 1 + min(rec(i + 1, j), rec(i, j - 1));   }   void solve() {     cin >> n;     for (int i = 0; i < n; i++) {       int tmp;       cin >> tmp;       if (!i)         arr.push_back(tmp);       else if (tmp != arr[(int)(arr.size()) - 1])         arr.push_back(tmp);     }     n = (int)(arr.size());     cout << rec(0, n - 1) <<   n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, q;   int x, y, z;   int sum = 0;   map<int, pair<char, int> > ans;   map<int, pair<char, int> >::iterator t;   int main() {     scanf( %d%d , &n, &q);     ans[0] = { U , 0};     ans[n + 1] = { L , 0};     for (int i = 1; i <= q; i++) {       scanf( %d%d%c%c , &x, &y, &z, &z);       t = ans.lower_bound(x);       if (t->first == x) {         printf( 0 n );         continue;       }       if (z ==  L ) t--;       sum = abs(x - t->first);       if (t->second.first == z) sum += t->second.second;       ans[x] = {z, sum};       printf( %d n , sum);     }   }
#include <bits/stdc++.h>   using namespace std;   inline long long qr() {     register long long ret = 0, f = 0;     register char c = getchar();     while (c < 48 || c > 57) f |= c == 45, c = getchar();     while (c >= 48 && c <= 57) ret = ret * 10 + c - 48, c = getchar();     return f ? -ret : ret;   }   const int maxn = 1e5 + 5;   int n, m, p;   int dis[maxn];   long long sumd[maxn];   long long sumdata[maxn];   long long x[maxn];   long long y[maxn];   long long ans;   deque<int> q;   struct NODE {     int pos, time;     long long limit;     NODE() { ans = pos = time = 0; }     inline bool operator<(const NODE& a) const { return limit < a.limit; }     inline void scan() {       pos = qr();       time = qr();       limit = time - sumd[pos];     }   } data[maxn];   long long dp[101][maxn];   inline void wk() {     memset(dp, 5, sizeof dp);     dp[0][0] = 0;     deque<int>::iterator ita;     for (register int i = 0; i <= m; ++i) x[i] = i;     for (register int t = 1; t <= p; ++t) {       for (register int i = 0; i <= m; ++i) {         dp[t][i] = dp[t - 1][i];         y[i] = dp[t - 1][i] + sumdata[i];       }       q.clear();       for (register int i = 1; i <= m; ++i) {         q.push_back(i - 1);         ita = q.begin();         while (q.size() > 1 && y[*(ita + 1)] - y[*ita] <=                                    1ll * data[i].limit * ((*(ita + 1)) - (*ita)))           q.pop_front(), ita = q.begin();         register int j = q.front();         dp[t][i] = min(dp[t][i], dp[t - 1][j] + 1ll * (i - j) * data[i].limit -                                      (sumdata[i] - sumdata[j]));         ita = q.end() - 1;         while (q.size() > 1 &&                (y[*ita] - 1ll * y[*(ita - 1)]) * (i - (*ita)) >=                    1ll * (y[i] - y[*ita]) * ((*ita) - (*(ita - 1))))           q.pop_back(), ita = q.end() - 1;         ;       }     }     cout << dp[p][m] << endl;   }   int main() {     n = qr();     m = qr();     p = qr();     for (register int t = 2; t <= n; ++t) sumd[t] = (dis[t] = qr()) + sumd[t - 1];     for (register int t = 1; t <= m; ++t) data[t].scan();     sort(data + 1, data + m + 1);     for (register int t = 1; t <= m; ++t)       sumdata[t] = data[t].limit + sumdata[t - 1];     wk();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAX = 2e5 + 5;   int now;   int n, m, c, a[MAX];   int main() {     cin >> n >> m >> c;     c /= 2;     while (m--) {       int x;       scanf( %d , &x);       if (x <= c) {         for (int i = 1; i <= n; i++) {           if (a[i] == 0 || a[i] > x) {             a[i] = x;             printf( %d n , i);             fflush(stdout);             break;           }         }       } else {         for (int i = n; i >= 1; i--) {           if (a[i] == 0 || a[i] < x) {             a[i] = x;             printf( %d n , i);             fflush(stdout);             break;           }         }       }       now = 0;       for (int i = 1; i <= n; i++) {         if (a[i] != 0) now++;       }       if (now == n) break;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T a, T b) {     if (a == 0) return b;     return gcd(b % a, a);   }   template <typename T>   T pow(T a, T b, long long m) {     T ans = 1;     while (b > 0) {       if (b % 2 == 1) ans = (ans * a) % m;       b /= 2;       a = (a * a) % m;     }     return ans % m;   }   long long grundy(long long k) {     long long i = 1;     long long sum = 1;     while (sum <= k) {       i++;       sum += i;     }     return i - 1;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     long long n;     cin >> n;     long long stxor = 0;     for (long long i = 0; i < n; i++) {       long long num;       cin >> num;       stxor ^= grundy(num);     }     if (stxor == 0) {       cout <<  YES ;     } else {       cout <<  NO ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int distance(char a, char b) {     int d = abs(a - b);     int d1 = 26 - d;     return min(d, d1);   }   int main() {     int n;     string A;     int cursor;     cin >> n >> cursor >> A;     cursor--;     n--;     cursor = min(cursor, n - cursor);     int middle = n / 2;     int leftmost = cursor, rightmost = cursor;     for (int i = cursor; i <= middle; i++)       if (A[i] != A[n - i]) rightmost = i;     for (int i = cursor; i >= 0; i--)       if (A[i] != A[n - i]) leftmost = i;     int vertical_moves = 0;     for (int i = leftmost; i <= rightmost; i++)       vertical_moves += min(abs(A[i] - A[n - i]), 26 - abs(A[i] - A[n - i]));     int horizontal_moves = min((cursor - leftmost) + (rightmost - leftmost),                                (rightmost - cursor) + (rightmost - leftmost));     int total_moves = horizontal_moves + vertical_moves;     cout << total_moves;     return 0;   }
#include <bits/stdc++.h>   int n, m, x, tab[11][1001][1001], mx[11][1001][1001], i = 1, j = 1, ij = 1;   char a[1001], b[1001], q;   inline int max(int a, int b) {     if (a >= b) return a;     return b;   }   int main() {     scanf( %d %d %d , &n, &m, &x);     q = getchar();     while (i <= n) {       a[i] = getchar();       i++;     }     q = getchar();     i = 1;     while (i <= m) {       b[i] = getchar();       i++;     }     i = 1;     while (ij <= x) {       while (i <= n) {         while (j <= m) {           if (a[i] == b[j])             tab[ij][i][j] =                 max(tab[ij][i - 1][j - 1] + 1, mx[ij - 1][i - 1][j - 1] + 1);           else             tab[ij][i][j] = 0;           mx[ij][i][j] = max(mx[ij][i - 1][j], mx[ij][i][j - 1]);           mx[ij][i][j] = max(mx[ij][i][j], tab[ij][i][j]);           j++;         }         i++, j = 1;       }       i = 1, j = 1, ij++;     }     printf( %d , mx[x][n][m]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       long long int arr[n];       set<long long int> s;       for (int i = 0; i < n; i++) cin >> arr[i];       for (int i = 0; i < n; i++) {         s.insert(arr[i]);       }       if (s.size() == 1)         cout << n <<   n ;       else         cout << 1 <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<long long int> primes;   void SOE(long long int n) {     primes.erase(primes.begin(), primes.end());     bool prime[n + 1];     memset(prime, true, sizeof(prime));     for (long long int p = 2; p * p <= n; p++) {       if (prime[p] == true) {         for (long long int i = p * p; i <= n; i += p) prime[i] = false;       }     }     for (long long int p = 2; p <= n; p++)       if (prime[p]) {         primes.push_back(p);       }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     long long int i, c = 0, j, k, t, f = 0, q, n, f1 = 0, y, x, m, l = 0, r, p;     string s, s1, s2;     cin >> t;     while (t--) {       cin >> n;       long long int a[n];       for (i = 0; i < n; i++) {         cin >> a[i];       }       a[0] < a[n - 1] ? cout <<  YES n  : cout <<  NO n ;     }   }
#include <bits/stdc++.h>   using namespace std;   string ans =   ;   set<int> s[26];   map<int, bool> vis;   void solve(char a) {     ans += a;     vis[a -  a ] = true;     for (auto it : s[a -  a ]) {       if (!vis[it -  a ]) {         solve(it);       }     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       string str;       cin >> str;       int n = str.length();       string alp =  abcdefghijklmnopqrstuvwxyz ;       if (n == 1) {         cout <<  YES  << endl;         cout << alp << endl;         continue;       }       for (int i = 0; i < 26; i++) s[i].clear();       vis.clear();       ans =   ;       for (long long int i = 1; i < n - 1; i++) {         s[str[i] -  a ].insert(str[i - 1]);         s[str[i] -  a ].insert(str[i + 1]);       }       s[str[0] -  a ].insert(str[1]);       s[str[n - 1] -  a ].insert(str[n - 2]);       int flag = 0, cnt = 0;       char st;       for (long long int i = 0; i < 26; i++) {         if (s[i].size() > 2) {           flag = 1;         } else if (s[i].size() == 1) {           cnt++;           st = alp[i];         }       }       if (flag or cnt < 2) {         cout <<  NO  << endl;         continue;       }       cout <<  YES  << endl;       solve(st);       for (int i = 0; i < 26; i++) {         if (!vis[alp[i] -  a ]) ans += alp[i];       }       cout << ans << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxF = (long long)1e9 * 1e9;   const int maxn = 100, maxm = 100, maxlen = maxn + maxm + 1;   int n, m, len, map[maxn + 5][maxm + 5], pri[maxlen], cir[maxlen];   long long dp[maxlen][(maxlen >> 1) + 1], k;   long long cal() {     memset(dp, 0, sizeof(dp));     dp[0][1] = 1;     int up = len >> 1;     for (int p = 1; p < len; p++)       for (int d = 0; d <= up; d++) {         if (cir[p] != 1 && d < up) dp[p][d] += dp[p - 1][d + 1];         if (cir[p] != -1 && d > 0) dp[p][d] += dp[p - 1][d - 1];         if (dp[p][d] >= maxF) dp[p][d] = maxF;       }     return dp[len - 1][0];   }   int main() {     cin >> n >> m >> k;     memset(pri, -1, sizeof(pri));     for (int i = 0, t; i < n; i++)       for (int j = 0; j < m; j++) {         cin >> t;         pri[i + j] = pri[i + j] == -1 ? t : min(pri[i + j], t);       }     len = n + m - 1;     memset(cir, 0, sizeof(cir));     cir[0] = 1;     cir[len - 1] = -1;     for (int t = 1; t < len - 1; t++) {       int pos, minPri = 100000000;       for (int p = 1; p < len - 1; p++)         if (cir[p] == 0 && minPri > pri[p]) pos = p, minPri = pri[p];       cir[pos] = 1;       long long s = cal();       if (k > s) k -= s, cir[pos] = -1;     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < m; j++) cout << (cir[i + j] == 1 ?  (  :  ) );       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T checkmin(T &a, T b) {     return (a < b) ? a : a = b;   }   template <class T>   inline T checkmax(T &a, T b) {     return (a > b) ? a : a = b;   }   template <class T>   T GCD(T a, T b) {     if (a < 0) return GCD(-a, b);     if (b < 0) return GCD(a, -b);     return (a == 0) ? b : GCD(b % a, a);   }   template <class T>   T LCM(T a, T b) {     if (a < 0) return LCM(-a, b);     if (b < 0) return LCM(a, -b);     return (a == 0 || b == 0) ? 0 : a / GCD(a, b) * b;   }   template <class T>   inline T sqr(T X) {     return X * X;   }   namespace Poor {   const int MaxiL = 1000005;   const int MaxiN = 205;   const int MaxiM = 205;   const int MaxiQL = 3005;   int N, L;   char St[MaxiL];   char Rule[MaxiQL];   int SeqX[MaxiM], SeqY[MaxiM];   int Stack[MaxiL];   inline bool Same(const int a, const int b, const int c, const int d) {     if (b - a != d - c) return 0;     for (register int i = 0; i < b - a; ++i)       if (St[a + i] != Rule[c + i]) return 0;     return 1;   }   void Run() {     gets(St);     L = strlen(St);     scanf( %d n , &N);     for (int i = 0; i < N; ++i) {       gets(Rule);       int M = 0;       for (int p = 0; Rule[p] !=   0 ; ++p)         if (isalpha(Rule[p])) {           int q = p;           for (; isalpha(Rule[q]); ++q)             ;           SeqX[M] = p;           SeqY[M] = q;           ++M;           p = q - 1;         }       register int res = 0;       register int *Top = Stack;       *Top = 0;       ++Top;       for (register int p = 0, q = 0; p < L; p = q + 1) {         for (q = p; St[q] !=  > ; ++q)           ;         if (St[p + 1] ==  / ) {           --Top;           continue;         }         if (St[q - 1] ==  / ) {           if (*(Top - 1) >= M - 1 && Same(p + 1, q - 1, SeqX[M - 1], SeqY[M - 1]))             ++res;           continue;         }         *Top = *(Top - 1);         if (*Top < M - 1) {           if (Same(p + 1, q, SeqX[*Top], SeqY[*Top])) ++*Top;         } else {           if (Same(p + 1, q, SeqX[M - 1], SeqY[M - 1])) ++res;         }         ++Top;       }       printf( %d n , res);     }   }   }  // namespace Poor   int main() {     Poor::Run();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long seg[200005], h[100005], dp[100005];   int n, cnt, arr[100005], a[100005], b[100005];   map<int, int> mn;   map<int, long long> tot;   inline void modify(int pos, long long x) {     for (seg[pos += cnt] += x; pos; pos >>= 1)       seg[pos >> 1] = max(seg[pos], seg[pos ^ 1]);   }   inline long long query(int l, int r) {     long long res = 0;     for (l += cnt, r += cnt; l < r; l >>= 1, r >>= 1) {       if (l & 1) res = max(res, seg[l++]);       if (r & 1) res = max(res, seg[--r]);     }     return res;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       int a, b, h;       scanf( %d%d%d , &a, &b, &h);       if (mn.count(b))         mn[b] = min(mn[b], a);       else         mn[b] = a;       tot[b] += h;     }     for (map<int, int>::iterator it = mn.begin(); it != mn.end(); it++) {       arr[cnt] = it->first;       a[cnt] = it->second;       b[cnt] = it->first;       h[cnt++] = tot[it->first];     }     long long ans = 0;     for (int i = 0; i < cnt; i++) {       dp[i] = query(lower_bound(arr, arr + cnt, a[i] + 1) - arr,                     lower_bound(arr, arr + cnt, b[i]) - arr + 1) +               h[i];       modify(lower_bound(arr, arr + cnt, b[i]) - arr, dp[i]);       ans = max(ans, dp[i]);     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};   const int Nmax = 200010;   int n, k;   long long a[20000], pref[20000];   long long f[60][500][500];   long long diff[500][2];   int main() {     ios::sync_with_stdio(0);     cin >> n >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];       pref[i] = pref[i - 1] + a[i];     };     for (int i = 0; i <= n; i++) diff[i][0] = diff[i][1] = -2000000000;     for (int i = 1; i <= n; i++)       for (int j = i; j <= n; j++) {         diff[j][0] = max(diff[j][0], pref[j] - pref[i - 1]);         diff[j][1] = max(pref[i - 1] - pref[j], diff[j][1]);       };     long long answer = 0;     for (int t = 2; t <= k; t++) {       for (int i = 1; i <= n; i++)         for (int j = i; j <= n; j++)           f[t][i][j] = max(diff[i - 1][0] - pref[j] + pref[i - 1],                            diff[i - 1][1] - pref[i - 1] + pref[j]);       for (int i = 0; i < n; i++) diff[i][0] = diff[i][1] = -2000000000;       for (int i = 1; i <= n; i++)         for (int j = i; j <= n; j++) {           diff[j][0] = max(diff[j][0], f[t][i][j] + pref[j] - pref[i - 1]);           diff[j][1] = max(diff[j][1], f[t][i][j] - pref[j] + pref[i - 1]);         };     };     for (int i = 1; i <= n; i++)       for (int j = i; j <= n; j++) answer = max(answer, (long long)f[k][i][j]);     cout << answer << endl;     return 0;   }