malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 200005;   const long long inf = 1e15;   long long dp[maxn], num[maxn], ans;   vector<int> G[maxn];   void dfs(int u, int fa) {     for (int i = 0; i < G[u].size(); i++) {       int v = G[u][i];       if (v == fa) continue;       dfs(v, u);       num[u] += num[v];       if (dp[u] != -inf) ans = max(ans, dp[u] + dp[v]);       dp[u] = max(dp[u], dp[v]);     }     dp[u] = max(dp[u], num[u]);   }   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %I64d , &num[i]);       dp[i] = -inf;       G[i].clear();     }     int u, v;     for (int i = 0; i < n - 1; i++) {       scanf( %d%d , &u, &v);       G[u].push_back(v);       G[v].push_back(u);     }     ans = -inf;     dfs(1, -1);     if (ans == -inf)       printf( Impossible n );     else       printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long quickpow(long long a, long long n, long long m) {     long long ans = 1;     while (n) {       if (n & 1) ans = (ans * a) % m;       a = (a * a) % m;       n >>= 1;     }     return ans;   }   bool is_prime(long long p) {     if (p < 2) return false;     for (long long i = 2; i * i <= p; i++) {       if (i < p && p % i == 0) return false;     }     return true;   }   bool is_root(long long a, long long p) {     if (a % p == 0) return false;     for (long long i = 1; i * i <= p - 1; i++) {       if ((p - 1) % i == 0) {         if (i < p - 1 && quickpow(a, i, p) == 1) return false;         if ((p - 1) / i < p - 1 && quickpow(a, (p - 1) / i, p) == 1) return false;       }     }     return true;   }   int main() {     long long N, X;     cin >> N >> X;     if (!is_prime(N + 1))       cout << -1 << endl;     else {       for (long long b = X - 1; b > 1; b--) {         if (is_root(b, N + 1)) {           cout << b << endl;           return 0;         }       }       cout << -1 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void fre() {     freopen( c://test//input.in ,  r , stdin);     freopen( c://test//output.out ,  w , stdout);   }   template <class T1, class T2>   inline void gmax(T1 &a, T2 b) {     if (b > a) a = b;   }   template <class T1, class T2>   inline void gmin(T1 &a, T2 b) {     if (b < a) a = b;   }   const int N = (1 << 20) + 10, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;   template <class T1, class T2>   inline void gadd(T1 &a, T2 b) {     a = (a + b) % Z;   }   int n, m, top;   int a[N], b[N];   long long num[21];   long long sum[21];   int v[N];   void add(int x, int val) {     for (; x <= top; x += x & -x) v[x] += val;   }   int cnt(int x) {     int ret = 0;     for (; x; x -= x & -x) ret += v[x];     return ret;   }   void init() {     for (int i = 1; i <= n; ++i) {       int l = 1 << i;       int d = m / l;       sum[i] = 0;       num[i] = 0;       int st = 0;       while (d--) {         for (int j = l / 2 + 1; j <= l; ++j) add(a[st + j], 1);         for (int j = 1; j <= l / 2; ++j) {           int tmp1 = cnt(a[st + j] - 1);           int tmp2 = cnt(a[st + j]);           num[i] += tmp1;           sum[i] += l / 2 - (tmp2 - tmp1);         }         for (int j = l / 2 + 1; j <= l; ++j) add(a[st + j], -1);         st += l;       }     }   }   int main() {     while (~scanf( %d , &n)) {       m = 1 << n;       for (int i = 1; i <= m; ++i) scanf( %d , &a[i]), b[i] = a[i];       sort(b + 1, b + m + 1);       top = unique(b + 1, b + m + 1) - b - 1;       for (int i = 1; i <= m; ++i)         a[i] = lower_bound(b + 1, b + top + 1, a[i]) - b;       init();       int q;       scanf( %d , &q);       while (q--) {         int x;         scanf( %d , &x);         for (int i = x; i >= 1; --i) num[i] = sum[i] - num[i];         long long ans = 0;         for (int i = 1; i <= n; ++i) ans += num[i];         printf( %lld n , ans);       }     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize(3)   using namespace std;   const int Mod = 1e9 + 7;   const int N = 3e5 + 100;   const int INF = 0x3f3f3f3f;   const long long LINF = 0x3f3f3f3f3f3f3f3f;   inline long long dpow(long long a, long long b) {     long long r = 1, t = a;     while (b) {       if (b & 1) r = (r * t) % Mod;       b >>= 1;       t = (t * t) % Mod;     }     return r;   }   inline long long fpow(long long a, long long b) {     long long r = 1, t = a;     while (b) {       if (b & 1) r = (r * t);       b >>= 1;       t = (t * t);     }     return r;   }   int a[N], ans[N], n;   int mx[N], lst[N];   void Init() {     for (int i = 1; i <= n; i++) mx[i] = lst[i] = ans[i] = 0;   }   int main() {     int T;     cin >> T;     while (T--) {       scanf( %d , &n);       Init();       for (int i = 1; i <= n; i++) {         scanf( %d , &a[i]);         mx[a[i]] = max(mx[a[i]], i - lst[a[i]]);         lst[a[i]] = i;       }       for (int i = 1; i <= n; i++)         if (lst[i]) mx[i] = max(mx[i], n + 1 - lst[i]);       int r = n;       for (int i = 1; i <= n; i++) {         if (!lst[i]) continue;         int j;         for (j = mx[i]; j <= r; j++) ans[j] = i;         r = min(r, mx[i] - 1);       }       for (int i = 1; i <= n; i++) {         if (!ans[i])           printf( -1  );         else           printf( %d  , ans[i]);       }       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, k, a[1005][1005], d[1005][1005], ans, D[10005];   pair<int, int> b[100005];   int ch(int x, int y) {     for (int i = 1; i <= k; i++) {       if (d[i][y] > d[i][x]) return 0;     }     return 1;   }   int main() {     cin >> n >> k;     for (int i = 1; i <= k; i++) {       for (int j = 1; j <= n; j++) cin >> a[i][j];       for (int j = 1; j <= n; j++) d[i][a[i][j]] = j;       for (int j = 1; j <= n; j++) b[a[i][j]].first = max(b[a[i][j]].first, j);     }     for (int j = 1; j <= n; j++) b[j].second = j;     sort(b + 1, b + n + 1);     for (int i = 1; i <= n; i++) {       int ma = 0;       int I = b[i].second;       for (int j = 1; j < i; j++) {         int J = b[j].second;         if (ch(I, J) && D[j] > ma) ma = D[j];       }       D[i] = ma + 1;       ans = max(ans, D[i]);     }     cout << ans << endl;     cin >> n;   }
#include <bits/stdc++.h>   using namespace std;   int a[105];   int main() {     int n, l, i, j, cnt, area, ans;     scanf( %d%d , &n, &l);     for (i = 1; i <= n; i++) scanf( %d , &a[i]);     ans = 0;     for (i = l; i <= 100; i++) {       cnt = 0;       for (j = 1; j <= n; j++) cnt += a[j] / i;       area = cnt * i;       ans = max(ans, area);     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     register long long x = 0, f = 1;     register char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 ) x = x * 10 + c - 48, c = getchar();     return x * f;   }   long long t, l, r, tot, cnt;   long long f[50 + 5][2520 + 5][50 + 5], in[2520 + 5], a[2520 + 5], bas[2520 + 5];   long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); }   long long dfs(long long p, long long sum, long long lcm, bool limit) {     if (!p) return sum % lcm == 0 ? 1 : 0;     if (!limit && f[p][sum][in[lcm]] != -1) return f[p][sum][in[lcm]];     long long up = limit ? a[p] : 9, ans = 0;     for (register long long i = 0; i <= up; ++i) {       if (i == 0)         ans += dfs(p - 1, sum * 10 % 2520, lcm, limit && (i == up));       else         ans += dfs(p - 1, (sum * 10 + i) % 2520, lcm * i / gcd(lcm, i),                    limit && (i == up));     }     if (!limit) f[p][sum][in[lcm]] = ans;     return ans;   }   long long calc(long long x) {     cnt = 0;     while (x) a[++cnt] = x % 10, x /= 10;     return dfs(cnt, 0, 1, 1);   }   signed main() {     t = read();     memset(f, -1, sizeof(f));     ;     for (register long long i = 1; i <= 2520; ++i)       if (2520 % i == 0) in[i] = (++(tot));     while (t--) l = read(), r = read(), printf( %lld n , calc(r) - calc(l - 1));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     ;     long long t = 1;     long long arr[7] = {1, 2, 3, 1, 3, 2, 1};     while (t--) {       long long a, b, c;       cin >> a >> b >> c;       long long full = min({a / 3, b / 2, c / 2});       a -= 3 * full;       b -= 2 * full;       c -= 2 * full;       long long extra = 0;       long long i = 0;       while (i < 7) {         long long a1 = a, b1 = b, c1 = c;         long long sta = i;         long long cue = 0;         while (1) {           if (arr[sta] == 1 && a1 == 0) break;           if (arr[sta] == 2 && b1 == 0) break;           if (arr[sta] == 3 && c1 == 0) break;           if (arr[sta] == 1) a1--;           if (arr[sta] == 2) b1--;           if (arr[sta] == 3) c1--;           cue++;           sta++;           if (sta == 7) sta = 0;         }         if (extra < cue) extra = cue;         i++;       }       cout << 7 * full + extra << endl;     }   }
#include <bits/stdc++.h>   #pragma GCC optimize( O3 )   using namespace std;   const long long mod = 1e9 + 7;   const long long inf = 1e9 + 7;   vector<vector<int> > g;   vector<bool> used;   int main() {     int n, m;     cin >> n >> m;     g.resize(n);     used.resize(n, false);     for (int i = 0; i < m; ++i) {       int u, v;       cin >> u >> v;       u--, v--;       g[u].push_back(v);       g[v].push_back(u);     }     set<int> q;     q.insert(0);     used[0] = true;     while (!q.empty()) {       int u = *q.begin();       q.erase(q.begin());       cout << u + 1 <<    ;       for (auto v : g[u])         if (!used[v]) {           q.insert(v);           used[v] = true;         }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   int fib(int n) {     int b = 1;     int c = 1;     while (n--) {       c = (c + b) % MOD;       swap(b, c);     }     return c;   }   int main() {     int n, m;     cin >> n >> m;     int a = fib(n);     int b = fib(m);     cout << (a + b - 1) % MOD * 2 % MOD << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[10100], b[1010];   int main() {     int n, m, c, ans;     scanf( %d%d%d , &n, &m, &c);     ans = (n + m + c) / 2;     if (n + c >= ans && m + c >= ans)       printf( %d n , ans * 2);     else       printf( %d n , min(n + c, m + c) * 2);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t;     cin >> n >> t;     if (t == 10) {       if (n < 2) {         cout <<  -1 n ;         return 0;       }       cout << 1;       for (int i = 0; i < n - 1; i++) cout << 0;       cout <<   n ;       return 0;     }     cout << t;     for (int i = 0; i < n - 1; i++) cout << 0;     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline int in() {     int32_t x;     scanf( %d , &x);     return x;   }   inline long long lin() {     long long x;     scanf( %lld , &x);     return x;   }   char ch[4000010];   inline string get() {     scanf( %s , ch);     return string(ch);   }   inline void read(int *a, int n) {     for (int i = 0; i < n; i++) a[i] = in();   }   template <class blank>   inline void out(blank x) {     cout << x <<   n ;     exit(0);   }   template <class blank, class blank2>   inline void smin(blank &a, blank2 b) {     a = a <= b ? a : b;   }   template <class blank, class blank2>   inline void smax(blank &a, blank2 b) {     a = a >= b ? a : b;   }   const int maxn = 3e5 + 10;   const int maxm = 4e6 + 10;   const int maxlg = 20;   const int base = 29;   const int mod = 1e9 + 7;   const int inf = 2e18 + 10;   const double eps = 1e-9;   string code[maxn];   int correct[maxn];   int mark[maxn];   int n, m;   int res;   inline void bt(int pos = 0, int cnt = 0) {     if (cnt > correct[0] || n - pos + cnt < correct[0]) return;     if (pos == n) {       string cc = code[0];       for (int i = 0; i < n; i++) {         if (mark[i] == 0) {           cc[i] ^=  1  ^  0 ;         }       }       bool fl = true;       for (int i = 0; i < m; i++) {         int diff = 0;         for (int j = 0; j < n; j++) diff += code[i][j] != cc[j];         if (diff != n - correct[i]) fl = false;       }       res += fl;       return;     }     bt(pos + 1, cnt);     mark[pos] = true;     bt(pos + 1, cnt + 1);     mark[pos] = false;   }   int32_t main() {     n = in(), m = in();     for (int i = 0; i < m; i++) {       code[i] = get(), correct[i] = in();     }     bt(0);     cout << res <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   const double pi = acos(-1.0);   bool getcell(vector<vector<bool>>& b, int i, int j) {     if (i >= b.size() || j >= b[0].size() || i < 0 || j < 0) return false;     return b[i][j];   }   bool isPlus(vector<vector<bool>>& b, int i, int j) {     return getcell(b, i + 1, j) && getcell(b, i - 1, j) && getcell(b, i, j + 1) &&            getcell(b, i, j - 1);   }   int genPlus(vector<vector<bool>>& b, int i, int j) {     int ret = 0;     int x, y;     x = i;     y = j;     while (getcell(b, x + 1, y)) {       ++x;       ++ret;     }     x = i;     y = j;     while (getcell(b, x - 1, y)) {       --x;       ++ret;     }     x = i;     y = j;     while (getcell(b, x, y + 1)) {       ++y;       ++ret;     }     x = i;     y = j;     while (getcell(b, x, y - 1)) {       --y;       ++ret;     }     return ret;   }   int main(int argc, char const* argv[]) {     cin.sync_with_stdio(0);     cin.tie(0);     int h, w;     cin >> h >> w;     int cnt = 0;     vector<vector<bool>> there(h, vector<bool>(w));     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         char c;         cin >> c;         there[i][j] = (c ==  * );         cnt += there[i][j];       }     }     for (int i = 0; i < h; i++) {       for (int j = 0; j < w; j++) {         if (there[i][j] && isPlus(there, i, j)) {           int g = genPlus(there, i, j);           if (g + 1 == cnt) {             cout <<  YES ;             return 0;           }           cout <<  NO ;           return 0;         }       }     }     cout <<  NO ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char str[200000 + 5];   long long f[200000 + 5];   struct node {     int nxt[26];     int fa;     long long len;   } t[200000 + 10];   long long ans = 0;   int last, tot;   vector<int> g[200000 + 5];   void insrt(int x) {     int nd = ++tot;     f[nd] = 1;     int p = last;     last = nd;     t[nd].len = t[p].len + 1;     for (; p && t[p].nxt[x] == 0; p = t[p].fa) t[p].nxt[x] = nd;     if (!p)       t[nd].fa = 1;     else {       int q = t[p].nxt[x];       if (t[p].len + 1 == t[q].len)         t[nd].fa = q;       else {         int nt = ++tot;         t[nt] = t[q];         t[nd].fa = t[q].fa = nt;         t[nt].len = t[p].len + 1;         for (; t[p].nxt[x] == q && p; p = t[p].fa) t[p].nxt[x] = nt;       }     }   }   void init() {     last = 1, tot = 1;     memset(t, 0, sizeof(t));     for (int i = 0; i <= 200000 + 3; i++) g[i].clear(), f[i] = 0;   }   void dfs(int u) {     for (int i = 0; i < g[u].size(); i++) {       dfs(g[u][i]);       f[u] += f[g[u][i]];     }     ans += (t[u].len - t[t[u].fa].len) * f[u] * f[u];     return;   }   int main() {     int T_T;     cin >> T_T;     while (T_T--) {       scanf( %s , str);       int len = strlen(str);       init();       ans = 0;       for (int i = 0; i < len; i++) insrt(str[i] -  a );       for (int i = 1; i <= tot; i++) g[t[i].fa].push_back(i);       dfs(0);       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int maximum_no = 1e18 + 1;   string A, B, C, curr;   long long int a, b, c, f[100005];   char solve(int n, long long int k) {     if (n == 0) {       if (k > f[0]) return  . ;       return curr[k - 1];     }     if (k <= a) return A[k - 1];     if (k <= a + f[n - 1]) return solve(n - 1, k - a);     if (k <= a + f[n - 1] + b) return B[k - a - f[n - 1] - 1];     if (k <= a + f[n - 1] + b + f[n - 1])       return solve(n - 1, k - a - f[n - 1] - b);     if (k <= a + f[n - 1] + b + f[n - 1] + c)       return C[k - (a + f[n - 1] + b + f[n - 1]) - 1];     return  . ;   }   int n, q;   long long int k;   int main() {     A =  What are you doing while sending   ;     B =   ? Are you busy? Will you send   ;     C =   ? ;     curr =          What are you doing at the end of the world? Are you busy? Will you save            us? ;     f[0] = curr.length();     a = A.length();     b = B.length();     c = C.length();     for (int i = 1; i <= 100000; i++) {       f[i] = min(maximum_no, a + f[i - 1] + b + f[i - 1] + c);     }     string ans =   ;     scanf( %d , &q);     while (q--) {       scanf( %d %lld , &n, &k);       if (f[n] < k)         ans.push_back( . );       else         ans.push_back(solve(n, k));     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t;     cin >> t;     while (t--) {       int len, k;       cin >> len >> k;       string s;       cin >> s;       sort(s.begin(), s.end());       if (s[0] != s[k - 1])         cout << s[k - 1] << endl;       else {         cout << s[k - 1];         if (s[k] == s[len - 1]) {           int q = (len - k) / k;           if ((len - k) % k) q++;           while (q--) cout << s[k];         } else           for (int i = k; i < len; i++) cout << s[i];         cout << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 500005;   int n, m[N], t[N];   struct People {     int a, b, c, cnt;   } p[N], ran[N];   int read() {     int x = 0;     char c = getchar();     while (!isdigit(c)) c = getchar();     while (isdigit(c)) x = x * 10 + c -  0 , c = getchar();     return x;   }   bool cmp(People x, People y) {     if (x.a != y.a)       return x.a > y.a;     else       return x.c < y.c;   }   int lowbit(int x) { return x & -x; }   void add(int x, int k) {     for (; x <= n; x += lowbit(x)) t[x] += k;     return;   }   int sum(int x) {     int ret = 0;     for (; x; x -= lowbit(x)) ret += t[x];     return ret;   }   void merge(int l, int r) {     if (l == r) return;     int mid = (l + r) / 2;     merge(l, mid);     merge(mid + 1, r);     int i = l, j = mid + 1, cur = l;     for (; i <= mid && j <= r;) {       if (p[i].b > p[j].b)         add(p[i].c, 1), ran[cur++] = p[i++];       else         p[j].cnt += sum(n) - sum(p[j].c), ran[cur++] = p[j++];     }     for (; i <= mid;) add(p[i].c, 1), ran[cur++] = p[i++];     for (; j <= r;) p[j].cnt += sum(n) - sum(p[j].c), ran[cur++] = p[j++];     for (i = l; i <= mid; ++i) add(p[i].c, -1);     for (i = l; i <= r; ++i) p[i] = ran[i];   }   int main() {     n = read();     for (int i = 1; i <= n; ++i) p[i].a = read();     for (int i = 1; i <= n; ++i) p[i].b = read();     for (int i = 1; i <= n; ++i) p[i].c = read(), m[i] = p[i].c;     sort(m + 1, m + 1 + n);     for (int i = 1; i <= n; ++i)       p[i].c = lower_bound(m + 1, m + 1 + n, p[i].c) - m;     sort(p + 1, p + 1 + n, cmp);     merge(1, n);     int ans = 0;     for (int i = 1; i <= n; ++i)       if (p[i].cnt) ++ans;     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string str;   string l;   map<string, int> m;   string Greedy_Solve() {     string res =   ;     for (int i = 0; i < l.length(); i += 10) {       str = l.substr(i, 10);       res += m[str] +  0 ;     }     return res;   }   int main() {     cin >> l;     for (int i = 0; i < 10; i++) {       cin >> str;       m[str] = i;     }     cout << Greedy_Solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, m, k;   long long ans;   int main() {     cin >> n >> m >> k;     if (k > n + m - 2) {       cout << -1 << endl;       return 0;     }     if (k > n - 1) {       long long x = k;       x -= (n - 1);       ans = m / (x + 1);     } else       ans = n / (k + 1) * m;     if (k > m - 1) {       long long x = k;       x -= (m - 1);       ans = max(ans, (n / (x + 1)));     } else       ans = max(ans, (m / (k + 1) * n));     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 0x3f3f3f3f;   const double EPS = 0.000000001;   const double PI = 3.141592653589793;   const long long LLINF = 99999999999999999LL;   const int MAX_N = 100005;   const int MOD = 1000000007;   int N, M, n, cnt, ans;   vector<int> v[MAX_N], w[MAX_N], SCC[MAX_N], a;   bool cycle;   bool hasCycle[MAX_N], vis[MAX_N];   void DFS1(int x) {     vis[x] = 1;     for (int i = 0; i < (int)v[x].size(); ++i) {       int y = v[x][i];       if (vis[y] == 0) DFS1(y);     }     a.push_back(x);   }   void DFS2(int x) {     vis[x] = 1;     SCC[n].push_back(x);     for (int i = 0; i < (int)w[x].size(); ++i) {       int y = w[x][i];       if (vis[y] == 0) DFS2(y);     }   }   void DFS(int x) {     ++cnt;     vis[x] = 1;     if (hasCycle[x]) cycle = 1;     for (int i = 0; i < (int)v[x].size(); ++i) {       int y = v[x][i];       if (vis[y] == 0) DFS(y);     }     for (int i = 0; i < (int)w[x].size(); ++i) {       int y = w[x][i];       if (vis[y] == 0) DFS(y);     }   }   int main() {     cin >> N >> M;     for (int i = 1; i <= M; ++i) {       int x, y;       cin >> x >> y;       v[x].push_back(y);       w[y].push_back(x);     }     for (int i = 1; i <= N; ++i)       if (!vis[i]) DFS1(i);     memset(vis, 0, sizeof(vis));     for (int i = a.size() - 1; i >= 0; --i)       if (!vis[a[i]]) {         ++n;         DFS2(a[i]);       }     for (int i = 1; i <= n; ++i)       if (SCC[i].size() > 1)         for (int j = 0; j < (int)SCC[i].size(); ++j) hasCycle[SCC[i][j]] = 1;     memset(vis, 0, sizeof(vis));     for (int i = 1; i <= N; ++i) {       if (vis[i]) continue;       cnt = 0;       cycle = 0;       DFS(i);       if (cycle)         ans += cnt;       else         ans += cnt - 1;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int vis[N], parent[N];   vector<int> v[N];   void dfs(int node) {     vis[node] = 1;     for (auto i : v[node]) {       if (!vis[i]) {         parent[i] = node;         dfs(i);       }     }   }   int res[N];   int main() {     int n, l, r, k = 0;     scanf( %d   , &n);     parent[n] = n;     for (int i = 1; i < n; ++i) {       parent[i] = i;       scanf( %d %d , &l, &r);       v[l].push_back(r);       v[r].push_back(l);       k = max(k, max((int)v[l].size(), (int)v[r].size()));     }     k += 1;     vector<int> col(k + 1);     parent[1] = 1;     dfs(1);     memset(vis, 0, sizeof(vis));     queue<int> q;     vis[1] = true;     q.push(1);     res[1] = 1;     col[1] = true;     while (!q.empty()) {       int node = q.front();       q.pop();       col[res[parent[node]]] = true;       col[res[node]] = true;       int j = 1, x = (int)col.size();       for (auto i : v[node]) {         if (!vis[i]) {           q.push(i);           while (j < x and col[j] == true) ++j;           res[i] = j;           col[j] = true;           vis[i] = 1;         }       }       for (auto i : v[node]) col[res[i]] = false;       col[res[node]] = false;     }     printf( %d n , k);     for (int i = 1; i <= n; ++i) printf( %d  , res[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e9 + 7;   int main() {     int n, k;     cin >> n >> k;     int a[n];     for (int i = 0; i < n; i++) cin >> a[i];     int difference;     int ans = 0;     for (int i = 0; i < n - 1; i++) {       if (a[i] + a[i + 1] < k) {         difference = k - (a[i] + a[i + 1]);         ans += difference;         a[i + 1] = a[i + 1] + difference;       }     }     cout << ans;     cout << endl;     ;     for (int i = 0; i < n; i++) cout << a[i] <<    ;   }