malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int h;   int edge[5000];   int extend[5000];   int maximum;   int v[5000];   bool flag = 0;   void dfs_m(int p, int value) {     v[p] = value;     if (edge[p * 2] != 0)       dfs_m(p * 2 + 0, value + edge[p * 2]);     else {       if (maximum < value) maximum = value;     }     if (edge[p * 2 + 1] != 0)       dfs_m(p * 2 + 1, value + edge[p * 2 + 1]);     else {       if (maximum < value) maximum = value;     }   }   void dfs(int p, int value) {     if (edge[p * 2] != 0) {       dfs(p * 2, value + edge[p * 2] + extend[p * 2]);     } else {       if (value < maximum) {         extend[p] += (maximum - value);       }     }     if (edge[p * 2 + 1] != 0) {       dfs(p * 2 + 1, value + edge[p * 2 + 1] + extend[p * 2 + 1]);     } else {       if (value < maximum) {         extend[p] += (maximum - value);       }     }   }   int min(int a, int b) {     if (a == b) return -1;     if (a < b) {       return a;     }     return b;   }   void merge(int n) {     if (extend[n * 2] != 0 && extend[n * 2 + 1] != 0) {       int m = min(extend[n * 2 + 1], extend[n * 2]);       flag = false;       if (m == -1) {         extend[n] += extend[n * 2 + 1];         extend[n * 2] = extend[n * 2 + 1] = 0;         merge(n / 2);       } else {         extend[n] += m;         extend[n * 2] -= m;         extend[n * 2 + 1] -= m;         merge(n / 2);       }     }   }   int main() {     scanf( %d , &h);     int e = pow(2, h + 1) - 2;     memset(edge, 0, sizeof(edge));     memset(extend, 0, sizeof(extend));     memset(v, 0, sizeof(v));     for (int i = 2; i <= e + 1; i++) {       scanf( %d , &edge[i]);     }     maximum = 0;     dfs_m(1, 0);     dfs(1, 0);     int node = pow(2, h + 1);     for (int i = 2; i <= e + 1; i++) {       extend[i] /= 2;     }     while (true) {       flag = true;       for (int i = 0; i < node; i++) {         merge(i);       }       for (int i = 2; i <= e + 1; i++) {       }       if (flag) {         break;       }     }     int ans = 0;     for (int i = 0; i <= e + 1; i++) {       ans += extend[i];     }     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<pair<long long int, long long int>, int> m;   long long int comb(long long int n, long long int r) {     int k1, k2;     if (m[make_pair(n, r)] != 0) return m[make_pair(n, r)];     if (r > n) return 0;     if (n == r) return 1;     if (r == 0) return 1;     if (r == 1) return n;     k1 = comb(n - 1, r - 1);     k2 = comb(n - 1, r);     m[make_pair(n - 1, r - 1)] = k1;     m[make_pair(n - 1, r)] = k2;     return k1 + k2;   }   int main() {     long long int n, m, t, k1 = 0, k2 = 0, k3 = 0, sum = 0, i = 0, x, y;     cin >> n >> m >> t;     if ((n + m) < t) cout << 0;     if (n < 4 || m < 1) cout << 0;     for (x = 4; x <= t - 1; x++) {       if (m >= (t - x)) sum += (comb(n, x) * comb(m, t - x));     }     cout << sum;   }
#include <bits/stdc++.h>   using namespace std;   int a[123], p[123];   signed main() {     int t;     scanf( %d , &t);     while (t--) {       int n, m;       scanf( %d %d , &n, &m);       memset(p, 0, sizeof(p));       for (int i = 1; i <= n; ++i) scanf( %d , a + i);       for (int i = 1, x; i <= m; ++i) scanf( %d , &x), p[x] = 1;       bool ok = 1;       for (int i = 1; i <= n; ++i)         for (int j = i + 1; j <= n; ++j) {           if (!ok) break;           if (a[i] > a[j])             for (int k = i; k < j; ++k)               if (!p[k]) {                 ok = 0;                 break;               }         }       ok ? puts( YES ) : puts( NO );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long fac[3000060];   void pre() {     fac[0] = 1;     for (int i = 1; i < 3000060; i++) fac[i] = fac[i - 1] * i % 1000000007;   }   long long powermod(long long x, long long y, long long p) {     long long res = 1;     x = x % p;     if (x == 0) return 0;     while (y > 0) {       if (y & 1) res = (res * x) % p;       y = y >> 1;       x = (x * x) % p;     }     return res;   }   long long nCrmod(long long n, long long r, long long p) {     if (r == 0) return 1;     return (fac[n] * powermod(fac[r], p - 2, p) % p *             powermod(fac[n - r], p - 2, p) % p) %            p;   }   void solve() {     long long n;     cin >> n;     long long use[n][n];     for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {         cin >> use[i][j];       }     }     for (int i = 0; i < n; i++) {       long long p = (use[i][(i + 1) % n] * use[i][(i + 2) % n]) /                     use[(i + 1) % n][(i + 2) % n];       p = sqrt(p);       cout << p <<    ;     }   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t;     t = 1;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long inf = 1e18;   vector<long long> a;   map<long long, bool> p;   int binp(long long x) {     int l = 0, r = 1e9 + 1;     while (r - l > 1) {       int m = (l + r) / 2;       if (1LL * m * m <= x)         l = m;       else         r = m;     }     return l;   }   int main() {     a.push_back(0);     for (long long i = 2; i <= 1000000; ++i) {       long long cur = i * i * i;       while (inf / i >= cur) {         long long x = binp(cur);         if (!p[cur] && x * x != cur) a.push_back(cur);         p[cur] = 1;         cur *= i;       }       long long x = binp(cur);       if (!p[cur] && x * x != cur) a.push_back(cur);       p[cur] = 1;     }     sort(a.begin(), a.end());     int t;     scanf( %d , &t);     while (t--) {       long long l, r;       scanf( %lld %lld , &l, &r);       long long ans = upper_bound(a.begin(), a.end(), r) -                       upper_bound(a.begin(), a.end(), l - 1);       ans += binp(r) - binp(l - 1);       printf( %lld n , ans);     }     return 0;   }
#include <bits/stdc++.h>   #pragma GCC optimize( Ofast )   #pragma GCC optimize( unroll-loops )   #pragma GCC target(         sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native )   void read(int &a) {     a = 0;     char c = getchar();     while (c <  0  || c >  9 ) {       c = getchar();     }     while (c >=  0  && c <=  9 ) {       a = (a << 1) + (a << 3) + (c ^ 48);       c = getchar();     }   }   const int Maxn = 50000;   const int Inf = 0x3f3f3f3f;   int n, q;   int a[Maxn + 5];   int fa[Maxn + 5];   int head[Maxn + 5], arrive[Maxn + 5], nxt[Maxn + 5], tot;   void add_edge(int from, int to) {     arrive[++tot] = to;     nxt[tot] = head[from];     head[from] = tot;   }   int dep[Maxn + 5], dis[Maxn + 5];   int dfn[Maxn + 5], right[Maxn + 5], rnk[Maxn + 5], dfn_tot, sz[Maxn + 5];   int sum[Maxn + 5];   long long f[Maxn + 5];   void init_dfs(int u) {     dep[u] = dep[fa[u]] + 1;     if (a[u] != 1) {       dfn[u] = ++dfn_tot;       dis[dfn_tot] = dep[u];       rnk[dfn_tot] = u;       sz[u] = 1;       sum[u] = 0;     } else {       dfn[u] = dfn_tot + 1;       sz[u] = 0;       sum[u] = 1;     }     for (int i = head[u]; i; i = nxt[i]) {       int v = arrive[i];       init_dfs(v);       sz[u] += sz[v];       sum[u] += sum[v];       f[u] += f[v];       f[u] += sum[v];     }     right[u] = dfn[u] + sz[u] - 1;   }   int del[Maxn + 5];   int main() {     read(n), read(q);     for (register int i = 1; i <= n; ++i) {       read(a[i]);       del[i] = -Inf;     }     for (register int i = 2; i <= n; ++i) {       read(fa[i]);       add_edge(fa[i], i);     }     init_dfs(1);     for (register int i = 1; i <= q; ++i) {       int u;       read(u);       long long ans_1 = 0;       int ans_2 = 0;       for (register int j = dfn[u]; j <= right[u]; ++j) {         int tmp = (i >= del[j]);         ans_1 += dis[j] * tmp;         ans_2 += tmp;         del[j] = (tmp ? i + a[rnk[j]] : del[j]);       }       ans_1 -= 1ll * ans_2 * dep[u];       ans_1 += f[u], ans_2 += sum[u];       printf( %lld %d n , ans_1, ans_2);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   long long sum[1505][1505];   long long ans[1505];   long long t[1505];   int main() {     int i, j, x;     long long val, now, res;     scanf( %d%d , &n, &m);     for (i = 1; i <= n; i++)       for (j = 1; j <= m; j++)         scanf( %I64d , &val), sum[i][j] = sum[i][j - 1] + val;     for (i = 1; i <= m; i++) ans[i] = sum[1][i];     for (i = 2; i <= n; i++) {       if (i % 2 == 0) {         now = -(long long)1e15;         for (x = m; x > 0; x--) {           t[x] = now + sum[i][x];           now = max(now, ans[x]);         }       } else {         now = -(long long)1e15;         for (x = 1; x <= m; x++) {           t[x] = now + sum[i][x];           now = max(now, ans[x]);         }       }       for (x = 1; x <= m; x++) ans[x] = t[x];     }     res = -(long long)1e15;     for (x = 1; x <= m; x++) res = max(res, ans[x]);     printf( %I64d , res);   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long int n, m, a[105], cnt = 0, i;     fill_n(a, 105, 0);     for (cin >> m >> n; n; n /= m) a[cnt++] = n % m;     if (cnt > 100) {       cout <<  NO ;       return 0;     }     for (i = 0; i < 105; ++i) {       if (a[i] <= 1)         continue;       else if (a[i] < m - 1) {         cout <<  NO ;         return 0;       } else         ++a[i + 1];     }     cout <<  YES ;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100 * 1000 + 10, M = 300 + 10;   int a[N], dp[N][M];   vector<int> p[N];   int main() {     ios_base::sync_with_stdio(false);     int n, m, s, e;     cin >> n >> m >> s >> e;     for (int i = (0); i <= (n - 1); i++) cin >> a[i];     for (int i = (1); i <= (m); i++) {       int x;       cin >> x;       p[x].emplace_back(i);     }     int ans = 0;     for (int i = (0); i <= (s / e); i++) dp[0][i] = i * (m + 1);     for (int i = (1); i <= (n); i++) {       for (int j = (1); j <= (s / e); j++) {         auto it = upper_bound((p[a[i - 1]]).begin(), (p[a[i - 1]]).end(),                               (j == 1 ? 0 : dp[i - 1][j - 1]));         dp[i][j] = dp[i - 1][j];         if (it != p[a[i - 1]].end()) {           dp[i][j] = min(dp[i][j], *it);           if (s - e * j >= i + dp[i][j]) ans = max(ans, j);         }       }     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct Node {     int val;     int op;     int cl;     Node(int x, int y, int z) {       val = x;       op = y;       cl = z;     }     Node() { Node(0, 0, 0); };   };   string s;   vector<Node> ST;   Node temp;   int n, t, x, y, t1, t2;   void update(int root, int l, int r, int pos) {     if (l > pos || r < pos) return;     if (l == r) {       if (s[pos - 1] ==  ( ) ST[root].op = 1;       if (s[pos - 1] ==  ) ) ST[root].cl = 1;       return;     }     int mid = (l + r) / 2;     update(root * 2, l, mid, pos);     update(root * 2 + 1, mid + 1, r, pos);     ST[root].val = ST[root * 2].val + ST[root * 2 + 1].val +                    min(ST[root * 2].op - ST[root * 2].val,                        ST[root * 2 + 1].cl - ST[root * 2 + 1].val);     ST[root].op = ST[root * 2].op + ST[root * 2 + 1].op;     ST[root].cl = ST[root * 2].cl + ST[root * 2 + 1].cl;   }   Node get(int root, int l, int r, int u, int v) {     if (u > r || v < l) return Node(0, 0, 0);     if (u == l && v == r) return ST[root];     int mid = (l + r) / 2;     if (v <= mid)       return get(root * 2, l, mid, u, v);     else if (u > mid)       return get(root * 2 + 1, mid + 1, r, u, v);     else {       Node n1 = get(root * 2, l, mid, u, mid),            n2 = get(root * 2 + 1, mid + 1, r, mid + 1, v);       int t1 = n1.val + n2.val + min(n1.op - n1.val, n2.cl - n2.val);       return Node(t1, n1.op + n2.op, n1.cl + n2.cl);     }   }   int main() {     cin >> s;     n = s.size();     for (int i = 0; i < 4 * n; ++i) ST.push_back(Node(0, 0, 0));     for (int i = 1; i <= n; ++i) update(1, 1, n, i);     cin >> t;     while (t--) {       cin >> x >> y;       cout << get(1, 1, n, x, y).val * 2 << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     int i, j, t, n, a[150], b[150], sum1 = 0, sum2 = 0;     cin >> n;     for (i = 1; i <= n; i++) {       cin >> a[i];     }     for (i = 1; i <= n; i++) {       cin >> b[i];     }     for (i = 1; i <= n; i++) {       if (a[i] && !b[i]) sum1++;       if (!a[i] && b[i]) sum2++;     }     if (sum1 == 0) {       cout <<  -1  <<   n ;     } else if (sum1 > sum2) {       cout << 1 <<   n ;     } else {       int k = sum2 / sum1;       k++;       cout << k <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, pos, l, r, diff, rflag = 1, lflag = 1;     cin >> n >> pos >> l >> r;     diff = r - l;     if (l == 1) lflag = 0;     if (r == n) rflag = 0;     if (lflag == 0 && rflag == 0)       cout << 0;     else if (lflag == 0)       cout << abs(r - pos) + 1;     else if (rflag == 0)       cout << abs(pos - l) + 1;     else       cout << diff + min(abs(r - pos), abs(pos - l)) + 2;   }
#include <bits/stdc++.h>   using namespace std;   double maxoo(double, double);   int main() {     int p1, p2, t1, t2;     cin >> p1 >> p2 >> t1 >> t2;     double p11, p12, p21, p22, misha, vasya;     p11 = (3 * p1) / 10;     p12 = (p1 - (p1 / 250) * t1);     p21 = (3 * p2) / 10;     p22 = p2 - (p2 / 250) * t2;     misha = maxoo(p11, p12);     vasya = maxoo(p21, p22);     if (misha > vasya)       cout <<  Misha ;     else if (misha == vasya)       cout <<  Tie ;     else if (misha < vasya)       cout <<  Vasya ;     return 0;   }   double maxoo(double m, double n) {     if (m >= n)       return m;     else       return n;   }
#include <bits/stdc++.h>   using namespace std;   long long sum[500];   int deep[500];   int in1[500];   int out1[500];   int in2[500];   int out2[500];   vector<int> maps1[500];   vector<int> maps2[500];   long long dp[150000];   long long n, m, t;   int cnt = 0;   void topo2() {     queue<int> q;     for (int i = 1; i <= n; ++i)       if (in2[i] == 0) {         q.push(i);         deep[i] = 0;       }     int n1, n2;     while (!q.empty()) {       n1 = q.front();       q.pop();       cnt++;       t -= deep[n1] * sum[n1];       for (int i = 0; i < maps2[n1].size(); ++i) {         n2 = maps2[n1][i];         in2[n2]--;         deep[n2] = max(deep[n2], deep[n1] + 1);         if (in2[n2] == 0) q.push(n2);       }     }   }   void topo1() {     queue<int> q;     int n1, n2;     for (int i = 1; i <= n; ++i)       if (in1[i] == 0) {         q.push(i);       }     while (!q.empty()) {       n1 = q.front();       q.pop();       for (int i = 0; i < maps1[n1].size(); ++i) {         n2 = maps1[n1][i];         in1[n2]--;         sum[n2] += sum[n1];         if (in1[n2] == 0) {           q.push(n2);         }       }     }   }   int main() {     scanf( %lld%lld%lld , &n, &m, &t);     for (int i = 1; i <= n; ++i) scanf( %lld , &sum[i]);     for (int i = 1; i <= m; ++i) {       int x, y;       scanf( %d%d , &x, &y);       maps1[x].push_back(y);       in1[y]++;       out1[x]++;       maps2[y].push_back(x);       in2[x]++;       out2[y]++;     }     topo2();     if (t < 0 || cnt != n)       printf( 0 n );     else {       topo1();       memset(dp, 0, sizeof(dp));       dp[0] = 1;       for (long long i = 1; i <= n; ++i) {         for (long long j = sum[i]; j <= t; ++j) {           dp[j] += dp[j - sum[i]];           dp[j] %= 1000000007;         }       }       printf( %lld n , dp[t]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int wyn[1024];   int odp[1024];   int n;   int pyt;   vector<pair<int, int> > v[2];   void pytaj(vector<int>& skad, vector<int>& gdzie) {     if (skad.size() == 0) return;     printf( %d n , int(skad.size()));     for (int i = 0; i < skad.size(); i++) printf( %d  , skad[i] + 1);     printf(  n );     fflush(stdout);     for (int i = 0; i < n; i++) scanf( %d , &odp[i]);     pyt++;     for (int q = 0; q < gdzie.size(); q++) {       int i = gdzie[q];       wyn[i] = min(wyn[i], odp[i]);     }   }   int main() {     pyt = 0;     scanf( %d , &n);     for (int i = 0; i < n; i++) wyn[i] = 1000000010;     v[0].push_back(make_pair(0, n - 1));     int akt = 0;     while (!v[akt].empty()) {       v[1 - akt].clear();       vector<int> pyt1, pyt2;       for (int k = 0; k < v[akt].size(); k++) {         int a = v[akt][k].first;         int b = v[akt][k].second;         int sr = (a + b) / 2;         for (int q = a; q <= sr; q++) pyt1.push_back(q);         for (int q = sr + 1; q <= b; q++) pyt2.push_back(q);         if (sr > a) v[1 - akt].push_back(make_pair(a, sr));         if (b > sr + 1) v[1 - akt].push_back(make_pair(sr + 1, b));       }       pytaj(pyt1, pyt2);       pytaj(pyt2, pyt1);       akt = 1 - akt;     }     printf( -1 n );     for (int i = 0; i < n; i++) printf( %d  , wyn[i]);     printf(  n );     fflush(stdout);   }
#include <bits/stdc++.h>   using namespace std;   int main(void) {     int n;     cin >> n;     if (n == 0) {       cout << 1 << endl;       return 0;     }     vector<double> p(n);     vector<int> powers(n, 1);     vector<double> curval(n);     using pdi = pair<double, int>;     priority_queue<pdi, vector<pdi>> q;     double prob = 1.0;     for (int i = 0; i < n; ++i) {       cin >> p[i];       p[i] /= 100.0;       prob *= p[i];       curval[i] = p[i];       q.push(pdi((1 - pow(1 - p[i], powers[i] + 1)) / curval[i], i));     }     double result = n + 1 - prob;     for (int i = 0; i < 500000; ++i) {       auto c = q.top();       q.pop();       prob /= curval[c.second];       ++powers[c.second];       curval[c.second] = 1 - pow(1 - p[c.second], powers[c.second]);       prob *= curval[c.second];       q.push(           pdi((1 - pow(1 - p[c.second], powers[c.second] + 1)) / curval[c.second],               c.second));       result += 1 - prob;     }     cout << fixed << setprecision(20) << result << endl;   }
// Problem: A. Robot Program   // Contest: Codeforces - Educational Codeforces Round 98 (Rated for Div. 2)   // URL: https://codeforces.com/contest/1452/problem/A   // Memory Limit: 256 MB   // Time Limit: 2000 ms   // Powered by CP Editor (https://github.com/cpeditor/cpeditor)      #include <bits/stdc++.h>      using namespace std;      #define fi first   #define se second   #define pb push_back   #define int long long   #define F(i, n) for (int i = 0; i < n; i++)   #define all(x)(x).begin(), (x).end()      using ld = long double;   using vi = vector <int>;   using mi = map <int, int>;   using pii = pair <int, int>;      const int N = 100005;   const int MOD = 1e9 + 7;      #define show(arr) { for (auto x: arr) cout << x <<    ; cout <<   n ; }   #define show_(arr, n) F(i, n) show(arr[i])      void solve() {    int n; cin>>n;    int a[n],sum=0,mx=0;    F(i,n){     cin>>a[i];     sum+=a[i];     mx=max(mx,a[i]);    }    cout<<max((n-1)*((sum+n-2)/(n-1)),mx*(n-1))-sum<<  n ;   }   int32_t main() {      ios::sync_with_stdio(false);      cin.tie(0);      cout.tie(0);         int32_t t=1;      cin >> t;      while (t--) solve();         return 0;   }
#include <bits/stdc++.h>   using namespace std;   using namespace std;   int k;   string ad;   vector<string> c(string s) {     vector<string> ans;     int last = 0;     for (long long i = 0; i < (s.size()); i++) {       if (s[i] ==     || s[i] ==  -  || i == s.size() - 1) {         ans.push_back(s.substr(last, i - last + 1));         last = i + 1;       }     }     return ans;   }   vector<string> v;   bool check(int mid) {     int nlen = 0;     int cnt = 1;     for (long long i = 0; i < (v.size()); i++) {       if (v[i].size() > mid) {         return false;       }       if (nlen + v[i].size() <= mid) {         nlen += v[i].size();       } else {         nlen = v[i].size();         cnt++;       }     }     if (cnt <= k) {       return true;     }     return false;   }   int main() {     cin >> k;     cin.ignore();     getline(cin, ad,   n );     v = c(ad);     int l = 0, r = ad.size();     while (l < r) {       int mid = (l + r) >> 1;       if (!check(mid)) {         l = mid + 1;       } else {         r = mid;       }     }     cout << r;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long a, b, r, x;   int main() {     cin >> a;     b = 45 * 17 * 10000000000000000LL;     r = 100000000000000000LL - 1;     x = b % a;     cout << a - x <<     << a - x + r;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename _T>   inline void read(_T &f) {     f = 0;     _T fu = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) {         fu = -1;       }       c = getchar();     }     while (c >=  0  && c <=  9 ) {       f = (f << 3) + (f << 1) + (c & 15);       c = getchar();     }     f *= fu;   }   template <typename T>   void print(T x) {     if (x < 0) putchar( - ), x = -x;     if (x < 10)       putchar(x + 48);     else       print(x / 10), putchar(x % 10 + 48);   }   template <typename T>   void print(T x, char t) {     print(x);     putchar(t);   }   const int N = 1e6 + 5;   struct ele {     int f, g;     ele(int a = 0, int b = 0) : f(a), g(b) {}     bool operator<(const ele A) const {       if (f != A.f) return f < A.f;       return g > A.g;     }   };   ele operator+(const ele a, const ele b) { return ele(a.f + b.f, a.g + b.g); }   ele f[N];   char c[N];   int s[N];   int n, k, l;   ele dp(int mid) {     for (int i = 0; i < l; i++) f[i] = ele(0, 0);     for (int i = l; i <= n; i++)       f[i] = max(f[i - 1], f[i - l] + ele(s[i] - s[i - l] - mid, 1));     return f[n];   }   int solve(int opt) {     for (int i = 1; i <= n; i++)       s[i] = s[i - 1] + ((c[i] >=  a  && c[i] <=  z ) == opt);     int l = 0, r = n - 1, ans = n;     while (l <= r) {       int mid = (l + r) >> 1;       if (dp(mid).g <= k)         ans = mid, r = mid - 1;       else         l = mid + 1;     }     return s[n] - (dp(ans).f + ans * k);   }   int main() {     read(n);     read(k);     read(l);     scanf( %s , c + 1);     if (1ll * k * l > n) {       print(0,   n );       return 0;     }     print(min(solve(0), solve(1)),   n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 100005;   int legs[N];   bool hide[N];   vector<vector<int> > adj;   void dfs(int v, int par = 0) {     if (adj[v].size() <= 2u) {       hide[v] = true;       for (int i : adj[v]) {         if (i != par) {           dfs(i, v);         }       }     }   }   int main() {     int n, a, b;     scanf( %d , &n);     adj.resize(n + 1);     for (int i = 0; i < n - 1; ++i) {       scanf( %d%d , &a, &b);       adj[a].push_back(b);       adj[b].push_back(a);     }     for (int i = 1; i <= n; ++i) {       if (adj[i].size() == 1u) {         dfs(i);       }     }     for (int i = 1; i <= n; ++i) {       for (int j : adj[i]) {         if (hide[j]) {           legs[i] = min(2, legs[i] + 1);         }       }     }     for (int i = 1; i <= n; ++i) {       if (!hide[i]) {         int count = 0;         for (int j : adj[i]) {           if (!hide[j] && (int)adj[j].size() - legs[j] > 1) {             ++count;           }         }         if (count > 2) {           printf( No n );           return 0;         }       }     }     printf( Yes n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int parent[5001][5001];   int dp[5001][5001];   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     long long n, m, t;     cin >> n >> m >> t;     vector<int> u(m + 1), v(m + 1), tm(m + 1);     for (int i = 1; i <= m; i++) {       cin >> u[i] >> v[i] >> tm[i];     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         dp[i][j] = 1e9 + 5;       }     }     dp[1][1] = 0;     for (int i = 2; i <= n; i++) {       for (int j = 1; j <= m; j++) {         int p = u[j];         int cur = v[j];         if (dp[p][i - 1] + tm[j] < dp[cur][i]) {           dp[cur][i] = dp[p][i - 1] + tm[j];           parent[cur][i] = p;         }       }     }     int res = 0;     for (int i = n; i > 0; i--) {       if (dp[n][i] <= t) {         res = i;         break;       }     }     cout << res << endl;     int ind = n;     int visit = res;     vector<int> path;     while (ind != 0) {       path.push_back(ind);       ind = parent[ind][visit--];     }     for (int i = path.size() - 1; i >= 0; i--) {       cout << path[i] <<    ;     }     cout << endl;   }