malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   struct _IO {     _IO() {       ios::sync_with_stdio(0);       cin.tie(0);       cout.tie(0);     }   } _io;   const long long maxn = 3e5 + 10, mod = 1e9 + 7;   template <typename... T>   long long madd(T... args) {     long long res = 0;     for (auto i : {args...}) {       res = (res + ((i % mod + mod) % mod)) % mod;     }     return res % mod;   }   template <typename... T>   long long mmul(T... args) {     long long res = 1;     for (auto i : {args...}) {       res = (res * ((i % mod + mod) % mod)) % mod;     }     return res % mod;   }   long long qpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res *= a;       a *= a;       b >>= 1;     }     return res;   }   long long mpow(long long a, long long b) {     long long res = 1;     while (b) {       if (b & 1) res = (res * a) % mod;       a = (a * a) % mod;       b >>= 1;     }     return res % mod;   }   long long _inv(long long x) { return mpow(x, mod - 2); }   long long dsufa[maxn], fa[maxn], cnt[maxn];   vector<pair<long long, long long> > query;   vector<long long> edge[maxn];   deque<long long> res;   long long findfa(long long x) {     return x == dsufa[x] ? x : (dsufa[x] = findfa(dsufa[x]));   }   bool mer(long long x, long long y) {     x = findfa(x);     y = findfa(y);     if (x == y) return 0;     dsufa[x] = y;     return 1;   }   void dfs(long long u, long long p) {     for (auto v : edge[u]) {       if (v == p) continue;       fa[v] = u;       dfs(v, u);     }   }   void path(long long x, long long y) {     deque<long long> a(1, x), b(1, y);     res.clear();     while (fa[x] != x) {       x = fa[x];       a.push_front(x);     }     while (fa[y] != y) {       y = fa[y];       b.push_front(y);     }     long long len = min(a.size(), b.size());     long long idx = 0;     for (; idx < len && a[idx] == b[idx]; ++idx)       ;     res.push_back(a[idx - 1]);     for (long long i = idx; i < b.size(); ++i) res.push_back(b[i]);     for (long long i = idx; i < a.size(); ++i) res.push_front(a[i]);   }   void solve() {     long long n, m;     cin >> n >> m;     for (long long i = 1; i <= n; ++i) dsufa[i] = i;     for (long long i = 1; i <= m; ++i) {       long long x, y;       cin >> x >> y;       if (mer(x, y)) {         edge[x].push_back(y);         edge[y].push_back(x);       }     }     dfs(1, 1);     long long q;     cin >> q;     for (long long i = 1; i <= q; ++i) {       long long x, y;       cin >> x >> y;       query.push_back({x, y});       ++cnt[x];       ++cnt[y];     }     bool ok = 1;     long long oddcnt = 0;     for (long long i = 1; i <= n; ++i) {       if (cnt[i] & 1) {         ok = 0;         ++oddcnt;       }     }     if (!ok) {       cout <<  NO n ;       cout << oddcnt / 2 <<   n ;     } else {       cout <<  YES n ;       for (auto vec : query) {         long long x = vec.first, y = vec.second;         path(x, y);         cout << res.size() <<   n ;         for (long long i = 0; i < res.size(); ++i)           cout << res[i] <<    n [i == (long long)res.size() - 1];       }     }   }   int main() {     long long T = 1;     while (T--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long mod = 1e9 + 7;   int a[2010];   bool vis[2010];   long long sum;   vector<int> edge[2010];   long long DFS(int u, int d, int fa, int root) {     long long ret = 1;     for (int i = 0; i < edge[u].size(); i++) {       int v = edge[u][i];       if (v != fa && a[root] >= a[v] && a[root] - a[v] <= d &&           !(a[root] == a[v] && v > root)) {         ret = ret * (DFS(v, d, u, root) + 1) % mod;       }     }     return ret;   }   int main() {     int n, d;     scanf( %d %d , &d, &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     for (int i = 1; i < n; i++) {       int a, b;       scanf( %d %d , &a, &b);       edge[a].push_back(b);       edge[b].push_back(a);     }     for (int i = 1; i <= n; i++) {       sum = (sum + DFS(i, d, -1, i)) % mod;       vis[i] = true;     }     printf( %lld n , sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXK = 21;   long long dp[(1 << MAXK)];   int n, m;   string s;   long long d[MAXK][MAXK];   long long solve(int mask) {     if (mask == ((1 << m) - 1)) return 0ll;     long long &ret = dp[mask];     if (ret != -1) return ret;     ret = 1e10;     long long cost = __builtin_popcount(mask);     for (int i = 0; i < m; ++i) {       if ((mask & (1 << i))) continue;       long long now = solve(mask | (1 << i));       for (int j = 0; j < m; ++j) {         if (i == j) continue;         if ((mask & (1 << j)))           now += cost * d[i][j];         else           now -= cost * d[i][j];       }       ret = min(ret, now);     }     return ret;   }   int main() {     memset(dp, -1, sizeof(dp));     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n >> m;     cin >> s;     for (int i = 0; i < n - 1; ++i) {       int a = s[i] -  a ;       int b = s[i + 1] -  a ;       d[a][b]++;       if (a != b) d[b][a]++;     }     return cout << solve(0) <<   n , 0;   }
#include <bits/stdc++.h>   using namespace std;   int main(int argc, char *argv[]) {     long long n, k;     cin >> n >> k;     long long len = 2 * n;     long long temp = k * 2 / len;     long long ans = k * 2 % len == 0 ? temp : temp + 1;     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   T min(T a, T b, T c) {     return min(a, min(b, c));   }   template <class T>   T max(T a, T b, T c) {     return max(a, max(b, c));   }   template <class T>   void read(vector<T> &v) {     T x;     cin >> x;     v.push_back(x);   }   int n, a, b, x[1000005];   long long ans = (long long)1000000007 * 1000000007, f[1000005][3];   bool isPrime(int n) {     if (n < 2) return false;     int lim = sqrt(n);     for (int i = (2); i <= (lim); ++i) {       if (n % i == 0) return false;     }     return true;   }   void Solve(int p) {     if (!isPrime(p)) return;     for (int i = (0); i <= (n); ++i) {       for (int k = (0); k <= (2); ++k)         f[i][k] = (long long)1000000007 * 1000000007;     }     f[0][0] = 0;     for (int i = (1); i <= (n); ++i) {       if (x[i] % p == 0) {         f[i][0] = f[i - 1][0];         f[i][2] = min(f[i - 1][1], f[i - 1][2]);       } else if ((x[i] - 1) % p == 0 || (x[i] + 1) % p == 0) {         f[i][0] = f[i - 1][0] + b;         f[i][2] = min(f[i - 1][1], f[i - 1][2]) + b;       }       f[i][1] = min(f[i - 1][0], f[i - 1][1]) + a;     }     for (int k = (0); k <= (2); ++k) ans = min(ans, f[n][k]);   }   void Try(int p) {     int lim = sqrt(p);     for (int i = (1); i <= (lim); ++i) {       if (p % i == 0) {         Solve(i);         Solve(p / i);       }     }   }   int main() {     scanf( %d%d%d , &n, &a, &b);     for (int i = (1); i <= (n); ++i) scanf( %d , &x[i]);     for (int d = (-1); d <= (1); ++d) {       Try(x[1] + d);       Try(x[n] + d);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e5 + 10;   long long a[maxn];   pair<long long, long long> st[maxn];   long long top, n, m, k, l, r, rec, rec2;   long long ans;   int main() {     cin >> n >> k >> m;     for (long long i = 1; i <= n; i++) cin >> a[i];     for (long long i = 1; i <= n; i++) {       if (!top || st[top].first != a[i])         st[++top] = make_pair(a[i], 1);       else         st[top].second = (st[top].second + 1) % k;       if (st[top].second == 0) top--;     }     for (long long i = 1; i <= top; i++) {       rec += st[i].second;     }     l = 1, r = top;     while (l < r && st[l].first == st[r].first &&            (st[l].second + st[r].second) % k == 0) {       rec2 += st[l].second + st[r].second;       l++;       r--;     }     if (l == r) {       if (st[l].second * m % k == 0)         ans += m * rec - rec2 * (m) - (st[l].second * m / k * k);       else         ans += m * rec - rec2 * (m - 1) - (st[l].second * m / k * k);     } else {       if (st[l].first == st[r].first)         rec2 += (st[l].second + st[r].second) / k * k;       ans = m * rec - rec2 * (m - 1);     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   vector<int> t;   void set_val(int p, int x) {     t[p + int(t.size()) / 2] = x;     for (p = (p + int(t.size()) / 2) / 2; p > 0; p /= 2) {       t[p] = max(t[2 * p], t[2 * p + 1]);     }   }   int leftmost(int left, int y) {     if (left >= int(t.size()) / 2) return -1;     int c = left + int(t.size()) / 2;     if (t[c] > y) return left;     for (; c > 1 && !(c % 2 == 0 && t[c + 1] > y);) c /= 2;     if (c == 1) return -1;     ++c;     for (; c < int(t.size()) / 2;) {       if (t[2 * c] > y)         c *= 2;       else         c = 2 * c + 1;     }     return c - int(t.size()) / 2;   }   int main() {     ios_base::sync_with_stdio(false);     int n;     cin >> n;     vector<string> req(n);     vector<int> x(n), y(n);     for (int i = 0; i < n; ++i) {       cin >> req[i] >> x[i] >> y[i];     }     vector<int> di = x;     sort(di.begin(), di.end());     di.resize(unique(di.begin(), di.end()) - di.begin());     map<int, int> id;     vector<set<int> > c(di.size());     for (int i = 0; i < int(di.size()); ++i) {       id[di[i]] = i;       c[i].insert(-1);     }     int k = 1;     for (; k < int(di.size());) k *= 2;     t = vector<int>(2 * k, -1);     for (int i = 0; i < n; ++i) {       if (req[i] ==  add ) {         c[id[x[i]]].insert(y[i]);         set<int>::iterator last = c[id[x[i]]].end();         --last;         set_val(id[x[i]], *last);       } else if (req[i] ==  remove ) {         c[id[x[i]]].erase(y[i]);         set<int>::iterator last = c[id[x[i]]].end();         --last;         set_val(id[x[i]], *last);       } else if (req[i] ==  find ) {         int a = leftmost(id[x[i]] + 1, y[i]);         if (a == -1)           cout << -1 << endl;         else           cout << di[a] <<     << *c[a].upper_bound(y[i]) << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int INF = 1000000001;   const long long LINF = 1000000000000000001LL;   int len[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};   string days[] = { monday ,  tuesday ,   wednesday ,  thursday ,                     friday ,  saturday ,  sunday };   int dayId(string s) { return find(days, days + 7, s) - days; }   int main() {     ios_base::sync_with_stdio(0);     string a, b;     cin >> a >> b;     int aid = dayId(a), bid = dayId(b);     for (int i = (0); i <= (10); ++i) {       if ((aid + len[i]) % 7 == bid) {         cout <<  YES ;         return 0;       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, k, a[200], m;   int main() {     cin >> n >> m;     k = n;     for (int i = 1; i <= n; i++) cin >> a[i];     sort(a + 1, a + 1 + n);     while (m > 0) m -= a[n--];     if (n < 0) n = 0;     cout << k - n << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     cin >> n >> k;     vector<string> words(4 * n);     string S;     int aabb = 0, abab = 0, abba = 0, aaaa = 0, NO = 0;     for (int i = (0); i < (4 * n); ++i) {       cin >> S;       int size = S.size();       int Kth = 0;       string newS =   ;       bool canRhyme = false;       for (int j = size - 1; j >= 0; --j) {         if (S[j] ==  a  || S[j] ==  e  || S[j] ==  i  || S[j] ==  u  ||             S[j] ==  o ) {           ++Kth;         }         newS = S[j] + newS;         if (Kth == k) {           canRhyme = true;           break;         }       }       if (canRhyme)         words[i] = newS;       else {         stringstream out;         out << i;         words[i] = out.str();       }     }     for (int i = 0; i < n; ++i) {       if (words[4 * i].compare(words[4 * i + 1]) == 0 &&           words[4 * i + 2].compare(words[4 * i + 3]) == 0) {         if (words[4 * i].compare(words[4 * i + 2]) == 0) {           ++aaaa;           ++aabb;           ++abab;           ++abba;         } else           ++aabb;       } else if (words[4 * i].compare(words[4 * i + 2]) == 0 &&                  words[4 * i + 1].compare(words[4 * i + 3]) == 0)         ++abab;       else if (words[4 * i].compare(words[4 * i + 3]) == 0 &&                words[4 * i + 1].compare(words[4 * i + 2]) == 0)         ++abba;       else         ++NO;     }     if (NO)       cout <<  NO  << endl;     else {       if (aaaa == n)         cout <<  aaaa  << endl;       else if (aabb == n)         cout <<  aabb  << endl;       else if (abab == n)         cout <<  abab  << endl;       else if (abba == n)         cout <<  abba  << endl;       else         cout <<  NO  << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MN = 1e6 + 44;   const int mod = 1e9 + 9, b = 8888;   long long rcshnt[MN];   int n;   char a[MN];   bool check(int len) {     ;     long long curr = 0;     for (int i = 0; i < len; ++i) curr = (curr * b + a[i]) % mod;     long long pre = curr;     ;     for (int i = len; i < n - 1; ++i) {       ;       ;       curr = ((curr - a[i - len] * rcshnt[len - 1]) % mod * b + a[i]) % mod;       ;       if ((pre - curr) % mod == 0) {         ;         return true;       }     };     return false;   }   int main() {     scanf( %s , a);     n = strlen(a);     rcshnt[0] = 1;     for (int i = 1; i < MN; ++i) {       rcshnt[i] = rcshnt[i - 1] * b % mod;     }     for (int i = 0; i < 10; ++i)       ;     vector<int> pre;     long long ph = 0, sh = 0;     for (int i = 0; i < n - 1; ++i) {       ph = (ph * b + a[i]) % mod;       sh = (sh + a[n - i - 1] * rcshnt[i]) % mod;       if (ph == sh) {         pre.push_back(i + 1);         ;       }     }     int low = 1, high = n - 1, ans = 0;     while (low <= high) {       int med = (low + high) / 2;       if (check(med)) {         ans = med;         low = med + 1;       } else         high = med - 1;     }     int res = -1;     for (auto c : pre) {       if (c <= ans) res = max(res, c);     }     if (res <= 0) {       printf( Just a legend n );     } else {       a[res] = 0;       printf( %s n , a);     }   }
#include <bits/stdc++.h>   using namespace std;   long long MOD = 1e9 + 7;   long long add(long long a, long long b) {     a += b;     if (a >= MOD) a -= MOD;     if (a < 0) a += MOD;     return a;   }   long long mul(long long a, long long b) { return a * 1ll * b % MOD; }   void solve() {     long long n;     string s;     cin >> n >> s;     string ans = string(n,  0 );     long long bal = 0;     for (long long i = 0; i < n; i++) {       if (s[i] ==  ) ) bal--;       ans[i] = char( 0  + (bal & 1));       if (s[i] ==  ( ) bal++;     }     cout << ans << endl;   }   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long t = 1;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve();   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     solve();     return 0;   }   void sd() {     int n;     cin >> n;     n = 2 * n;     vector<vector<pair<int, long long>>> adj(n + 1);     vector<int> cnt(n + 1, 1);     int i;     for (i = 0; i < n - 1; i++) {       int u, v, t;       cin >> u >> v >> t;       adj[u].emplace_back(v, t);       adj[v].emplace_back(u, t);     }     vector<int> parent(n + 1, -1), topo(1, 1);     vector<long long> cost(n + 1, 0);     parent[1] = 1;     stack<int> st;     st.push(1);     while (!st.empty()) {       int u = st.top();       st.pop();       for (auto [v, c] : adj[u]) {         if (parent[v] == -1) {           parent[v] = u;           cost[v] = c;           topo.push_back(v);           st.push(v);         }       }     }     reverse(topo.begin(), topo.end());     long long G, B;     G = B = 0;     for (int u : topo) {       cnt[parent[u]] += cnt[u];       B += cost[u] * min(cnt[u], n - cnt[u]);       G += cost[u] * (cnt[u] % 2 == 1);     }     cout << G <<     << B << endl;   }   void solve() {     int t;     cin >> t;     while (t--) {       sd();     }   }
#include <bits/stdc++.h>   using namespace std;   int a[1000009];   int main() {     int t;     scanf( %d , &t);     while (t--) {       int n;       scanf( %d , &n);       int v = 0;       int min1 = 0x3f3f3f3f;       for (int i = 1; i <= n; ++i) {         scanf( %d , &a[i]);       }       for (int i = n; i >= 1; --i) {         min1 = min(a[i], min1);         if (min1 < a[i]) {           v++;         }       }       printf( %d n , v);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int dp[100005], ans[100005];   struct node {     int x, y, t;   } a[100005];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout.tie(nullptr);     int r, n;     cin >> r >> n;     a[0].x = a[0].y = 1;     a[0].t = 0;     for (int i = 1; i <= n; i++) {       cin >> a[i].t >> a[i].x >> a[i].y;       dp[i] = -n;       for (int j = i - 1; j >= 0; j--) {         if (a[i].t - a[j].t >= 2 * (r - 1)) {           dp[i] = max(dp[i], ans[j] + 1);           break;         }         if (a[i].t - a[j].t >= abs(a[i].x - a[j].x) + abs(a[i].y - a[j].y)) {           dp[i] = max(dp[i], dp[j] + 1);         }       }       ans[i] = max(dp[i], ans[i - 1]);     }     cout << ans[n] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200100;   const int oo = (N << 1);   int dp[19][N][2][2];   int d1[N], d2[2][N];   char mat[2][N];   int cal(int x1, int y1, int x2, int y2) {     if (x1 == x2) {       return d2[x2][y1];     }     return min(d1[y1] + d2[x2][y1], d2[x1][y1] + d1[y2]);   }   int p[N << 1];   int fi(int x) { return x == p[x] ? x : p[x] = fi(p[x]); }   void update(int x, int y) {     int f1 = fi(x);     int f2 = fi(y);     if (f1 != f2) {       p[f1] = f2;     }   }   void init(int n) {     for (int i = 0; i < (n << 1); i++) {       p[i] = i;     }     for (int i = 0; i < n; i++) {       if (mat[0][i] ==  X  || mat[1][i] ==  X ) {         d1[i] = oo;       } else {         update(i, i + n);         d1[i] = 1;       }       for (int j = 0; j < 2; j++) {         if (mat[j][i] ==  X  || mat[j][i + 1] ==  X ) {           d2[j][i] = oo;         } else {           if (i + 1 < n) {             update(j * n + i, j * n + i + 1);           }           d2[j][i] = 1;         }       }     }     for (int i = 0; i < n; i++) {       for (int j = 0; j < 2; j++) {         for (int k = 0; k < 2; k++) {           if (j == k) {             dp[0][i][j][k] = 0;           } else {             dp[0][i][j][k] = d1[i];           }         }       }     }     for (int d = 0; d < 18; d++) {       for (int i = 0; i < n; i++) {         for (int x1 = 0; x1 < 2; x1++) {           for (int y1 = 0; y1 < 2; y1++) {             dp[d + 1][i][x1][y1] = oo;             for (int x2 = 0; x2 < 2; x2++) {               for (int y2 = 0; y2 < 2; y2++) {                 dp[d + 1][i][x1][y1] =                     min(dp[d + 1][i][x1][y1],                         dp[d][i][x1][x2] + dp[d][i + (1 << d)][y2][y1] +                             cal(x2, i + (1 << d) - 1, y2, i + (1 << d)));               }             }           }         }       }     }   }   int query(int x1, int y1, int x2, int y2) {     int d = (y2 - y1);     int r[2] = {dp[0][y1][x1][0], dp[0][y1][x1][1]};     int x = y1;     for (int i = 18; i && (d + 1); i--) {       while ((1 << i) & (d + 1)) {         int tmp[2];         tmp[0] = min(r[0] + dp[i][x][0][0], r[1] + dp[i][x][1][0]);         tmp[1] = min(r[0] + dp[i][x][0][1], r[1] + dp[i][x][1][1]);         r[0] = tmp[0];         r[1] = tmp[1];         d -= (1 << i);         d++;         x += (1 << i);         x--;       }     }     return r[x2];   }   int main() {     int n, m;     scanf( %d%d , &n, &m);     scanf( %s%s , mat[0], mat[1]);     init(n);     while (m--) {       int x, y;       scanf( %d%d , &x, &y);       x--;       y--;       pair<int, int> q1 = make_pair(x % n, x / n), q2 = make_pair(y % n, y / n);       if (q1 > q2) {         swap(q1, q2);       }       int ans = -1;       if (fi(x) == fi(y)) {         ans = query(q1.second, q1.first, q2.second, q2.first);       }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int a;     cin >> a;     int n, s, t;     while (a--) {       cin >> n >> s >> t;       cout << max(n - s + 1, n - t + 1) <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int query(long long a, long long b) {     cout << a <<     << b << endl << flush;     int ans;     cin >> ans;     return ans;   }   int main() {     long long n;     cin >> n;     long long a = 0, b = 0, sa = 1, sb = 1;     int ans = query(a + sa, b + sb);     while (ans) {       switch (ans) {         case 1:           a += sa;           sa = min(sa * 2, n - a);           break;         case 2:           b += sb;           sb = min(sb * 2, n - b);           break;         case 3:           sa = max(sa / 2, 1ll);           sb = max(sb / 2, 1ll);           break;         default:           return 0;       }       ans = query(a + sa, b + sb);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   int a[maxn], cnt[maxn];   int main() {     int n, m, k;     cin >> n >> m >> k;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     int l = 1, r = 1;     int ans = 0;     cnt[a[1]]++;     while (l <= n) {       if (((r - l + 1) - cnt[a[l]]) <= k) {         ans = max(ans, cnt[a[l]]);         if (r < n) {           r++;           cnt[a[r]]++;         } else {           cnt[a[l]]--;           l++;         }       } else {         cnt[a[l]]--;         l++;       }     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #define ll long long   #define MOD (ll)1000000007   #define pii pair<int,int>   #define pll pair<ll,ll>   #define UNIQUE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());   ll GCD(ll a,ll b){return b?GCD(b,a%b):a;}   ll power(ll a,ll b){ll ret=1;while(b){if(b%2)ret=ret*a%MOD;a=a*a%MOD;b/=2;}return ret;}      ll a[200001];      int main()   {       int tc;       scanf( %d ,&tc);       while(tc--)       {           int n;           scanf( %d ,&n);           for(int i=1;i<=n;i++)               scanf( %lld ,&a[i]);           vector<ll> v;           ll temp = 0;           a[0] = a[n+1] = 1e18;           for(int i=2;i<n;i++)           {               temp = max(temp, abs(a[i-1]-a[i]) + abs(a[i]-a[i+1])-abs(a[i+1]-a[i-1]));           }           temp = max(temp, abs(a[1]-a[2]));           temp = max(temp, abs(a[n]-a[n-1]));           ll ans = 0;           for(int i=2;i<=n;i++)               ans+=abs(a[i]-a[i-1]);           printf( %lld n ,ans-temp);       }       return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool has[20];   int dp[1010][12][12];   int use[1010][12][12];   int main() {     for (int i = 1; i <= 10; i++) {       char c;       cin >> c;       if (c ==  1 ) has[i] = true;     }     int m;     cin >> m;     if (m == 1) {       for (int i = 1; i <= 10; i++)         if (has[i]) {           cout <<  YES  << endl << i << endl;           return 0;         }       cout <<  NO  << endl;       return 0;     }     for (int i = 1; i <= 10; i++)       for (int j = 1; j <= 10; j++) {         use[m][i][j] = -1;         dp[m][i][j] = true;       }     for (int i = m - 1; i >= 1; i--)       for (int dif = 1; dif <= 10; dif++)         for (int ls = 1; ls <= 10; ls++)           for (int j = dif + 1; j <= 10; j++)             if (j != ls && has[j]) {               if (dp[i + 1][j - dif][j]) {                 dp[i][dif][ls] = true;                 use[i][dif][ls] = j;                 break;               }             }     for (int i = 1; i <= 10; i++)       if (has[i])         if (dp[1][i][i]) {           cout <<  YES  << endl;           cout << i <<    ;           int k = use[1][i][i];           int diff = i;           int lev = 1;           while (k != -1) {             cout << k <<    ;             lev++;             diff = k - diff;             k = use[lev][diff][k];           }           return 0;         }     cout <<  NO  << endl;   }