malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   const long long inf = 1e12;   int n, h[N], w[N], u, v;   long long ans, f[N][2];   vector<int> g[N];   void dfs(int u, int fa) {     vector<long long> vec;     long long sum = 0;     for (auto v : g[u]) {       if (v ^ fa) {         dfs(v, u);         sum += f[v][0];         vec.push_back(f[v][1] - f[v][0]);       }     }     sort(vec.begin(), vec.end());     for (int t = 0; t < 2; ++t) {       f[u][t] = inf;       if (u > 1 && ((!t && h[fa] > h[u]) || (t && h[fa] < h[u]))) {         continue;       }       int out = vec.size() + t;       long long cur = sum;       for (int i = 0; ~out; ++i, --out) {         if (i && i <= vec.size()) {           cur += vec[i - 1];         }         f[u][t] =             min(f[u][t], cur + 1ll * w[u] * max(out, (int)g[u].size() - out));       }     }   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &w[i]);     }     for (int i = 1; i <= n; ++i) {       scanf( %d , &h[i]);     }     for (int i = 1, u, v; i < n; ++i) {       scanf( %d%d , &u, &v);       g[u].push_back(v);       g[v].push_back(u);     }     dfs(1, 0);     printf( %lld n , f[1][0]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 4e5 + 5;   int head[maxn], e[maxn << 1], nxt[maxn << 1], cnt = 1;   void addedge(int u, int v) {     e[++cnt] = v, nxt[cnt] = head[u], head[u] = cnt;     e[++cnt] = u, nxt[cnt] = head[v], head[v] = cnt;   }   int s[maxn], t[maxn];   int f[maxn], num;   int d[maxn];   int n, C = 2e5;   void dfs(int u) {     while (head[u] && f[head[u] >> 1]) head[u] = nxt[head[u]];     if (head[u]) {       f[head[u] >> 1] = ++num;       dfs(e[head[u]]);     }   }   int main() {     scanf( %d , &n);     for (int i = 1, x, y; i <= n; i++) {       scanf( %d%d , &x, &y);       d[x]++, d[y + C]++;       s[i] = x, t[i] = y + C;       addedge(x, y + C);     }     int last, tot = 0;     for (int i = 1; i <= 2 * C; i++)       if (d[i] % 2) addedge(i, 2 * C + 1);     dfs(2 * C + 1);     for (int i = 1; i <= 2 * C; i++) dfs(i);     memset(d, 0, sizeof(d));     for (int i = 1; i <= n; i++) {       if (f[i] % 2)         d[s[i]]++, d[t[i]]++;       else         d[s[i]]--, d[t[i]]--;     }     for (int i = 1; i <= 2 * C; i++)       if (d[i] > 1 || d[i] < -1) {         printf( shaleba n );         return 0;       }     for (int i = 1; i <= n; i++) {       if (f[i] % 2)         printf( r );       else         printf( b );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long T[1001][1001];   int main() {     long long n;     cin >> n;     long long a[n];     for (long long i = 0; i < n; i++) cin >> a[i];     for (int i = 0; i < 1001; i++) {       for (int j = 0; j <= i; j++) {         if (j == 0 || i == j)           T[i][j] = 1;         else           T[i][j] = (T[i - 1][j] + T[i - 1][j - 1]) % 998244353;       }     }     long long L[n + 1];     L[n] = 1;     for (long long i = n - 1; i >= 0; i--) {       if (a[i] <= 0 || (i + a[i]) >= n) {         L[i] = 0;       } else {         long long s = 0;         for (long long k = i + a[i]; k < n; k++) {           s = (s + (T[k - i][a[i]] * (L[k + 1])) % 998244353) % 998244353;         }         L[i] = s;       }     }     long long ans = 0;     for (int i = 0; i < n; i++) ans = (ans + L[i]) % 998244353;     cout << ans;   }
#include <bits/stdc++.h>   using namespace std;   const long long N = 2e5 + 10;   long long n;   long long pre[N];   long long cal(long long x) {     if (x == n * (n - 1) + 1) return 1;     long long p = lower_bound(pre + 1, pre + n + 1, x) - pre;     long long b = x - pre[p - 1];     if (b & 1)       return p;     else       return p + b / 2;   }   signed main() {     long long t;     cin >> t;     while (t--) {       long long l, r;       cin >> n >> l >> r;       for (long long i = 1; i <= n; i++) pre[i] = pre[i - 1] + 2 * (n - i);       for (long long i = l; i <= r; i++) cout << cal(i) <<    ;       puts(  );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int siz = 1e5 + 7;   const int siz2 = 2e5 + 7;   int t, n;   int a[222];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> t;   A:     while (t--) {       cin >> n;       map<int, int> mp;       vector<int> Ans;       for (int i = 0; i < n; ++i) {         cin >> a[i];         mp[a[i]]++;       }       if (n > 1) {         for (int i = 0; i < n; ++i) {           for (int j = a[i]; j <= 2 * n; ++j) {             if (!mp[j]) {               mp[j]++;               Ans.push_back(a[i]);               Ans.push_back(j);               break;             }           }         }         if (Ans.size() != 2 * n) {           cout << -1 << endl;           goto A;         }       } else {         if (a[0] == 1) {           cout << 1 <<     << 2 << endl;           goto A;         } else {           cout << -1 << endl;           goto A;         }       }       for (auto it : Ans) cout << it <<    ;       cout << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 20 * 1000 + 200;   const int INF = 1000 * 1000 * 1000;   int d[N];   int n, m;   inline void read() { scanf( %d %d , &n, &m); }   inline void solve() {     for (int i = 0; i < N; i++) d[i] = INF;     queue<int> q;     q.push(n);     d[n] = 0;     while (!q.empty()) {       int v = q.front();       q.pop();       if (v * 2 <= 20 * 1000 && d[v * 2] == INF) {         q.push(v * 2);         d[v * 2] = d[v] + 1;       }       if (v - 1 > 0 && d[v - 1] == INF) {         q.push(v - 1);         d[v - 1] = d[v] + 1;       }     }     cout << d[m];   }   int main() {     read();     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1e9 + 7;   int N, M;   int dist[200000];   int interval[200000];   int cnt[200000];   int main(void) {     cin >> N;     for (int i = 0; i < (int)(N); ++i) scanf( %d , dist + i);     for (int i = 0; i < (int)(N - 1); ++i) interval[i] = dist[i + 1] - dist[i];     cin >> M;     map<int, int> _ma;     for (int i = 0; i < (int)(M); ++i) {       int b;       scanf( %d , &b);       cnt[b - 1]++;       _ma[b - 1]++;     }     vector<int> candid;     for (int i = 1; i < (int)(N - 1); ++i)       if (cnt[i - 1] != cnt[i] or cnt[i] != cnt[i + 1])         if (cnt[i]) candid.push_back(i);     candid.push_back(0);     candid.push_back(N - 1);     long long res = -1;     for (int s : candid) {       for (int sd = -1; sd <= 1; sd += 2) {         map<int, int> ma = _ma;         int now = s, d = sd;         long long sum = 0;         ma[now]--;         if (ma[now] == 0) ma.erase(now);         while (true) {           if (ma.find(now) == ma.end() and ma.size() == 0) {             if (res == -1 or res == sum) {               res = sum;               break;             } else {               cout << -1 << endl;               return 0;             }           }           now = now + d;           if (!(0 <= (now) && (now) < (N))) {             d *= -1;             now += 2 * d;           }           sum += interval[min(now, now - d)];           if (ma[now] == 0) break;           ma[now]--;           if (ma[now] == 0) ma.erase(now);         }       }     }     cout << res << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int G[100010];   int A[1000];   int g(int q) {     int n = 2, m, i, g, t, nn;     memset(A, 0, sizeof(A));     n = 2;     nn = -1;     while (q >= n * (n + 1) / 2) {       m = q - n * (n + 1) / 2;       if (m % n != 0) {         n++;         continue;       }       m /= n;       t = 0;       for (i = m + 1; i < m + n + 1; i++) {         t ^= G[i];       }       A[t] = 1;       if ((t == 0) && (nn == -1)) nn = n;       n++;     }     for (i = 0; i < 1000; i++)       if (A[i] == 0) break;     G[q] = i;     return nn;   }   int main() {     int i, n, k, j, d;     memset(G, 0xFF, sizeof(G));     scanf( %d , &n);     for (i = 0; i < n; i++) g(i);     printf( %d n , g(n));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e6 + 15;   int cc[N];   long long sum[N];   long long getSeg(int pos, int p, int x, int y) {     int last = pos + p - 1;     int t = x / y;     int ed = max(pos, last - t);     long long ret = (long long)(cc[ed] - cc[pos]) * x;     long long tot = (long long)(cc[last] - cc[ed]);     long long u = pos + p;     tot *= u;     long long totSum = sum[last] - sum[ed];     ret += (tot - totSum) * y;     return ret;   }   bool isPrime[N];   void solve() {     int n, x, y;     scanf( %d%d%d , &n, &x, &y);     for (int i = 0; i < n; ++i) {       int a;       scanf( %d , &a);       ++cc[a];       sum[a] += a;     }     for (int i = 1; i < N; ++i) {       cc[i] += cc[i - 1];       sum[i] += sum[i - 1];     }     for (int i = 2; i < N; ++i) {       if (!isPrime[i]) {         for (int j = i + i; j < N; j += i) isPrime[j] = true;       }     }     long long ret = 1e18;     for (int i = 2; i <= 1e6; ++i) {       if (isPrime[i]) continue;       long long tot = 0;       for (int pos = 0; pos <= 1e6; pos += i) {         tot += getSeg(pos, i, x, y);       }       ret = min(ret, tot);     }     cout << ret << endl;   }   int main() { solve(); }
#include <bits/stdc++.h>   using namespace std;   string f[300], t[300], e[300];   char s[20];   int n;   bool cmp(string& a, string b) {     if (a ==   ) return 1;     return a.size() == b.size() ? a > b : a.size() > b.size();   }   int main() {     f[240] =  x ;     f[204] =  y ;     f[170] =  z ;     while (1) {       bool ok = 0;       for (int i = 0; i < 256; i++)         if (e[i] !=    && cmp(f[i],  (  + e[i] +  ) )) {           ok = 1;           f[i] =  (  + e[i] +  ) ;         }       for (int i = 0; i < 256; i++)         if (f[i] !=    && cmp(f[i ^ ((1 << 8) - 1)],  !  + f[i])) {           f[i ^ ((1 << 8) - 1)] =  !  + f[i];           ok = 1;         }       for (int i = 0; i < 256; i++)         if (f[i] !=    && cmp(t[i], f[i])) {           t[i] = f[i];           ok = 1;         }       for (int i = 0; i < 256; i++)         if (t[i] !=   )           for (int j = 0; j < 256; j++)             if (t[j] !=    && cmp(t[i & j], t[i] +  &  + t[j])) {               t[i & j] = t[i] +  &  + t[j];               ok = 1;             }       for (int i = 0; i < 256; i++)         if (t[i] !=    && cmp(e[i], t[i])) {           e[i] = t[i];           ok = 1;         }       for (int i = 0; i < 256; i++)         if (e[i] !=   )           for (int j = 0; j < 256; j++)             if (e[j] !=    && cmp(e[i | j], e[i] +  |  + e[j])) {               e[i | j] = e[i] +  |  + e[j];               ok = 1;             }       if (!ok) break;     }     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %s , s);       int c = 0;       for (int j = 0; j < 8; j++) c |= ((s[j] -  0 ) << j);       cout << e[c] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   void Max(T &a, T b) {     if (b > a) a = b;   }   template <class T>   void Min(T &a, T b) {     if (b < a) a = b;   }   template <class T>   T gcd(T a, T b) {     return b ? gcd(b, a % b) : a;   }   int main() {     ios::sync_with_stdio(false);     int n;     cin >> n;     long long ans = 0;     for (int i = (3), _t = (n + 1); i < _t; ++i) {       ans += (i - 1) * i;     }     cout << ans << endl;     return 0;   }
#include<bits/stdc++.h>   #define __fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)   #define MOD 1000000007   using namespace std;       typedef long long int ll;           int tc;       void solve()   {       int n,w,x;       multiset<int>st;       vector<int>r;       cin>>n>>w;       for(int i=0; i<n; i++){           cin>>x;           r.push_back(x);       }       sort(r.rbegin(),r.rend());           int h=0;       for(int i=0; i<n; i++){           if(!st.empty() && r[i]<= *(st.rbegin()) ){               x=*(st.rbegin());               st.erase( st.lower_bound(x) );               x-=r[i];               if(x>0)st.insert(x);           }           else{               h++;               if(w-r[i]>0)st.insert(w-r[i]);           }       }       cout<<h<<endl;   }       int main()   {           __fastIO;           cin>>tc; while(tc--)solve();           return 0;   }
#include <bits/stdc++.h>   using namespace std;   int x[10], p[50], dp[200][200];   int pp[50], q[50];   int n, k;   int maxi;   double suma;   int nm = 0;   void check(int a, int b, int c, int d) {     nm++;     for (int i = 1; i <= n; i++) p[i] = x[i];     int br = q[a];     for (int j = pp[a]; j <= ((q[a] + pp[a]) >> 1); j++) {       int pom = p[j];       p[j] = p[br];       p[br] = pom;       br--;     }     if (b > 0) {       int br = q[b];       for (int j = pp[b]; j <= ((q[b] + pp[b]) >> 1); j++) {         int pom = p[j];         p[j] = p[br];         p[br] = pom;         br--;       }     }     if (c > 0) {       int br = q[c];       for (int j = pp[c]; j <= ((q[c] + pp[c]) >> 1); j++) {         int pom = p[j];         p[j] = p[br];         p[br] = pom;         br--;       }     }     if (d > 0) {       int br = q[d];       for (int j = pp[d]; j <= ((q[d] + pp[d]) >> 1); j++) {         int pom = p[j];         p[j] = p[br];         p[br] = pom;         br--;       }     }     int cnt = 0;     for (int j = 1; j <= n; j++)       for (int f = (j + 1); f <= n; f++)         if (p[j] > p[f]) cnt++;     suma = suma + cnt;   }   int main() {     srand(0);     scanf( %d%d , &n, &k);     suma = 0.0;     maxi = n * (n + 1) / 2;     int pom = maxi;     for (int i = 2; i <= 4; i++) {       if (k < i) break;       pom = pom * maxi;     }     maxi = pom;     for (int i = 1; i <= n; i++) scanf( %d , &x[i]);     int cnt = 0;     for (int i = 1; i <= n; i++)       for (int j = i; j <= n; j++)         if (x[i] > x[j]) cnt++;     int i, j, i1, i2, j1, j2;     int br = 0;     for (i = 1; i <= n; i++)       for (j = i; j <= n; j++) {         pp[++br] = i;         q[br] = j;       }     int ppp = 0;     for (i = 1; i <= br; i++) {       if (k == 1) {         check(i, 0, 0, 0);         continue;       }       for (j = 1; j <= br; j++) {         if (k == 2) {           check(i, j, 0, 0);           continue;         }         for (i1 = 1; i1 <= br; i1++) {           if (k == 3) {             check(i, j, i1, 0);             continue;           }           for (i2 = 1; i2 <= br; i2++) {             check(i, j, i1, i2);             ppp++;           }         }       }     }     suma = suma / maxi;     printf( %.11lf n , suma);   }
#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:512000000 )   using namespace std;   void solve(bool);   void precalc();   clock_t start;   int main() {     start = clock();     int t = 1;     cout.sync_with_stdio(0);     cin.tie(0);     precalc();     cout.precision(10);     cout << fixed;     int testNum = 1;     while (t--) {       solve(true);     }     cout.flush();     return 0;   }   template <typename T>   T binpow(T q, T w, T mod) {     if (!w) return 1 % mod;     if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod;     return binpow(q * 1LL * q % mod, w / 2, mod);   }   template <typename T>   T gcd(T q, T w) {     while (w) {       q %= w;       swap(q, w);     }     return q;   }   template <typename T>   T lcm(T q, T w) {     return q / gcd(q, w) * w;   }   template <typename T>   void make_unique(vector<T>& a) {     sort(a.begin(), a.end());     a.erase(unique(a.begin(), a.end()), a.end());   }   template <typename T>   void relax_min(T& cur, T val) {     cur = min(cur, val);   }   template <typename T>   void relax_max(T& cur, T val) {     cur = max(cur, val);   }   void precalc() {}   void solve(bool read) {     long long n, Q;     cin >> n >> Q;     while (Q--) {       long long x;       cin >> x;       --x;       long long cur_n = n;       long long pref = 0;       long long steps = 0;       bool printed = false;       while (x % 2 != steps) {         if (cur_n == 1) {           cout << pref + 1 <<   n ;           printed = true;           break;         }         long long dec = (cur_n + 1 - steps) / 2;         pref += dec;         steps = steps + 2 * (dec - 1) == cur_n - 1;         cur_n -= dec;         x /= 2;       }       if (!printed) {         cout << pref + (x - steps) / 2 + 1 << endl;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int arr[102][102] = {0};   int main() {     int n, m, a, b, sum = -1, minsum = -1;     scanf( %d%d , &n, &m);     int cost[n + 1];     for (int i = 1; i <= n; i++) {       scanf( %d , &cost[i]);     }     for (int i = 1; i <= m; i++) {       scanf( %d%d , &a, &b);       arr[a][b] = arr[b][a] = 1;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= n; j++) {         for (int k = 1; k <= n; k++) {           if (sum == -1 && j != k && i != j && j != k && arr[i][j] == 1 &&               arr[j][k] == 1 && arr[k][i] == 1)             minsum = sum = cost[i] + cost[j] + cost[k];           else if (j != k && i != j && j != k && arr[i][j] == 1 &&                    arr[j][k] == 1 && arr[k][i] == 1) {             sum = cost[i] + cost[j] + cost[k];             if (minsum > sum) minsum = sum;           }         }       }     }     printf( %d n , minsum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = (int)1e6;   long long n, m, a;   int main() {     cin >> a;     if (a == 1) {       cout <<  1 1  n 1 ;       return 0;     }     cout << (a - 1) * 2 <<     << 2 <<   n ;     cout <<  1 2 ;   }
#include <bits/stdc++.h>   using namespace std;   int MOD = 1000000007;   int dp[1001][1001];   int main() {     int k;     cin >> k;     int balls[1000];     int n = 0;     for (int i = 0; i < k; i++) {       cin >> balls[i];       n += balls[i];     }     long long l = 1;     for (int i = 0; i <= n; i++) {       dp[i][0] = 1;       dp[i][i] = 1;       for (int j = 1; j <= i; j++) {         dp[i][j] = (dp[i - 1][j] + dp[i - 1][j - 1]) % MOD;       }     }     long long ans = 1;     int cur = balls[0];     for (int i = 1; i < k; i++) {       cur += balls[i];       ans = ((ans % MOD) * (dp[cur - 1][balls[i] - 1]) % MOD) % MOD;     }     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 3e5 + 5;   const int inf = 0x3f3f3f3f;   const int mod = 1e9 + 7;   int T, n, m;   string s;   int vis[26];   int pos[maxn][26];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cin >> T;     while (T--) {       memset(vis, 0, sizeof(vis));       cin >> n >> m;       for (int i = 0; i < 26; ++i) {         for (int j = 0; j <= n + 5; ++j) {           pos[j][i] = 0;         }       }       cin >> s;       for (int i = 0; i < s.size(); ++i) {         for (int j = 0; j < 26; ++j) {           pos[i][j] = pos[i - 1 == -1 ? 0 : i - 1][j];         }         pos[i][s[i] -  a ]++;       }       for (int i = 0, x; i < m; ++i) {         cin >> x;         x--;         for (int j = 0; j < 26; ++j) {           vis[j] += pos[x][j];         }       }       for (int j = 0; j < 26; ++j) {         vis[j] += pos[n - 1][j];       }       for (int i = 0; i < 26; ++i) {         cout << vis[i] <<    ;       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   class BIT {     vector<int> tree;       public:     BIT(int n) : tree(n + 1, 0) {}     BIT(const vector<int>& Arg) {       tree.resize(Arg.size());       for (int i = 0; i < tree.size(); i++) increase(i, Arg[i]);     }     void increase(int i, int delta) {       for (; i < (int)tree.size(); i |= i + 1) tree[i] += delta;     }     int getsum(int left, int right) { return sum(right) - sum(left - 1); }     int sum(int ind) {       int sum = 0;       while (ind >= 0) {         sum += tree[ind];         ind &= ind + 1;         ind--;       }       return sum;     }   };   int main(int argc, char const* argv[]) {     int n, k, a, b, q;     scanf( %d %d , &n, &k);     scanf( %d %d , &a, &b);     scanf( %d , &q);     BIT bita(n + 10);     BIT bitb(n + 10);     for (int i = 0; i < q; i += 1) {       int op;       scanf( %d , &op);       if (op == 1) {         int di, ai;         scanf( %d %d , &di, &ai);         bitb.increase(di, min(b, ai));         bita.increase(di, min(a, ai));         int sb = bitb.getsum(di, di);         int sa = bita.getsum(di, di);         if (sb > b) bitb.increase(di, b - sb);         if (sa > a) bita.increase(di, a - sa);       } else {         int p;         scanf( %d , &p);         int nxt = p + k;         int s = 0;         if (p > 1) s += bitb.getsum(1, p - 1);         if (nxt <= n) s += bita.getsum(nxt, n);         printf( %d n , s);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n = 0, cc = 0, cn = 0, ic = 0, in1 = 0;     int pr;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> pr;       if (pr % 2 == 1) {         cn++;         in1 = i;       } else {         cc++;         ic = i;       }     }     int answer = cn > cc ? ic : in1;     cout << answer;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e3 + 10, oo = maxn;   int n, m;   char s[maxn], t[maxn];   int a[maxn];   int dp[maxn][maxn];   void myMax(int &a, int b) {     if (a < b) {       a = b;     }   }   int main() {     int i, j, k, x, y;     scanf( %s , s + 1);     n = strlen(s + 1);     scanf( %s , t + 1);     m = strlen(t + 1);     for (i = n; i >= m; i--) {       for (j = i, k = 0; j; j--) {         if (s[j] == t[m - k] && ++k == m) {           break;         }       }       a[i] = k == m ? i - j + 1 : oo;     }     for (; i; i--) {       a[i] = oo;     }     dp[0][0] = 0;     for (i = (1); i <= (n); i++) {       for (j = (0); j <= (i); j++) {         dp[i][j] = 0;         if (j < i) {           dp[i][j] = dp[i - 1][j];         }         if (j) {           myMax(dp[i][j], dp[i - 1][j - 1]);         }         if (a[i] != oo && (y = j - a[i] + m) >= 0 && (x = i - a[i]) >= y) {           myMax(dp[i][j], dp[x][y] + 1);         }       }     }     for (i = (0); i <= (n); i++) {       printf( %d%c , dp[n][i],    n [i == n]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long i, j, k, n, t, s, p[100009], d[100009], a[100009];   int main() {     cin >> n;     for (i = 1; i <= n; i++) cin >> p[i];     for (i = 1; i <= n; i++)       if (!d[i]) {         k = 0;         j = i;         while (d[j] == 0) {           d[j] = 1;           j = p[j];           k++;         }         d[i] = 1;         k = max(1LL, k);         a[++t] = k;       }     sort(a + 1, a + t + 1);     for (i = 1; i <= t - 2; i++) s += a[i] * 1LL * a[i];     k = a[t - 1] + a[t];     s += k * 1LL * k;     cout << s;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     int score = 0;     string str;     cin >> str;     for (int j = 0; j < str.size(); ++j) {       if (str[j] ==  A ) score += 1;       if (str[j] ==  D ) {         score -= 1;       }     }     if (score > 0) cout <<  Anton ;     if (score < 0) cout <<  Danik ;     if (score == 0) cout <<  Friendship ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int INF = 1000 * 1000 * 1000;   const long long INF64 = 1LL * INF * INF;   const long long mod = INF + 7;   int m[10][10];   bool check(int k, int x, int y, int w, int h) {     int ans = 0;     for (int i = x; i <= h; ++i) {       for (int j = y; j <= w; ++j) {         ans += m[i][j];       }     }     return ans >= k;   }   void solve() {     int r, c, n, k;     cin >> r >> c >> n >> k;     for (int i = 0; i < n; ++i) {       int x, y;       cin >> x >> y;       x--;       y--;       m[x][y] = 1;     }     int ans = 0;     for (int i = 0; i < r; ++i) {       for (int j = 0; j < c; ++j) {         for (int w = j; w < c; ++w) {           for (int h = i; h < r; ++h) {             ans += check(k, i, j, w, h);           }         }       }     }     cout << ans << endl;   }   int main() {     ios_base::sync_with_stdio(false);     solve();     return 0;   }