solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int main() { long long int a = 0, b = 0, i, x, y, sum = INT_MIN, temp, check = 0, less = INT_MAX; vector<long long int> av, bv; map<long long int, bool> mp; cin >> x; for (i = 0; i < x; i++) { cin >> temp; av.push_bac...
8
#include <bits/stdc++.h> using namespace std; vector<long long int> adj[10000007]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; long long int cnt = 0, a = 0, b = 0; map<char, long long int> cou; if (s.size() % 2 != 0) { cout << "-1" << endl; return 0; } else ...
3
#include <bits/stdc++.h> #pragma GCC optimize("-Ofast", "-funroll-all-loops") using namespace std; const long long N = 1e5 + 10; long long n, a[N], res, mx, s; void solve() { mx = -1e16, res = s = 0; cin >> n; for (long long i = 1; i <= n; i++) { scanf("%lld", &a[i]); res = max(res, mx - a[i]); mx = m...
7
#include <bits/stdc++.h> using namespace std; const long long int mod = (long long int)(998244353); const long long int mod2 = (long long int)1e9 + 7; long long int test = 1; vector<long long int> read(int n) { vector<long long int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; return v; } struct custom_hash { ...
7
#include <bits/stdc++.h> using namespace std; vector<string> vs; int visi[501][501]; int vis[501][501]; int typ[501][501]; int siz[501][501]; int cc, si; int ff[501 * 501]; int n, k; void print(int a[501][501]) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { cout << a[i][j] << " "; } ...
16
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; long long t[N]; char p[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; long long c, d; cin >> n >> c >> d; for (int i = 1; i <= n; ++i) { cin >> t[i] >> p[i]; a[i] = (p[i] == 'W'); } cin >> t...
23
#include <bits/stdc++.h> using namespace std; double ans, lve; struct node { int l; double p; bool operator<(const node &o) const { return l * p / (1 - p) > o.l * o.p / (1 - o.p); } } a[100000]; int main() { int n; scanf("%d", &n); ans = 0; lve = 0; for (int i = 0; i < n; i++) { scanf("%d%lf",...
13
#include <bits/stdc++.h> using namespace std; inline void debugMode() {} long long dp1[1001][1001], dp2[1001][1001]; int state[1001][1001]; void solvecases(int cse) { long long n, m, q; cin >> n >> m >> q; long long cnt = 1ll * n * m; long long ans = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= ...
13
#include <bits/stdc++.h> using namespace std; long long dp[2][4][4], ara[200003], mod = 1000000007; int main() { long long n, m, i, j, k, a, b, c, cnt = 0, d, ans = 0; string str, ttr; ttr = "abc"; scanf("%lld", &n); cin >> str; ara[0] = 1; for (i = 1; i <= n; i++) { if (str[i - 1] == '?') cnt++; ...
12
#include <bits/stdc++.h> using namespace std; template <typename IT> ostream &print(IT b, IT e, string sep = " ", ostream &stream = cout) { for (IT it = b; it != e; ++it) { stream << *it; if (it + 1 != e) stream << sep; } return stream; } template <typename IT> istream &read(IT b, IT e, istream &stream = ...
20
#include <bits/stdc++.h> using namespace std; string checker(string &s, string &t, long long idx1, long long idx2, long long c) { if (idx2 == t.size()) { return "yes"; } string ans = "no"; if ((idx1 + 1) < s.size() && s[idx1 + 1] == t[idx2] && (c == -1 || c == 0)) { ans = checker(s, t, id...
5
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; inline long long add(long long a, long long b) { a += b; if (a >= mod) a -= mod; return a; } inline long long sub(long long a, long long b) { a -= b; if (a < 0) a += mod; return a; } inline long long mul(long long a, long long b) { ret...
13
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-12; const int inf = 2000000000; const long long int infLL = (long long int)2e18; int MOD = 1000000007; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL ...
15
#include <bits/stdc++.h> using namespace std; int n, ans, p; template <class T> T fmin(T a, T b) { return a < b ? a : b; } int main() { int i, j, k; scanf("%d", &n); int mi = INT_MAX, x; p = 0; for (i = 1; i <= 2 * n - 1; i++) { scanf("%d", &x); if (x < 0) x = -x, p++; ans += x; mi = fmin(mi...
10
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; void sc(int& x) { scanf("%d", &x); } void sc(long long& x) { scanf("%lld", &x); } template <class T, class... Ts> void sc(T& t, Ts&... ts) { sc(t), sc(ts...); } const int MAX = 210; int dp[MAX][MAX][...
14
#include <bits/stdc++.h> using namespace std; int main() { int a, b, r, t; cin >> t; for (int i = 0; i < t; i++) { cin >> a >> b; r = (a / b) * b; if (a % b > b / 2) { r += b / 2; } else { r += a % b; } cout << r << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m, a, b, c; scanf("%d", &n); if (n == 3) { printf("1 1 1"); } else { a = 1; n -= 1; for (int i = 1; i < n; ++i) { b = i; c = n - i; if (b % 3 != 0 && c % 3 != 0) { printf("%d %d %d", a, b, c); ret...
0
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, a[N]; vector<int> edge[N]; vector<long long> subList[N]; long long M[N], res = LLONG_MIN; void dfs1(int u, int par) { M[u] = a[u]; for (int i = 0; i < edge[u].size(); i++) { if (edge[u][i] != par) { dfs1(edge[u][i], u); M[u] ...
10
#include <bits/stdc++.h> using namespace std; int main() { int p, x, y; cin >> p >> x >> y; for (int s = y;; s++) { if (s % 50 != x % 50) { continue; } bool me = false; int i = s / 50 % 475; for (int j = 0; j < 25; j++) { i = (i * 96 + 42) % 475; if (i + 26 == p) { me...
5
#include <bits/stdc++.h> using namespace std; long long a2[55], b[int(2e5 + 5)], cnt; map<long long, long long> m; int main() { long long n, i, j; a2[0] = 1; for (i = 1; i <= 50; i++) { a2[i] = a2[i - 1] * 2; } cin >> n; for (i = 0; i < n; i++) { scanf("%lld", &j); m[j]++; } map<long long, l...
10
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { long long int temp; while (b) { temp = a; a = b; b = temp % b; } return a; } int main() { ios_base::sync_with_stdio(0); long long int t, w, b, m, l, ans; cin >> t >> w >> b; m = min(w, b); l...
10
#include <bits/stdc++.h> using namespace std; const int INF = (1LL << 31) - 1; int A[2002]; int main() { int res = 0; int n, a, b; cin >> n >> a >> b; for (int i = 0; i < n; ++i) cin >> A[i]; sort(A, A + n); cout << A[b] - A[b - 1] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int q, n; int a[5005]; int shu[5005][5005]; int out[5005][5005]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; shu[1][i] = a[i]; out[1][i] = a[i]; } for (int i = 2; i <= n; ++i) for (int j = 1; j <= n - i + 1; ++j) { shu[i]...
10
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long w, l; cin >> w >> l; vector<long long> a(w); long long res = 4154118101919364364; for (int i = 1; i <= w - 1; i++) { cin >> a[i]; a[i] += a[i - 1]; if (i >= l) { res = min(...
11
/* author: Kamal Shakhdullaev */ #define _CRT_SECURE_NO_WARNINGS #pragma comment(linker, "/stack:512000000") #include <iostream> #include <stdio.h> #include <fstream> #include <vector> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <cstring> #include <string> #include <cmath>...
6
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-9; const int inf = (1 << 30) - 1; const long long inf64 = ((long long)1 << 62) - 1; const long double pi = 3.1415926535897932384626433832795; template <class T> T sqr(T x) { return x * x; } template <class T> T abs(T x) { return x < 0 ? -x : x...
19
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; long long int a = 1; vector<long long int> v; v.push_back(1); for (int i = 0; i < 38; i++) { a *= 3; v.push_back(a); } for (int i = 0; i < n; i++) { long long int b;...
7
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int main() { int n; cin >> n; int x[12]; for (int i = 0; i < 12; i++) { cin >> x[i]; } if (n == 0) { cout << 0; return 0; } else { sort(x, x + 12); int sum = 0, m = 0; for (int i = 11; i >= 0; i--) { ...
1
#include <bits/stdc++.h> using namespace std; long long n, f[255][255], k, c[255][255], fac[255 * 255], fac2[255 * 255]; int main() { scanf("%lld%lld", &n, &k); for (long long i = 0; i <= n; i++) c[i][0] = c[i][i] = 1; for (long long i = 1; i <= n; i++) { for (long long j = 1; j < i; j++) { c[i][j] = (c...
15
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct point { int x, y; bool operator<(const point &b) const { return x < b.x || x == b.x && y < b.y; } } a[N]; vector<pair<int, int>> pre, post; int main() { int n, m, i = 1, j = 1, tmpl, tmpr, lastx = 0; scanf("%d%d", &n, &m); for (i...
17
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFL = 0x3f3f3f3f3f3f3f3fLL; const double eps = 1e-8; const double PI = acos(-1.0); const int maxn = 1e5 + 10; int n, k; int arr[maxn]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &arr[i]); p...
11
#include <bits/stdc++.h> using namespace std; double const EPS = 1e-12, PI = acos(-1); const int N = 3e5 + 9, M = 5e3 + 9, OO = 1e9 + 7, MOD = 1e9 + 7; const long long inf = 1e18; int par[(1 << 22)], sz[(1 << 22)], arr[(1 << 22)], comp, n, m; bool mwgood[(1 << 22)], vis[(1 << 22)]; int find(int x) { if (par[x] == x) ...
17
#include <bits/stdc++.h> using namespace std; template <typename T> void chkmax(T &x, T y) { x = x > y ? x : y; } template <typename T> void chkmin(T &x, T y) { x = x > y ? y : x; } const int INF = 2139062143; template <typename T> void read(T &x) { x = 0; bool f = 1; char ch; do { ch = getchar(); i...
12
#include <bits/stdc++.h> using namespace std; const long long M = 998244353; signed main() { long long n; cin >> n; long long p = 0, s = 0, a = 0; while (n--) { scanf("%d", &p); a = (a + p) % M; s = (s * 2 + a) % M; a = a * 2 - p; } cout << s << endl; return 0; }
12
#include <bits/stdc++.h> using namespace std; int ans[110000], add[110000]; int n, m, a, cnt, me; char temp[3]; int main() { while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) ans[i] = 1, add[i] = 0; ; cnt = 0; me = 0; for (int i = 1; i <= m; i++) { scanf("%s%d", temp, &a); ...
10
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, a, b, c; cin >> n >> a >> b >> c; long long ans = (long long)9e18; for (int i = 0; i <= 4; i++) { for (int j = 0; j <= 4; j++) { for (int ii = 0; ii <= 4; ii++) { if ((n + i + 2...
5
#include <bits/stdc++.h> using namespace std; const int N = 105, M = (1 << 20) + 2, inf = 1e9 + 7; int n, ar[N], mp[N], dp[M], u[M]; vector<pair<int, int> > req; bool B(int mask, int j) { return mask & (1 << j); } int Fun(int i, int mask) { if (i >= req.size()) return 0; if (u[mask]) return dp[mask]; u[mask] = 1;...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int n; cin >> n; int even[200005] = {}, odd[200005] = {}; for (int i = 1; i <= n; i++) { int x; cin >> x; even[i] = even[i - 1]; odd[i] = odd[i - 1]; if (i % 2) ...
4
#include <bits/stdc++.h> using namespace std; int main() { string s; int num; while (cin >> s >> num) { transform(s.begin(), s.end(), s.begin(), ::tolower); for (int i = 0; i < s.size(); ++i) if (s[i] < 97 + num) s[i] = toupper(s[i]); else s[i] = tolower(s[i]); cout << s <<...
6
#include <bits/stdc++.h> using namespace std; const int MX = 35; struct Point { int x, y; Point() {} Point(int _x, int _y) : x(_x), y(_y) {} bool operator==(const Point &b) const { return x == b.x && y == b.y; } bool operator!=(const Point &b) const { return !(*this == b); } }; int N, M, ip[MX][MX], op[MX][MX...
20
#include <bits/stdc++.h> using namespace std; int main() { int tests = 1; while (tests--) { int n, m; cin >> n >> m; if (m > n) { cout << -1 << endl; } else { int i = m; while (true) { if (2 * i >= n) { break; } i += m; } cout << i << e...
2
#include <bits/stdc++.h> using namespace std; const int mxn = 200000; int n, k; int a[mxn], d[mxn], p[mxn], first[mxn]; vector<int> g[mxn]; int dfs(int c, int v) { int ret = c; for (int i : g[c]) if (i != p[c]) { p[i] = c, d[i] = d[c] + 1, a[i] = (a[c] + v) % k; int x = dfs(i, v); if (d[x] > d...
20
#include <bits/stdc++.h> using namespace std; int main() { int n, t, c, k, m, i, j; while (cin >> n) { if (n == 1 || n == 2) { cout << n << endl; continue; } for (i = n - 1; i >= 1; i--) { if (n % i) { cout << n - i << endl; break; } } } return 0; }
0
#include <bits/stdc++.h> int a1, a2, a3, a4; int main() { scanf("%d%d%d%d", &a1, &a2, &a3, &a4); a4 -= a3 - 1; a2 -= a3; a1 -= a3; int mark = 0; if (a4 == 2) { a1--; a2--; mark = 3; } else if (a4 == 1) { if (a1) { a1--; mark = 1; } else { a2--; mark = 2; } ...
10
#include <bits/stdc++.h> using namespace std; int main() { string a, s; getline(cin, s); getline(cin, a); int b = 0, k = 0, x = 0, i, y, j; for (i = 0; i < 200; i++) { if (a[i] == '\0') break; else b++; } int c[200] = {0}; for (j = 0, k = 0; j < b; j++) { char u = a[j]; if (u...
0
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b) { a += b; if (a >= 1000000007) { a -= 1000000007; } return a; } long long sub(long long a, long long b) { a -= b; if (a < 0) { a += 1000000007; } return a; } long long mul(long long a, long long b) { return (a ...
11
#include <bits/stdc++.h> using namespace std; template <typename T> string tostr(const T& t) { ostringstream os; os << t; return os.str(); } long long big = 10000000; int N; vector<string> F; string W; char let; bool equal(string& a, string& b) { if ((int)a.size() != (int)b.size()) return false; for (int i = ...
8
#include <bits/stdc++.h> using namespace std; vector<long long int> primes; bool visited[1000010]; void seive() { long long int i, j; for (i = 2; i < 1000010; i++) { if (visited[i]) continue; primes.push_back(i); for (j = i; j < 1000010; j += i) { visited[j] = 1; } } } long long int count_fa...
5
#include <bits/stdc++.h> using namespace std; const int mm = 1e6 + 5; const float f = acos(-1); map<long long, int> mp; long long bit[mm], b[mm], r[mm], h[mm], vol[mm], dp[mm]; long long maxi; int p, n, k; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d%I64d", &r[i], &h[i]); vol[i] ...
12
#include <bits/stdc++.h> long power(long int x, long int y) { int temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return temp * temp; else return x * temp * temp; } using namespace std; int main() { int a, b, dif, ans, n, m; cin >> a >> b; dif = abs(a - b); if (dif <= 2) ...
0
#include <bits/stdc++.h> using namespace std; class Disjoint_Set_Tree { private: vector<int> root, rank, sum, depth; vector<vector<int> > LCA, adj; int x, y, n, p, lg; long long count; vector<long long> f; public: Disjoint_Set_Tree(int n, int num) { root = vector<int>(n, 0); depth = vector<int>(n...
25
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long t = 1; while (t--) { long long n; cin >> n; long long arr[n]; long long a[7] = {INT_MAX, 4, 8, 15, 16, 23, 42}; long long ans[7] = {INT_MAX, 0, 0, 0, 0, 0, 0}; for (long long i = 1; ...
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int N = 1010; int n; vector<set<int> > lista[N]; set<int> go(string str) { int i; for (i = 0; str[i] != ':'; i++) ; i++; set<int> ret; int val = 0; for (; i < (int)str.size(); i++) { if (str[i] == ',') { ret.insert...
16
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; char s[maxn]; long long ans; int main() { scanf("%s", s); int len = strlen(s); int r = len; for (int l = len - 1; l >= 0; l--) { r = min(r, l + 9); for (int k = 1; l + 2 * k < r; k++) { if (s[l] == s[l + k] && s[l] == s[l + 2...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int x, y; cin >> x >> y; if (x == y) { cout << 0; } else { if (x < y) { if ((y - x) % 2 == 0) { cout << 2...
0
#include <bits/stdc++.h> const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; using namespace std; vector<int> G[500050]; void dfs(int v, int p, int d, vector<int>& dist) { bool leaf = true; for (int ch : G[v]) { if (ch == p) continue; leaf = false; dfs(ch, v, d + 1, dist); } if (leaf) di...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; char k; cin >> n >> m; int r[3] = {}, c[3] = {}, g = 0, h = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> k; if (k == '*') { r[g] = i + ...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(0); int a, b; cin >> a >> b; vector<pair<pair<int, int>, int>> v; v.push_back({{0, 0}, 0}); for (int i = 0; i < a; i++) { int x, y; cin >> x >> y; v.push_back({{x, y}, x * 60 + y...
3
#include <bits/stdc++.h> using namespace std; long long n; long long a[200005]; long long rec(long long l, long long r) { if (r - l <= 1) { return 0; } long long mid = (l + r) / 2; long long ans = 0; ans += rec(l, mid); ans += rec(mid + 1, r); set<long long> L, R; long long maxval = 0, ptr = mid; ...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int n = s.size(); int le = 0, ri = n - 1; while (le < ri) { if (s[le] != s[ri]) { cout << 0; return 0; } int i = le, j = ri; while (s[i] == s[le]) ++i; ...
1
#include <bits/stdc++.h> using namespace std; int ovr[105], init[105], incr[105], n; int change(int pos) { int now = init[pos], pore = init[pos] + incr[pos], i; int nowcnt = 0, porecnt = 0; for (i = 0; i < n; i++) { if (i == pos) continue; int val = init[i]; if (ovr[i]) val += incr[i]; if (val > n...
14
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 100; const long long inf = 1e18 + 7; const long long mod = 1e9 + 7; int nxt[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; for (int tc = 0; tc < t; tc++) { int n; cin >> n; for (int ...
13
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int nr, ng, nb; cin >> nr >> ng >> nb; vector<int> R, G, B; for (int i = 0; i < nr; i++) { int r; cin >> r; R.push_back(r)...
9
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int m = (n + 2) / 2; cout << m << endl; int upper = min(n, m); for (int i = 0; i < upper; i++) { cout << i + 1 << " " << 1 << endl; n--; } if (n > 0) { for (int i = 0; i <...
3
#include <bits/stdc++.h> using namespace std; inline long long getnum() { char c = getchar(); long long num, sign = 1; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') sign = -1; for (num = 0; c >= '0' && c <= '9';) { c -= '0'; num = num * 10 + c; c = getchar(); } return num * sign; }...
8
#include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define F first #define S second typedef long long ll; typedef pair<int, int> pii; #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() #define itn int ll powmod(ll a,ll b) {ll res=1; assert(b>=0); for(;b;b>>=1){if(b&1)r...
16
#include <bits/stdc++.h> using namespace std; int N, K; int c0, c1, A; int main() { scanf("%d%d", &N, &K); for (int(i) = 0; (i) < (N); (i)++) scanf("%d", &A), c0 += (A % 2 == 0), c1 += (A % 2 == 1); if (N == K) { if (c1 % 2) puts("Stannis"); else puts("Daenerys"); } else if ((N - K) % 2 ...
14
#include <bits/stdc++.h> struct node { int x, y; }; node h[1000050]; int kx[1000050]; int ans[1000050]; int main() { int n, m, dx, dy; scanf("%d %d %d %d", &n, &m, &dx, &dy); h[1].x = 0; h[1].y = 0; kx[0] = 1; for (int i = 2; i <= n; i++) { h[i].x = (h[i - 1].x + dx) % n; h[i].y = (h[i - 1].y + dy...
12
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167772160000") using namespace std; bool is(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') return 1; return 0; } int p[110000]; int main() { int n; cin >> n; for (int i = 0; i < (n); i++) cin >> p[i]; string s;...
4
#include <bits/stdc++.h> using namespace std; long long personal[100005], bike[100005], shared; long long cost(long long pos) { long long res = 0, i; for (i = 0; i <= pos; i++) { if (personal[pos - i] < bike[i]) res += bike[i] - personal[pos - i]; } return res; } long long result(long long pos) { long lon...
10
#include <bits/stdc++.h> using namespace std; int main() { int n, sum[2][100], a[2][100], b[100]; scanf("%d", &n); for (int k = 0; k < 2; k++) { for (int i = 0; i < n - 1; i++) { scanf("%d", &a[k][i]); } } for (int i = 0; i < n; i++) { scanf("%d", &b[i]); } sum[0][0] = 0; for (int i = ...
5
#include <bits/stdc++.h> using namespace std; struct team { string name; long int win = 0; long int deltagoals = 0; long int goals = 0; }; struct pred { bool operator()(const team& h1, const team& h2) { if (h1.win != h2.win) { return h1.win > h2.win; } else if (h1.deltagoals != h2.deltagoals) { ...
6
#include <bits/stdc++.h> using namespace std; inline int read() { int s = 0, ch = getchar(), w = 1; while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar(); return s * w; } inline long long Read() { long long s = 0;...
24
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; if (n <= m) cout << n << '\n'; else { long long b = 1, e = 2000000000, mid, d, diff = n - m, ans = -1; while (b <= e) { mid = (b + e) / 2; d = (mid * (mid + 1)) / 2; if (d >= diff) { a...
8
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1; char ch = ' '; while (!isdigit(ch)) { ch = getchar(); if (ch == '-') f = -1; } while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return x * f; } int n, m, needh[105], needl[105], a[105][10...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); using Edge = tuple<int, int, int>; using Graph = vector<vector<Edge>>; int N, M; cin >> N >> M; Graph graph(N); for (int i = 0; i < M; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; graph[u]...
12
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; template <class T> inline T biton(T n, T pos) { return n | ((T)1 << pos); } template <class T> inline T bitoff(T n, T pos) { return n & ~((T)1 << pos); } template <class T> inline T ison(T n...
3
#include<bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; void solve() { int n, m; cin >> n >> m; set<int> se; while (n) { int dig = n % m; if (se.count(dig)) { cout << "NO" << '\n'; return; } se.insert(dig); ...
10
#include <bits/stdc++.h> using namespace std; long long takbir(long long a) { long long c = 9, count = 0; while (a >= c) { count++; c = 10 * c + 9; } return count; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long t, a, b; cin >> t; while (t--) { cin >> a >> b; long long co...
3
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } struct Poin...
16
#include <bits/stdc++.h> using namespace std; const int maxn = 500005; struct edge { int v, next; }; edge w[maxn], x[maxn]; int deg[maxn], cnt[maxn], a[maxn], b[maxn], xx[maxn], ww[maxn], ans[maxn], color[maxn], que[maxn], visit[maxn]; int N, M, W = 1, X = 1; bool sort_cnt(const int x, const int y) { return cnt[x...
17
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> a(n); int x = 0; for (int i = 0; i < n; i++) { cin >> a[i]; a[i] += x; x = max(x, a[i]); cout << a[i] << ' '; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long in...
1
#include <bits/stdc++.h> inline int read() { int res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); return bo ? ~res + 1 : res...
18
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int inf = 0x3f3f3f3f; int n, l, r; char s1[102], s2[102], s3[102]; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; if (p1 == p2) { p2 = (p1 = buf) + fread(buf, 1, 100000, stdin); if (p1 == p2) return EOF; } ret...
11
#include <bits/stdc++.h> using namespace std; int isGoodNum(int p) { while (p) { if (p % 10 != 7 && p % 10 != 4) return 0; p /= 10; } return 1; } int main(void) { int n; scanf("%d", &n); vector<pair<int, int> > ar(n); for (int i = 0; i < n; i++) { scanf("%d", &ar[i].first); ar[i].second = ...
12
#include <bits/stdc++.h> using namespace std; int f[100100], g[100100]; void find(int x) { if (f[x] == x) return; find(f[x]); g[x] ^= g[f[x]]; f[x] = f[f[x]]; } int check(int x, int y, char c) { int v = (c == 'o'); find(x); find(y); if (f[x] == f[y]) { return (g[x] ^ g[y]) == v; } else { g[f[x...
20
#include <bits/stdc++.h> using namespace std; using ll = long long; vector<int> v[400010]; bool vis[400010]; void add(int v1, int v2) { v[v1].push_back(v2); v[v2].push_back(v1); } void dfs(int i, vector<int> &temp) { if (vis[i] || v[i].size() == 0) return; vis[i] = 1; if (i % 2 == 0) temp.push_back(i / 2); ...
15
#include <bits/stdc++.h> using namespace std; int a[100010], c[100010], n; void add(int x, int k) { while (x <= n) { c[x] += k; x += x & -x; } } int read(int k) { int sum = 0; while (k) { sum += c[k]; k -= k & -k; } return sum; } struct node { int x, loc; } b[100010]; int cmp(node a, node ...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 7e3 + 20; int dp[2][maxn], n, T[2][maxn], k[2]; vector<vector<int> > V(2); int main(void) { int k1, k2; V[0].reserve(maxn); V[1].reserve(maxn); cin >> n; cin >> k1; for (int i = 0; i < k1; i++) cin >> V[0][i]; cin >> k2; for (int i = 0; i < ...
12
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; inline int add(int a, int b) { if ((a += b) >= mod) a -= mod; return a; } inline int mult(int a, int b) { long long t = 1ll * a * b; if (t >= mod) t %= mod; return t; } inline int dec(int a, int b) { if ((a -= b) < 0) a += mod; retu...
15
#include <bits/stdc++.h> using namespace std; long long int mod = 1000000007; bool sortBysec(pair<long long int, long long int> a, pair<long long int, long long int> b) { return a.second < b.second; } long long int binExp(long long int a, long long int n) { long long int res = 1; while (n) { if...
10
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops") using namespace std; const long double EPS = 10e-9; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } ...
7
#include <bits/stdc++.h> using namespace std; int main() { int t; string str; cin >> t; bool same[13]; while (t--) { cin >> str; int ans[12][2]; int cont = 0; for (int i = 1; i <= 12; i++) { if (12 % i) continue; set<char> s[12]; int x = 12 / i; for (int j = 0; j < (int...
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int t; cin >> t; while (t--) { long long n, m; cin >> n >> m; vector<long long> arr(n); for (int j = 0; j < n; j++) { str...
13
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int MAXN = 1e5 + 10; const long long MOD = 1e9 + 7; const long long MOD2 = 998244353; const long long INF = 8e18; const int LOG = 20; long long pw(long long a, long long b, long long mod) { return (!b ? 1 : (b & 1 ? (a * pw(a ...
19
#include <bits/stdc++.h> int main() { int a[101][101], k0[101] = {0}, n0[101] = {0}, i, j, k, n, m, p, o, q; scanf("%d %d %d", &n, &m, &k); for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) scanf("%d", &a[i][j]); for (j = 1; j <= m; j++) { for (i = 1; i <= n; i++) if (!n0[i] && a[i][j]) k0[a[i][j]...
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; unsigned long long ans = 0; for (int i = 2; i < n; i++) ans += (i) * (i + 1); cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; signed main() { long long k; cin >> k; if (k <= 9) cout << k; if (k > 9 && k < 190) { long long t = k - 8; long long c = t / 2 + 9; if (t % 2 == 0) cout << c / 10; else cout << c % 10; } else if (k > 189 && k < 2890) { long long t =...
2
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; int x = n / 2; if (x == 0 && k == 0) cout << "1" << "\n"; else if (x > k || x == 0) cout << "-1" << "\n"; else...
7
#include <bits/stdc++.h> using namespace std; int func(int i, int n) { if (i == -1) { return n - 1; } if (i == n) { return 0; } return i; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; int k; cin >> k; string s; cin >> s; int ...
15