solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; long long L, R, n; int c[10], a[19], b[19]; int ret; bool check(int step, int lim) { if (step < 0) return 1; if (lim == 3) { int l = b[step], u = a[step]; if (l == u) { if (!c[l]) return 0; --c[l]; int res = check(step - 1, 3); ++c[l]; ...
9
#include <bits/stdc++.h> using namespace std; int n, m; int from[5050], to[5050]; vector<pair<int, int> > v[1010]; int d[1010], cnt[1010]; bool in[1010], out[1010]; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> from[i] >> to[i]; from[i]--, to[i]--; v[fro...
9
#include<iostream> #include<cstdio> #include<cmath> #include<string> #include<algorithm> #include<vector> #include<map> #include<stack> #include<queue> #include<set> #include<cstring> #define ESP 10e-9 #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x); using namespace std; vector<pair<pair<int,int>,int> >odd; v...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int mod[2] = {98765431, (int)1e9 + 9}; int base = 71; int n; int a[N]; int b[N], c[N]; int num[N]; map<pair<long long, long long>, int> mp; int nbit(int x) { int ans = 0; while (x) ans += x & 1, x /= 2; return ans; } int32_t main() { ios::syn...
8
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 5; const long long Linf = 1e18 + 5; template <class T> inline void umax(T &a, T b) { if (a < b) a = b; } template <class T> inline void umin(T &a, T b) { if (a > b) a = b; } template <class T> inline T abs(T a) { return a > 0 ? a : -a; } template...
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int a[N], n; int dp[N][2]; int solve(int idx, int player) { if (idx >= n) return 0; if (dp[idx][player] != -1) return dp[idx][player]; int op1 = solve(idx + 1, 1 - player) + (player * a[idx]); int op2 = solve(idx + 2, 1 - player) + ((player) *...
3
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); } const int MAXN = 100010; const long long MOD = 1e9 + 7; int sum[MAXN]; int src[MAXN], N, num; int val[MAXN], cnt[MAXN]; long long pow_mod(long long x, long long cnt) { long long ret = 1; while (cnt) { ...
6
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); template <class T = int> T __rand(T range = numeric_limits<T>::max()) { return (T)(rng() % range); } template <class T> ostream& operator,(ostream& out, const T& thing) { return out << ", " << thing; }...
9
#include <bits/stdc++.h> const int MAXN = 5010; long long dp[2][MAXN]; void getmin(long long& x, long long y) { x > y ? x = y : 0; } int X[MAXN], A[MAXN], B[MAXN], C[MAXN], D[MAXN]; int L[2][MAXN], R[2][MAXN]; int n, S, E; inline int tr(int x) { return x == 1 ? -1 : 1; } int main() { std::ios_base::sync_with_stdio(fa...
8
#include <bits/stdc++.h> using namespace std; int n, x, y, x2, y2; struct rct { int x, y, x2, y2; } v[200005]; struct rctXd { int val; bool operator<(const rctXd& oth) const { if (v[val].x2 == v[oth.val].x2) { if (v[val].x == v[oth.val].x) return val < oth.val; return v[val].x < v[oth.val].x; ...
11
#include <bits/stdc++.h> using namespace std; int a[109], s, qzh[109], n, m, r, l; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; qzh[i] = qzh[i - 1] + a[i]; } for (int i = 1; i <= m; i++) { cin >> l >> r; if (qzh[r] > qzh[l - 1]) s += qzh[r] - qzh[l - 1]; } cout << ...
2
#include <bits/stdc++.h> using namespace std; set<int> s; int main() { int n; cin >> n; int k = 0; int max = 0; for (int i = 1; i <= 2 * n; i++) { int x; cin >> x; if (s.find(x) == s.end()) { k++; s.insert(x); } else k--; if (max < k) max = k; } cout << max; }
0
#include <bits/stdc++.h> char s[1001000], kkk; int ans[1001000], at = 0; int main() { gets(s); int st = 0, ls, ed, flag = 1; ans[at++] = 0; for (ed = st; s[ed] != '.'; ed++) ; if (ed > 8 || ed < 1) flag = 0; st = ed + 1; for (; flag;) { ed = st; for (; s[ed] && s[ed] != '.'; ed++) ; ...
3
#include <bits/stdc++.h> using namespace std; const int N = 1100005; char s[N]; int T, h, t, n, m = 1, cur = 1, ans, l[15], r[15], c[N][11], a[N][27], L[N], par[N], d[N], f[N]; void extend(int i, int t, int ch) { int x = cur, y = cur = ++m; L[y] = i, ++c[y][t]; for (; x && !a[x][ch]; x = par[x]) a...
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int pol = x1 + y1; int mn = min(x2, y2); int vas = mn + x2 - mn + y2 - mn; if (pol <= vas || (x1 <= x2 && y1 <= y2)) { cout << "Poly...
4
#include <bits/stdc++.h> using namespace std; const int N = 70009; string s, a, b; int n, q; int id[500009], tot, len[N], F[431][N], p[5][N], wt[N], cnt; vector<int> vt[N]; int H(string s) { int sum = 0; for (auto it : s) sum = sum * 26 + it - 'a' + 1; return sum; } int main() { cin >> s >> q; n = s.length();...
8
#include <bits/stdc++.h> using namespace std; template <typename T> bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; } template <typename T> bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; } const int maxN = 2000 + 5; const int P = 1000000007; int n, m; char b[maxN][maxN]; int s...
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int MOD = 1e9 + 7; long long pow(long long a, long long b, long long m) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } vector<int> parent; long long ans1 = 0; int fin...
5
#include <bits/stdc++.h> const int MXN = 1e6 + 5; const long long MNN = 1e5 + 1; const long long MOD = 1000000007; const long long INF = 1e18; const int OO = 1e9 + 500; using namespace std; long long n, cnt[MXN], num[MXN], up[MXN]; long long ans; vector<pair<long long, long long> > g[MXN]; map<int, bool> mp; void dfs(i...
5
#include <bits/stdc++.h> using namespace std; int s[4005], dp[2005]; vector<int> v; int main() { int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= 2 * n; i++) cin >> s[i]; v.clear(); for (int i = 1; i <= 2 * n; i++) { int j = i; while (j + 1 <= 2 * n && s[j + 1] ...
5
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; int dp[100005][2]; vector<int> vis; bool draw = 0; stack<int> ans; int dfs(int s, int ch) { if (dp[s][ch] == 2) draw = 1; vis[s] = 1; if (dp[s][ch] == -1) { vector<int>::iterator it = g[s].begin(); dp[s][ch] = 2; int u = 0; for ...
6
#include <bits/stdc++.h> using namespace std; int m, n; int main() { cin >> m >> n; cout << m * n / 2; }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 100100, LG = 18; int n, head[MAX], to[MAX * 2], nxt[MAX * 2], Ecnt, par[MAX][LG], dpth[MAX]; class Node { public: int A[11], sz; Node() { memset(A, 100, sizeof A); sz = 0; } void insert(int v) { if (v > MAX || binary_search(A, A + 11, v)...
7
#include <bits/stdc++.h> using namespace std; const int Ntest = 1e5 + 5; long long gcd(long long a, long long b) { long long temp; while (b > 0) { temp = a % b; a = b; b = temp; } return a; } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long fpow(long long b, long long ...
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 15; int n, a; string solve() { map<int, int> m; cin >> n; for (int i = 0, ggdem = n; i < ggdem; i++) cin >> a, m[a]++; for (int i = 0, ggdem = 20; i < ggdem; i++) { if ((1 << i) == 2048) { if (m[1 << i]) return "YES"; return "NO";...
1
#include <bits/stdc++.h> using namespace std; const int N = 1000001; const double Eps = 1e-12; struct Point { double x, y; Point(double _x = 0, double _y = 0) { x = _x, y = _y; } }; Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } double operator*(Point A, Point B) { return A.x * B.y - A.y ...
9
#include <bits/stdc++.h> using namespace std; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = ml * ml * ml + 7; long long n, i, j; vector<long long> m; bool viv = false; string s; int main() { ios_base::sync_with_stdio(0); ...
2
#include <bits/stdc++.h> using namespace std; int arr[50 + 5]; bool Sum[(int)1e4 * 50 + 5]; vector<int> vec; int main() { int N, D, ans = 0, maxi = 0, tot = 0; scanf("%d %d", &N, &D); for (int i = 0; i < N; i++) { scanf("%d", &arr[i]); tot += arr[i]; } Sum[0] = 1; for (int i = 0; i < N; i++) { f...
7
#include <bits/stdc++.h> using namespace std; long long int MOD = 1000000007LL; long long int fastpow(long long int base, long long int exp) { long long int res = 1; while (exp > 0) { if (exp % 2 == 1) res = (res * base) % MOD; base = (base * base) % MOD; exp /= 2; } return res % MOD; } bool isprime...
2
#include <bits/stdc++.h> using namespace std; int main() { char s[1000010]; int ans = 0, M = 0; scanf("%s", s); for (int i = 0; i < strlen(s); i++) { if (s[i] == 'M') M++; else if (M) { ans++; ans = ans > M ? ans : M; } } printf("%d", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long rdtsc() { long long tmp; asm("rdtsc" : "=A"(tmp)); return tmp; } inline int myrand() { return abs((rand() << 15) ^ rand()); } inline int rnd(int x) { return myrand() % x; } void precalc() {} const int maxn = 2e5 + 10; struct tree { int x[4 * maxn]; int m...
4
#include <bits/stdc++.h> using namespace std; int func(string &t, string &p) { int c = 0; for (int i = 0; i < t.length() && c < p.length(); i++) { if (t[i] == p[c]) c++; } if (c == p.length()) return 1; return 0; } int main() { string t; string p; cin >> t >> p; int n = t.length(); vector<int> v...
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { int t; scanf("%d", &t); while (t--) { long long int n; cin >> n; if (n % 3 == 0) cout << n / 3 << " " << n / 3 << endl; else if (n % 3 == 1) cout << (n / 3 + 1) << " " << n / 3 << endl; else c...
0
#include <bits/stdc++.h> using namespace std; long long combinatorial(long long x1, long long x2) { long long sum = 1, sum2 = 1; long long i; for (i = x1; i > x1 - x2; i--) sum *= i; for (i = 1; i <= x2; i++) sum2 *= i; return sum / sum2; } int main() { long long int i, n, v[100000], count, x[3]; while (s...
3
#include <bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define endl "\n" #define int long long const int N = 2e5 + 5; int n; int a[N]; int32_t main() { IOS; int t; cin >> t; while(t--) { cin >> n; int ans = 0, sum = 0; for(int i = 1; i <= n; i++) {...
3
#include <bits/stdc++.h> using namespace std; struct bien1 { int x, y; }; struct bien { int sl; int itest; bien1 a[110]; }; bien d[200010]; int n; long long a[200010]; int kq; void timkhoang() { for (int i = 1; i < 100000; i++) { long long gt1 = (long long)i * i; for (int j = (i + 1); j <= (i + 500); ...
5
#include <bits/stdc++.h> using namespace std; struct Edge { int dest, back; long long f, c; }; struct PushRelabel { vector<vector<Edge>> g; vector<long long> ec; vector<Edge*> cur; vector<vector<int>> hs; vector<int> H; PushRelabel(int n) : g(n), ec(n), cur(n), hs(2 * n), H(n) {} void add_edge(int s, ...
8
#include <bits/stdc++.h> using namespace std; int n; int main() { ios_base::sync_with_stdio(0); cin >> n; if (n % 2) { cout << "black"; } else { cout << "white\n1 2"; } }
4
#include <bits/stdc++.h> using namespace std; int a[100000 + 5], u[100000 + 5], p[100000 + 5], r[100000 + 5]; int main() { int n, b; cin >> n; a[1] = 0; for (int i = 2; i <= n; i++) { for (int k = 2; 2 * i - k * (k - 1) > 0; k++) if ((2 * i - k * (k - 1)) % (2 * k) == 0) { b = (2 * i - k * (k ...
6
#include <bits/stdc++.h> using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; const int N = 2e5 + 30; struct SuffixArray { vector<int> sa, lcp; SuffixArray(const vector<int> &xx, int lim = 100030) { int n = ((int)(xx).size()) + 1, k = 0, a, b; vector<int> x((xx)...
10
#include <bits/stdc++.h> using namespace std; const int N = 60; long long n, arr[N], dp[N][2]; long long fun(long long i, long long flag) { if (i > n) { return 0; } if (dp[i][flag] != -1) { return dp[i][flag]; } if (flag == 1) { return dp[i][flag] = max(arr[i] + fun(i + 1, (long lon...
3
#include <bits/stdc++.h> using namespace std; long long int modpow(long long int a, long long int n, long long int temp) { long long int res = 1, y = a; while (n > 0) { if (n & 1) res = (res * y) % temp; y = (y * y) % temp; n /= 2; } return res % temp; } long long int nC2(long long int n) { return (...
6
#include <bits/stdc++.h> using namespace std; int n; string s[1001000]; int p[1001000]; const int MOD = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; map<int, int> f; for (int i = (1); i <= (n); ++i) { cin >> s[i] >> p[i]; if (s[i][1] == 'D') f[p[i...
6
#include <bits/stdc++.h> using namespace std; const int N = 777; int a[N][N]; double f[N][N], p[N][N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < (1 << n); i++) { for (int j = 0; j < (1 << n); j++) { scanf("%d", a[i] + j); } } for (int i = 0; i < (1 << n); i++) { p[0][i] = 1.0;...
6
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const long long maxn = 1005; const long long mod = 1e9 + 7; long long n, m; long long a[maxn][maxn]; long long vis[maxn]; long long rmax[maxn], cmax[maxn]; map<long long, long long> ma; map<long long, long long> r[maxn], c[maxn]; long long dp[maxn...
4
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.writ...
6
#include <bits/stdc++.h> using namespace std; int in() { int n; scanf("%d", &n); return n; } long long Lin() { long long n; scanf("%lld", &n); return n; } double Din() { double n; scanf("%lf", &n); return n; } const long long inf = 1e17; const long long mod = 1e9 + 7; const int N = 3e6 + 5; vector<int...
8
#include <bits/stdc++.h> using namespace std; struct Trie { int son[2], num; Trie() { memset(son, -1, sizeof son); num = 0; } } trie[300010 * 31]; int cnt = 1; int a[300010]; void Insert(int x) { int dep = 30, now = 1; trie[now].num++; while (dep >= 0) { int t = (x >> dep) & 1; if (trie[now]...
5
#include <bits/stdc++.h> using namespace std; char s1[200100], s2[200100]; vector<int> pos[26]; long long sum1[27][200100]; long long sum2[27][200100]; int main() { int n; scanf("%d", &n); long long res2 = 0; for (int i = (1); i < (n + 1); ++i) res2 += (n - i + 1) * 1ll * (n - i + 1); scanf("%s%s", &s1, &s2);...
6
#include <bits/stdc++.h> using namespace std; int n, m, flag, cnt, res, i, p; char str[2010]; int C[2010]; vector<pair<int, int>> V[2 * 2010]; void dfs(int i) { int j, sz, k, e; sz = V[i].size(); for (j = 0; j < sz; j++) { k = V[i][j].first; e = V[i][j].second; if (C[k] == -1) { if (e == 0) ...
8
#include <bits/stdc++.h> using namespace std; const double EPS = 1E-14; const long long mod = 1000000007; using namespace std; int n; long long m; int l, r; int a[55]; int main() { ios_base::sync_with_stdio(0); cin >> n >> m; l = 0; r = n - 1; long long p = 1LL << (n - 1); m--; for (int i = 0; i < n; i++)...
3
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const int MAXN = 100000 * 3 + 10, MAXL = 10 + 2; map<long long, pair<long long, long long> > f[MAXL]; long long n; pair<long long, long long> DFS(long long n, int Maxdigt) { if (n == 0 && Maxdigt == 0) return make_pair(0, 0); ...
1
#include <bits/stdc++.h> using namespace std; #define INF 1000000000 #define INFLL 0x3f3f3f3f3f3f3f3fLL #define EPS 10e-9 #define MOD 1000000007 #define mp make_pair #define mt make_tuple #define pb push_back #define st first #define nd second #define sz(v) int(v.size()) #define all(X) (X).begin(), (X).end() #define...
0
#include <bits/stdc++.h> using namespace std; long long T, t, n, k, d, i; int main() { scanf("%lld", &T); for (t = 1; t <= T; t++) { scanf("%lld%lld", &n, &k); d = n; for (i = 2; i <= n; i++) { if (n % i == 0) { d = i; break; } if (i * i >= n) break; } cout << n...
0
#include <bits/stdc++.h> using namespace std; inline int read() { int first = 0, f = 1; char ch = getchar(); for (; ch < '0' || ch > '9';) { if (ch == '-') f = -1; ch = getchar(); } for (; ch >= '0' && ch <= '9';) { first = first * 10 + ch - '0'; ch = getchar(); } return first * f; } int n...
7
#include <bits/stdc++.h> using namespace std; int a[1 << 20]; int n, k; namespace Flow { const int MAXN = (int)2e4 + 5; const int INF = (int)1e9; struct Edge { int to, f, c, w; Edge() : to(0), f(0), c(0), w(0) {} Edge(int to, int c, int w) : to(to), f(0), c(c), w(w) {} }; vector<int> adj[MAXN]; bool was[MAXN]; in...
10
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using vi = vector<int>; using pii = pair<int, int>; const int mxn = 1000006; int n, m, s, t; vi e[mxn]; bool v[mxn]; int p[mxn]; bool ins[mxn]; vector<pii> path[mxn]; int a[mxn][20]; int h[mxn]; void dfs(int x, int p) { v[x] = true...
12
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int l, r; cin >> l >> r; long long int x = r + 1; if (x & 1) x++; if ((l % x) >= (x / 2)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; ...
0
#include <bits/stdc++.h> using namespace std; int n; const int MAX_N = int(3e5) + 10; int p[MAX_N]; struct Event { int minL, maxL; int minR, maxR; Event() {} Event(int minL, int maxL, int minR, int maxR) : minL(minL), maxL(maxL), minR(minR), maxR(maxR) {} }; int nE; Event events[MAX_N * 2]; vector<Event*>...
10
#include <bits/stdc++.h> using namespace std; long long spf[101]; long long fac[101]; void sieve() { spf[1] = 1; for (long long i = 2; i < 101; i++) spf[i] = i; for (long long i = 4; i < 101; i += 2) spf[i] = 2; for (long long i = 3; i * i < 101; i++) { if (spf[i] == i) { for (long long j = i * i; j <...
6
#include <bits/stdc++.h> using namespace std; bool o; char ch, SSS[1 << 17], *A = SSS, *B = SSS; inline char gc() { return A == B && (B = (A = SSS) + fread(SSS, 1, 1 << 17, stdin), A == B) ? EOF : *A++; } template <typename _Tp> inline void rd(_Tp &x); template <typename _Tq> inline void wr(...
10
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m, x, y, k, i, j, ans = 0, c = 0, sum = 0; string s; cin >> n >> k; vector<long long int> v(n + 1); std::vector<long long int> vv(n + 1); for (i = 1; i <= n; i++) { cin >> v[i]; vv[i] = v[i]; } ans = LLONG_MIN; for (...
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 100500; const int MAXK = 1000; int n; int block_cnt; int block_sz; int l[MAXK], r[MAXK]; char s[MAXN]; int q; int a[MAXN], b[MAXN]; int lena[MAXN], lenb[MAXN]; char as[5], bs[5]; map<pair<int, int>, int> diff; map<int, vector<int> > all_block_lft; map<int, ...
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int i, cnt = 0; for (i = 1; i <= n / 2; i++) { if (n % i == 0) { cnt++; } } cout << cnt << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef struct pinfo { int k; vector<int> princ; } _pinfo; int main() { int t; cin >> t; int n; int k; int x; while (t--) { cin >> n; vector<_pinfo> p(n); for (int i = 0; i < n; i++) { cin >> p[i].k; for (int j = 0; j < p[i].k; j++) {...
2
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 200003; int n; int maxspd = INF; int noa = 0; int stk[N]; int main() { scanf("%d", &n); int ans = 0, spd = 0, top = 0; for (int i = 0; i < n; ++i) { int t; scanf("%d", &t); switch (t) { case 1: scanf(...
5
#include <bits/stdc++.h> using namespace std; long long n, sx, sy, fx, fy, vis[60][60]; char a[60][60]; long long nx[] = {0, 0, -1, 1}; long long ny[] = {-1, 1, 0, 0}; vector<pair<long long, long long> > land1, land2; bool check(long long x, long long y) { if (x > n || x < 1 || y > n || y < 1) return 0; return 1; }...
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 10, MOD = 1e9 + 7, nax = 1000005; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { long long n, k, d1, d2; cin >> n >> k >> d1 >> d2; if (n % 3 != 0) { cout << "no\n"; continu...
4
#include <bits/stdc++.h> using namespace std; int main() { long long a, t, x, y, p, q, i, j, k, e1, e2; cin >> t; while (t--) { cin >> x >> y >> p >> q; if (x * q == p * y) { printf("0\n"); continue; } if (p == q || !p) { printf("-1\n"); continue; } a = ceil((y - x ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sum[n]; long long arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); sum[n - 1] = arr[n - 1]; for (int i = n - 2; i >= 0; i--) sum[i] = sum[i + 1] + arr[i]; long long ans = 0; for (int i = 0; i ...
2
#include <bits/stdc++.h> using namespace std; int n, m, k, w, ans = 0; vector<string> levels; int cost(string a, string b) { int diff = 0; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) diff++; } return diff * w; } int main() { ios::sync_with_stdio(false); cin >> n >> m >> k >> w; for (int i =...
5
#include <bits/stdc++.h> int main() { int n = 0; std::cin >> n; int k = n / 2; std::cout << k << '\n'; if (n % 2 == 0) { for (int i = 0; i < n / 2; i++) std::cout << 2 << ' '; } else { for (int i = 0; i < n / 2 - 1; i++) std::cout << 2 << ' '; std::cout << 3; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long t; cin >> t; vector<long long> a; for (long long i = 1; i <= sqrt(1e10); i++) { a.push_back(i * i); } while (t--) { long long x; cin >> x; auto pos = u...
4
#include <bits/stdc++.h> using namespace std; int n, k, p[111], q[111], s[111], f[111], g[111]; int run(int h[]) { for (int i = 1; i <= n; i++) p[i] = i; for (int i = 0; i <= k; i++) { bool same = 1; for (int j = 1; j <= n; j++) if (p[j] != s[j]) same = 0; if (same) return i; for (int j = 1; j...
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class T, class U> inline void Max(T &a, U b) { if (a < b) a = b; } template <class T, class U> inline void Min(T &a, U b) { if (a > b) a = b; } inline void add(int &a, int b) { a += b; while (a >= 99824...
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], jumlah = 0, jumlahnol = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> satu; vector<int> nol; for (int i = 0; i < n; i++) { if (a[i] == 1) { jumlah++; satu.push_back(i); if (a[i +...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 110; int have[2][maxn]; int n; bool work(int k) { for (int i = 1; i <= n; i++) if (!have[k][i]) return 0; for (int i = 1; i <= n; i++) if (k == 0) printf("%d %d\n", i, have[k][i]); else printf("%d %d\n", have[k][i], i); return ...
3
#include <bits/stdc++.h> using namespace std; int p = 100003; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; cout << p << " " << p << endl; cout << 1 << " " << 2 << " " << p - (n - 2) << endl; for (int i = 1; i < n - 1; i++) { cout << i + 1 << " " << i + 2 << " " << 1 << endl; ...
4
#include <bits/stdc++.h> const long long inf = 20000000000000000; using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; if (n & 1) cout << (n / 2) * (n / 2 + 1) << endl; else cout << (n / 2) * (n / 2) << endl; for (int i = 1; i <= n / 2; i++) { ...
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; unordered_map<string, int> hmap0, hmap1; string str; long long int count0 = 0, count1 = 0; long long int count00 = 0, count11 = 0; for (int i = 0; i < n; i++) { cin >> str; ...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 3010; struct GG { int x, cnt; bool operator<(const GG& obj) const { return cnt > obj.cnt; } } p[maxn]; int vis[maxn][maxn]; long long group[maxn][maxn]; long long sum[maxn][maxn]; long long rest[maxn]; int n, m; int main() { scanf("%d %d", &n, &m); ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n < m) { cout << -1; return 0; } int min = n / 2 + n % 2; while (min % m != 0) { min++; } cout << min; }
1
#include <bits/stdc++.h> using namespace std; struct trie { struct tree { long long al[26]; bool ex; } nxt[100005]; long long cnt, dep[100005]; void insert(char *s) { long long l = strlen(s), rt = 0; for (register long long i = 0; i < l; i++) { long long c = s[i] - 'a'; if (!nxt[rt]....
7
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18 + 1; const long long int MOD = 1000000007L; clock_t time_p = clock(); void timer() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } template <typename Arg1> ostream& operator<<(ostream& ou...
7
#include <bits/stdc++.h> using namespace std; template <class P, class Q> inline void smin(P &a, Q b) { if (b < a) a = b; } template <class P, class Q> inline void smax(P &a, Q b) { if (a < b) a = b; } const int maxn = 200 + 10; const int inf = 1 << 28; int n, m, k; int a[maxn]; int d[maxn][maxn]; int pos[7]; int d...
8
#include <bits/stdc++.h> using namespace std; const long long nax = 6e5 + 69; const long long nochoice = 3e5 + 5; const long long INF = 1e12; long long ans = 0; long long n, k, par[nax], cost[nax]; string s; vector<long long> g[nax]; void init() { for (long long i = 1; i <= k; ++i) { long long pos = i * 2, neg = ...
8
#include <bits/stdc++.h> using namespace std; int n, data[400050], done[400050], f[5][20][400050]; int Rand() { return (rand() << 15) | rand(); } int Read() { int result = 0; char flag = getchar(); for (; flag < '0' || '9' < flag; flag = getchar()) ; for (; '0' <= flag && flag <= '9'; flag = getchar()) { ...
11
#include <bits/stdc++.h> using namespace std; long long n, k, pc, pw[2000005], mu[2000005], p[2000005], b[4000005]; bool vis[2000005]; long long pow(long long x, long long k) { long long ret = 1; while (k) { if (k & 1) ret = ret * x % 1000000007; x = x * x % 1000000007; k >>= 1; } return ret; } int ...
7
#include <bits/stdc++.h> using namespace std; const int N = 1.6e5 + 5; int n, m, cnt, num, ans, sum, down[205][205], righ[205][205], first[N], nex[N << 1], to[N << 1], match[N]; bool vis[N], color[N]; char c[205][205]; inline void dadd(int u, int v) { to[++num] = v; nex[num] = first[u]; first[u] = num; to[+...
10
#include <bits/stdc++.h> using namespace std; const int N = 65; const int inf = 1e9; int a[N][N][N]; int b[N][N]; int dp[N][N][N]; int main() { int n, m, r; scanf("%d%d%d", &n, &m, &r); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { scanf("%d", &a[i]...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 7; int n, k; int a[maxn], w[maxn], sz[maxn], up[maxn], dp[maxn]; vector<int> E[maxn]; int flag; void dfs1(int x, int fa, int m) { sz[x] = 1; for (int i = 0; i < E[x].size(); i++) { int v = E[x][i]; if (v == fa) continue; dfs1(v, x, m);...
9
#include <bits/stdc++.h> using namespace std; const int inf = 0x7FFFFFFF; struct point_int { int x, y; point_int() {} point_int(int a, int b) { x = a, y = b; } }; struct point_double { double x, y; point_double() {} point_double(double a, double b) { x = a, y = b; } }; struct Node { int v, w; Node() {} ...
2
#include <bits/stdc++.h> using namespace std; int toNum(char x) { if (x >= '0' && x <= '9') return x - '0'; return x - 'A' + 10; } char NumToChar(int x) { if (x < 10) return x + '0'; return x - 10 + 'A'; } struct lnum { vector<int> a; int _base; void norm() { while (!a.empty() && a.back() == 0) a.pop_...
6
#include <bits/stdc++.h> const int MAX_N = 1000000; const int MOD = 1000000007; std::vector<int> groups; int table[1 + MAX_N], dp[1 + MAX_N]; std::deque<int> tablePositions; int main() { std::string s; std::cin >> s; groups.push_back(0); int cnt = 0; for (int i = 0; i < s.size(); ++i) if (s[i] == '0') ...
10
#include <bits/stdc++.h> using namespace std; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline int wh(char ch) { if (ch == 'A') return 0; if (ch == 'T') return 1; if (ch == 'G') return 2; if (ch == 'C') return 3; return 4; } int tree[4][11][11][100007], n; char s[100007], t[100007]; vo...
6
#include <bits/stdc++.h> using namespace std; constexpr int N = 1e5; int n, m, q; int us[N], vs[N], ww[N]; vector<int> g[N]; long long dist[N]; int dist2[N]; vector<int> que[N * 2]; void dijkstra() { for (int i = 0; i < n; i++) { dist[i] = 1e16; } priority_queue<pair<long long, int>> q; q.emplace(0, 0); d...
13
#include <bits/stdc++.h> using namespace std; int n, ans = 0, ans1 = 0; string s; int main() { cin >> n; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '<') ans++; else break; } for (int i = n - 1; i >= 0; i--) { if (s[i] == '>') ans1++; else break; } cout << a...
1
#include <bits/stdc++.h> using namespace std; vector<int> adj[50001]; deque<int> q; int p[50001] = {0}; int color[50001] = {0}; void dfs(int u) { color[u] = 1; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (color[v] == 0) p[v] = u, dfs(v); } q.push_front(u); } int main() { int n, r1,...
4
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int c, d; cin >> c >> d; a = abs(a - c); b = abs(b - d); cout << max(a, b); }
0
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; Point() {} Point(double _x, double _y) : x(_x), y(_y) {} void input() { scanf("%lf%lf", &x, &y); } } p0, p1, p2; double t1, t2; double sqr(double a) { return a * a; } double dist(Point p1, Point p2) { return sqrt(sqr(p1.x - p2.x) + sqr(...
9