solution
stringlengths
53
181k
difficulty
int64
0
27
#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 mul(long long a, long long b) { return ((a % 1000000007) * (b % 1000000007)) % 1000000007; } long long binary_expo(long long base, long long expo) { long ...
10
#include <bits/stdc++.h> using namespace std; struct pos { int x, y, z; inline bool operator<(const pos t) const { if (x != t.x) return x < t.x; else return y < t.y; } } g[120000]; int i, n; int main() { scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d%d", &g[i].x, &g[i].y), g[i].z = i;...
8
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 1; int n, m, x, y, w; map<int, int> answer[MAX]; vector<int> adj[MAX]; bool hasColor(int u, int c) { int l = 0, r = adj[u].size() - 1, mid; while (l <= r) { mid = (l + r) / 2; if (adj[u][mid] == c) return true; if (adj[u][mid] > c) ...
16
#include <bits/stdc++.h> using namespace std; int main() { string s; int n; cin >> n >> s; sort(s.begin(), s.end()); cout << s << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-8; const double PI = acos(-1.0); const int MAXN = 1e5 + 5; const int MAXM = 2e6 + 5; const int MOD = 1e9 + 7; int sgn(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; else return 1; } long long pw...
13
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<vector<long long>> v(n); for (long long i = 0; i < n - 1; i++) { long long a, b; cin >> a >> b; v[a - 1].push_back(b - 1); v[b - 1].push_back(a - 1); } ...
8
#include <bits/stdc++.h> using namespace std; struct node { int d; node* next[2]; }; struct node* nn() { struct node* n = new struct node(); n->next[0] = NULL; n->next[1] = NULL; return n; } void create(struct node* ptr, int a, int i) { if (i < 0) { ptr->d = a; return; } int d = (a >> i) & 1; ...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, k; while (cin >> n >> k) { vector<long long> v; for (int i = 0; i < n; i++) { long long add; cin >> add; v.push_back(add); } sort(v.begin(), v.end()); int p1 = ...
12
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; for (int p = 1; p <= t; p++) { int n, z; cin >> n; vector<vector<int>> grid(n, vector<int>(n, 0)); for (int i = 0; i < n; i++) { for (int j = 0; ...
3
#include <bits/stdc++.h> char mas[100][101]; int main(int argc, char* argv[]) { int n, m, i; scanf("%d%d", &n, &m); for (;;) { bool st = false; for (i = 1; i <= n; i++) { if (i > m) { st = true; break; } m -= i; } if (st) break; } printf("%d\n", m); }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long n1, long long n2) { if (!n1) return n2; if (!n2) return n1; if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { base %= 1000000007; long long ans = 1; while (exponent) { if ...
14
#include <bits/stdc++.h> using namespace std; string str; int N; bool can(long long x) { vector<int> pack, com; for (int i = 0; i < N; i++) { if (str[i] == 'P') pack.push_back(i); if (str[i] == '*') com.push_back(i); } int i = 0; for (auto &act : pack) { int mini = N + 1, maxi = -1; while (i <...
10
#include <bits/stdc++.h> using namespace std; int main() { long long int test_case; cin >> test_case; while (test_case--) { long long int n, m, a, b; cin >> n >> m >> a >> b; if (n * a != m * b) cout << "NO" << endl; else { cout << "YES" << endl; long long int i, j, t; long...
11
#include <bits/stdc++.h> using namespace std; int const N = 20; int const M = 101000; int f[N + 1][1 << N], s[2][N + 1][1 << N]; char a[N][M]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < (n); ++i) scanf(" %s", a[i]); for (int j = 0; j < (m); ++j) { int t = 0; for (int i = 0; i < (n...
18
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int dx[] = {-1, 0, 0, 1, -1, 1, 1, -1}; const int dy[] = {0, -1, 1, 0, 1, 1, -1, -1}; long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); } long long lcm(long long x, long long y) { return ((x / gcd(x, y)) * y); } con...
11
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int MAXN = 1000; const int MAXM = 1000; long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } int nxt[140]; int fa[140]; int vis[140]; int now...
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") using namespace std; int a, b, c, d, i, j, n, m, k; inline bool prime(int a) { for (int i = 2; i * i <= a; ++i) if (a % i == 0) return 0; return 1; } int main() { scanf("%d", &n); for (int _n(1000000000), i(2); i <= _n; i++) { if (!prim...
8
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); int n, i, j; for (i = 0; i < t; i++) { scanf("%d", &n); for (j = 0; j < 2 * n; j++) { if (j % 2 != 0) { printf("%d ", 1); } } printf("\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; string to_string(string const& s) { return '"' + s + '"'; } string to_string(char c) { return string(1, c); } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <class T1, class T2> string to...
14
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops,no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; namespace io { const int L = (1 << 20) + 1; char buf[L], *S, *T, c; char getchar() { if (__builtin_expect(S == T, 0)) { T...
24
#include <bits/stdc++.h> int v1, v2, t, d; long long dp[10010][105]; bool visited[10010][105]; long long F(int v, int i) { if (i == t) { if (v == v2) return 0; else return (0LL - (1LL << 60)); } if (visited[v][i]) return dp[v][i]; int j, x; long long y, z, res = (0LL - (1LL << 60)); for ...
6
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), ...
1
#include <bits/stdc++.h> using namespace std; const long long N = 1005; long long n, mn; double p[N], f[N][N]; inline long long read() { long long ret = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -1; ch = getchar(); } while (isdigit(ch)) { ret = ret * 10 + (ch ^ 48)...
12
#include <bits/stdc++.h> using namespace std; int main() { int T; vector<int> sta; cin >> T; for (int i = 0; i < T; i++) { int temp; cin >> temp; sta.push_back(temp); } int s, t; cin >> s >> t; if (s > t) { int temp = s; s = t; t = temp; } int result1 = 0; int result2 = 0; ...
0
#include <bits/stdc++.h> using namespace std; bool checkbit(int mask, int bit) { return mask & (1 << bit); } int setbit(int mask, int bit) { return mask | (1 << bit); } int togglebit(int mask, int bit) { return mask ^ (1 << bit); } int n; int gg[30][30]; vector<int> v; int dp[(1 << 23) + 10]; void print(int mask) { c...
14
#include <bits/stdc++.h> const int MAXINT = 2147483640; const long long MAXLL = 9223372036854775800LL; const long long MAXN = 1000000; const double pi = 3.1415926535897932384626433832795; using namespace std; int x1[10000000]; int x2[10000000]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(time(0))...
1
#include <bits/stdc++.h> using namespace std; int a, b, c, d; int q, w, e, r; int main() { cin >> a >> b >> c >> d; q = max(3 * a / 10, a - (a / 250) * c); w = max(3 * b / 10, b - (b / 250) * d); if (q == w) { cout << "Tie"; } if (q > w) { cout << "Misha"; } if (q < w) { cout << "Vasya"; }...
1
#include <bits/stdc++.h> using namespace std; inline int read() { int 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; } inline long long readll...
21
#include <bits/stdc++.h> using namespace std; int n; int solve(long long a, long long b) { if (a < b) swap(a, b); if (a == 0 || b == 0) return 0; if (a % b == 0) return 1; if (solve(b, a % b) == 0) return 1; return ((a / b) % (b + 1) + 1) % 2; } int main() { scanf("%d", &n); long long a, b; int flag; ...
15
#include <bits/stdc++.h> using namespace std; struct debugger { template <typename T> debugger& operator,(const T& v) { cerr << v << " "; return *this; } } debug1; const long long inf = 0x7fffffff; const int ND = 112345; pair<long long, long long> co[ND]; int n; long long dp1_min[ND], dp1_max[ND]; long lo...
21
#include <bits/stdc++.h> using namespace std; int main() { int w, h; long long R; while (cin >> w >> h) { R = (w / 2) * (w / 2 + w % 2); R *= (h / 2) * (h / 2 + h % 2); cout << R << endl; } }
5
#include <bits/stdc++.h> using namespace std; int n, k; bool ck[500]; int use[500]; string a; int main() { int i, j, l; cin >> k; cin >> a; n = a.size(); for (i = 0; i < n; i++) ck[a[i]]++; for (i = 'a' + k; i <= 'z'; i++) if (ck[i]) { printf("IMPOSSIBLE"); return 0; } if (n % 2 == 0) ...
8
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const double eps = 1e-9; int N, M; int h[100005], t[100005]; long long p[100005]; void init() { p[0] = 1; for (int i = (1); i < (100005); ++i) { t[i] = i; p[i] = p[i - 1] * 2LL; p[i] %= 1000000009; } } inline int root(int n) { ...
15
#include <bits/stdc++.h> using namespace std; long long int ans[] = { 47, 74, 4477, 4747, 4774, 7447, 7474, 7744, 444777, 447477, 447747, 447774, 474477, 474747, 474774, 477447, 477474, 477744, 744477, 744747, 744774, 747447, 747474, 747744, ...
5
#include <bits/stdc++.h> using namespace std; int const N = 300000; vector<pair<int, int> > tr[N + 1]; int w[N + 1]; pair<long long, long long> go(int v = 1, int p = 0) { long long mx1 = 0, mx2 = 0, an = 0; for (int i = 0; i < (int)(tr[v].size()); ++i) { int u = tr[v][i].first; if (u == p) continue; pai...
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int n, m; bool space[25][25], vis[25][25]; int dist[600][600]; int make_label(int x, int y) { return x * m + y; } void bfs(int x, int y) { memset(vis, 0, sizeof(vis)); queue<pair<pair<int, int>, int...
20
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; if (n == 12) { if ((s[0] > '1') && (s[1] != '0')) s[0] = '0'; else if (s[0] > '1' && s[1] == '0') s[0] = '1'; else if (s[0] == '1' && s[1] > '2') s[0] = '0'; else if (s[0] == '0' &...
5
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); long long a, b; long long falta, num = 0, outro = 0; for (int i = 0; i < t; ++i) { scanf("%I64d %I64d", &a, &b); falta = (4 - (a % 4)) % 4; if (falta) { for (long long j = 0; j < min(falta, b); ++j) { ...
12
#include <bits/stdc++.h> double s; std::vector<long> G[100000]; void DFS(long x, long y = -1, long d = 0) { s += 1. / ++d; for (long e : G[x]) if (y ^ e) DFS(e, x, d); } int main(void) { long n, x, y; for (scanf("%li", &n); --n; G[y].push_back(x)) scanf("%li%li", &x, &y), G[--x].push_back(--y); DFS(0)...
14
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k; cin >> k; cout << 3 << " " << 4 << endl; int x1 = 262143, x2 = 131072; cout << x1 << " " << k << " " << k << " " << 0 << endl; cout << x2 << " " << 0 << " " << k << " " << 0 << ...
9
#include <bits/stdc++.h> using namespace std; int t; void solve() { int n, m; cin >> n >> m; string block_row(m, 'B'); vector<string> result(n, block_row); result[0][0] = 'W'; for (int i = 0; i < n; ++i) { cout << result[i] << endl; } } int main() { cin >> t; while (t--) { solve(); } retur...
2
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T a) { return a < 0 ? (-a) : a; } template <typename T> inline T sqr(T x) { return x * x; } bool IsWin(char c1, char c2) { return c1 == '(' && c2 == '8' || c1 == '8' && c2 == '[' || c1 == '[' && c2 == '('; } int main() { s...
9
#include <bits/stdc++.h> using namespace std; long long null = 0; struct segment; struct line; struct point; struct vec; struct ray; struct point { long double x, y; point() {} point(long double x1, long double y1) { x = x1; y = y1; } long double dist_to_point(point p) { return sqrt((p.x - x) * (p...
15
#include <bits/stdc++.h> using namespace std; const long long N = 505; long long g[N][N]; long long adj[N][N]; long long ans[N]; long long mn[N][N]; long long n, w; const long long mx = INT_MAX; long long order[N]; int32_t main() { cin >> n; ans[n] = 0; for (long long i = 1; i <= n; i++) { for (long long j = ...
9
#include <bits/stdc++.h> using namespace std; int main() { int n, q; cin >> n >> q; map<int, int> cnt; for (int i = 0; i < n; i++) { int x; cin >> x; ++cnt[x]; } while (q--) { int x; cin >> x; int ans = 0; for (int i = 30; i >= 0 && x > 0; --i) { int val = 1 << i; int...
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; int t, n, m; int pos[MAXN]; int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; int l = 2500000, r = 2499999; while (t--) { char op; cin >> op >> n; if (op == 'L') { l--; pos[n] = l; ...
6
#include <bits/stdc++.h> const int N = 3e5 + 10; using namespace std; int used[N], vis[N], l[N], r[N], Hash[N], n, m, size, tot; vector<int> e[N]; int read() { int x = 0, f = 1, c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c...
8
#include <bits/stdc++.h> using namespace std; long long n, m, k, i, j, a[1000001], maxnm, ans[1000000], b[1000000], ind, res; int main() { cin >> n >> k; a[0] = -1; for (i = 1; i <= n; i++) { cin >> a[i]; } for (i = 0; i <= n; i++) { if (a[i] != a[i + 1]) { m++; b[m] = a[i + 1]; } } ...
8
#include <bits/stdc++.h> using namespace std; const long long MAXN = 500 + 6.9; const long long INF = 1e18; long long dist[MAXN][MAXN]; long long tail[MAXN]; long long ans[MAXN]; vector<pair<long long, long long>> a[MAXN]; long long n, m; void floyd() { for (long long k = 1; k <= n; k++) { for (long long i = 1; i...
17
#include <bits/stdc++.h> using namespace std; const int inf = 1000000007; long long int modpower(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = ((x % p) * (x % p)) % p; } return res % p; } long ...
7
#include <bits/stdc++.h> using namespace std; long long int inf = pow(10, 14), inf2 = pow(10, 12); long long int n; long long int arr[100005], diff[100005]; map<long long int, long long int> maxima, minima; struct node { long long int max1, min1; }; node tr[7 * 100005]; node findSum(long long int qs, long long int qe...
18
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; map<string, long long> mp1, mp2; for (long long i = 0; i < n; i++) { string s; cin >> s; mp1[s]++; } for (long long i = 0; i < n; i++) { string s; cin >> s; mp2[s]++; } for (auto it = mp1.begin(); i...
4
#include <bits/stdc++.h> using namespace std; const int MAX_N = 3e5 + 200; int n, ai[MAX_N], pi[MAX_N]; long long f[MAX_N]; bool opt[MAX_N]; vector<int> ans; int process(int pos) { int val = min(ai[pos], ai[pos + 1]); if (val) ans.push_back(pos); ai[pos] -= val, ai[pos + 1] -= val; return val; } int main() { ...
24
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int sol = 0, tmp = 0; while (n) { n--; sol++; tmp++; if (tmp == m) { tmp = 0; n++; } } cout << sol << endl; }
1
#include <bits/stdc++.h> using namespace std; const int M = 210; const int N = 30; struct Node { int len; string l, r; set<string> ss; int now; } a[M]; int n; char s[M]; void update(Node &a) { bool flag = true; for (int t = a.now + 1;; ++t) { for (int j = 0; j < 1 << t && flag; ++j) { int v = j; ...
14
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; static const ll INF = 1000000000000000000ll; static ll dp[2][64][8192]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<pii> tasks(n); for (int i = 0; i < n; ++i) { cin >> tasks[i].first;...
17
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; if (s[0] == 'h') { cout << "http://"; int i; for (i = 4; i < s.length(); i++) { if (i > 4 && s[i] == 'r' && s[i + 1] == 'u') break; cout << s[i]; } cout << ".ru"; i += 2; if (i < s.length()) { ...
3
#include <bits/stdc++.h> using namespace std; int n, m, i; pair<int, pair<int, string> > p[100005]; int main() { cin >> n >> m; for (i = 1; i <= n; i++) { cin >> p[i].second.second >> p[i].first >> p[i].second.first; } p[n + 1].second.second = "///"; p[n + 1].second.first = INT_MAX; p[n + 1].first = INT...
5
#include <bits/stdc++.h> using namespace std; inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } int q[200005]; bool vis[200005]; int main() { int m, a, b; cin >> m >> a >> b; int k = gcd(a, b); long long ans = (long long)(m / k + 1) * (m / k + 2) / 2 * k - (long long)((m / k + 1) ...
13
#include <bits/stdc++.h> using namespace std; int arr[5000]; long long int cost[5000]; vector<long long int> v[5000]; bool cmp(long long int a, long long int b) { if (a > b) return true; return false; } int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> arr[i] >> cost[i]; for (int i = ...
9
#include <bits/stdc++.h> using namespace std; struct s_ix { long long ix; long long val; }; bool operator<(s_ix aa, s_ix bb) { return aa.val - aa.ix < bb.val - bb.ix; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long T; cin >> T; while (T--) { long long n, k, sum, nbw, maxnbw; cin >> n...
7
#include <bits/stdc++.h> using namespace std; inline int read() { char ch = getchar(); int w = 1, c = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) c = (c << 1) + (c << 3) + (ch ^ 48); return w * c; } const int M = 3e5 + 10, inf = 1e9; int head[M / 10],...
15
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long inf = 1LL << 62; const long long mx = 100005; const double eps = 1e-10; long long dx[10] = {1, 0, -1, 0}; long long dy[10] = {0, -1, 0, 1}; long long power(long long a, long long b) { if (b == 0) return 1; long long x = ...
0
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; long long n, ans, now; long long num[1010000]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> num[i]; for (int i = 1; i <= n; i++) if (num[i] != -1) num[i] = 0; else break; ans += num[n]; num[n] = 0x...
16
#include <bits/stdc++.h> using namespace std; vector<long long> d1, d2; vector<long long> d, h; struct node { long long mai; long long m1, m2; int l1, l2; node() {} node(long long mm, long long mm1, long long mm2, int ll1, int ll2) { mai = mm; m1 = mm1; m2 = mm2; l1 = ll1; l2 = ll2; } };...
15
#include <bits/stdc++.h> using namespace std; string A[9]; int main() { long long test; cin >> test; for (int cs = 1; cs <= test; cs++) { for (long long i = 0; i < 8; i++) cin >> A[i]; long long x1, y1, x2, y2; bool ys = true; for (long long i = 0; i < 8; i++) { for (long long j = 0; j < 8; ...
7
#include<iostream> using namespace std; int main() { int t,a,b; cin>>t; while(t--) { char n='b'; cin>>a>>b; for(int i=1;i<=b;i++) cout<<"a"; for(int i=1;i<=a-b;i++) { if(n==99){cout<<n;n='a';} else cout<<n++; } cout<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; string s, t; int a, b; int main() { cin >> s >> t; for (int i = 0; i < s.size(); ++i) if (s[i] == '1') a++; for (int i = 0; i < t.size(); ++i) if (t[i] == '1') b++; a += (a % 2); if (a >= b) cout << "YES" << endl; else cout << "NO" << endl; }
9
#include <bits/stdc++.h> using namespace std::chrono; using namespace std; const long long int mod = 998244353; int main() { cout << "? "; for (int i = 1; i < 101; ++i) cout << i << " "; cout << endl; int x; cin >> x; x = x & (((1 << 7) - 1) << 7); cout << "? "; for (int i = 1; i < 101; ++i) { int j...
6
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, p; vector<int> x; cin >> a; for (int i = 0; i < a; i++) { cin >> b; bool er = false; for (int j = 0; j < b; j++) { cin >> c; x.push_back(c); p = c; } sort(x.begin(), x.end()); for (int j = 1; j < b;...
0
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, k1, k2, n; cin >> a1 >> a2 >> k1 >> k2 >> n; int min = 0, max = 0; if (a1 * (k1 - 1) + a2 * (k2 - 1) >= n) min = 0; else min = n - (a1 * (k1 - 1) + a2 * (k2 - 1)); int p = a1 * k1 + a2 * k2; max = a1 + a2; p = p - n; if (k1...
2
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; const long long MOD = 1e9 + 7; const long long MODD = 1e9 + 1; const long long MODL = 998244353; const long long N = 200005; const long long INF = 1e18; const long long mx = 1e5 + 1; long long Power(long long x, long long y) { long long r =...
12
#include <bits/stdc++.h> int main() { int a; scanf("%d", &a); int type1 = 0; int type2 = 0; int type3 = 0; int b = 1; int num; while (b != a + 1) { scanf("%d", &num); type1 += num; b++; if (b >= a + 1) { break; } scanf("%d", &num); type2 += num; b++; if (b >= a ...
0
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, a[N], s[N], js; int p, q; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { int m; scanf("%d", &m); int t = m >> 1; for (int j = 0; j <= m - 1; ++j) { scanf("%d", &a[j]); if (j < t) p += a[j]; ...
12
#include <bits/stdc++.h> using namespace std; const long long NUM = 1000000009; const long long NUM2 = 998244353; long long binpow(long long a, long long b) { if (b == 0) return 1; long long res = binpow(a, b / 2) % NUM; if (b % 2) return (((res * res) % NUM) * a) % NUM; else return (res * res) % NUM; }...
7
#include <bits/stdc++.h> using namespace std; int n, x[55], y[55], r[55]; string s[55], ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d%d", &x[i], &y[i], &r[i]); for (int i = 1; i <= n; ++i) { s[i] = ""; x[i] /= 2; if (x[i] < 10) s[i] += '0' + x[i]; else { s...
14
#include <bits/stdc++.h> using namespace std; const int N = 250003; template <typename T> void read(T &x) { int ch = getchar(); x = 0; for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; } int n, cnt, head[N], to[N << 1], nxt[N << 1], f[18][N]...
24
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; const int MAX = 100000; const long long lim = 1e18; struct Enemy { long long mxH; long long hlth; long long reg; vector<pair<long long, int>> v; void generate(long long strt, long long dmg) { if (!v.size() && hlth <= dmg) { v.push_ba...
17
#include <bits/stdc++.h> using namespace std; long long n, k; long long dp1[100005][40], dp2[100005][40], dp3[100005][40]; long long ans1[100005], ans2[100005]; int main() { scanf("%lld", &n); scanf("%lld", &k); for (int i = 0; i < n; i++) { scanf("%lld", &dp1[i][0]); } for (int i = 0; i < n; i++) { a...
13
#include <bits/stdc++.h> using namespace std; const int maxP = 17; const int maxN = 100010; const int maxS = 1 << 17; bool f[maxP][maxP], dp0[maxS], dp1[maxS], dp2[maxS]; int cnt[maxN], n, m; char s[maxN]; int main() { cin >> n >> m >> s; for (int i = 0; i < n; i++) cnt[int(s[i] -= 'a')]++; for (int i = 0; i < m;...
17
#include <bits/stdc++.h> using namespace std; void read(long long &x) { x = 0; char c = getchar(); int f = 1; while (c < '0' || c > '9') { if (c == '-') f = -f; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } x *= f; return; } long lo...
17
#include <bits/stdc++.h> using namespace std; using K = long double; using PII = pair<int, int>; using PKK = pair<K, K>; const int N = 1e6; PII p[N]; map<PII, int> M; bool xgty(PII a) { return a.first > a.second; } K dst(PII xy, PKK a) { return abs(xy.first - a.first) + abs(xy.second - a.second); } void order(PII* be...
13
#include <bits/stdc++.h> using namespace std; int C[100010]; int n, m; int p[100010], c[100010]; bool s[100010]; int find(int x) { if (p[x] == x) return x; return p[x] = find(p[x]); } void join(int x, int y) { int xr = find(x), yr = find(y); c[yr] = min(c[yr], c[xr]); p[xr] = yr; } int main() { scanf("%d %d...
5
#include <bits/stdc++.h> using namespace std; const int N = 2e2 + 5; const int mod = 1e9 + 7; struct Point { long long int x, y; Point operator-(Point oth) { return (Point){x - oth.x, y - oth.y}; } long long int operator*(Point oth) { return x * oth.y - y * oth.x; } } a[N]; vector<pair<Point, Point> > v; int n, d...
17
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; const long long infLL = 9000000000000000000; template <typename first, typename second> ostream& operator<<(ostream& os, const pair<first, second>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream& op...
14
#include <bits/stdc++.h> int Compare(const void* x1, const void* x2) { return ((*((int*)x1)) - (*((int*)x2))); } int main(void) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); int ans = 2 * (abs(x1 - x2) + 1 + abs(y1 - y2) + 1); if (x1 == x2) { ans += 2; } if (y1 == y2) { ans += 2;...
3
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n == 1 or n == 2) return cout << -1 << endl, 0; n = n * n; long long ans; if (n % 2 == 1) ans = 1; else ans = 2; long long x = n / ans, y = ans; cout << (x + y) / 2 << ' ' << (x - y) / 2 << endl; }
7
#include <bits/stdc++.h> using namespace std; int Trie[7000001][2], sz = 0, A[7000001]; void insert(long long int a) { int cur = 0; A[cur]++; for (int i = 31; i >= 0; i--) { int d = (a & (1ll << i)) ? 1 : 0; if (Trie[cur][d] == 0) { Trie[cur][d] = ++sz; } cur = Trie[cur][d]; A[cur]++; ...
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> a(n), b(m), ans; for (int i = 0; i < n; ++i) cin >> a[i]; set<int> s; for (int i = 0; i < m; ++i) { cin >> b[i]; s.insert(b[i]); } for (int...
0
#include <bits/stdc++.h> using namespace std; int n, c, a[200020], b[200020]; long long dp[200020][3]; long long rec(int ind, bool elev) { if (ind == -1) return 0; if (dp[ind][elev] != -1) return dp[ind][elev]; if (elev) return dp[ind][elev] = min(rec(ind - 1, false) + a[ind], rec(ind - 1, true...
9
#include <bits/stdc++.h> #pragma comment(linker, "/stack:64000000") using namespace std; template <typename first> inline first abs(const first& a) { return a < 0 ? -a : a; } template <typename first> inline first sqr(const first& a) { return a * a; } const long double PI = 3.1415926535897932384626433832795; const ...
18
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 69, base = 1024 * 1024, mod = 1e9 + 7; int n; tuple<int, int, int, int> pref[N]; tuple<int, int, int, int> suma(tuple<int, int, int, int> a, tuple<int, int, int, int> b) { tuple<int, int, int, int> re; get<0>(a) =...
8
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f; const int N = 3e5 + 10; const long long mod = 998244353; long long n, m; long long a[N], vis[N], c[N], k; long long ans[N]; vector<long long> mp[N], op[N], v[N]; long long x, y, w; long long lowbit(long long x) { return x & (-x); } void add...
11
#include <bits/stdc++.h> using namespace std; int main() { int n, i, flag = 0; string s; cin >> n; cin >> s; for (i = 0; i < n - 1; i++) { if (flag == 0 && s[i] != s[i + 1]) { flag = 1; cout << "YES" << endl; cout << s[i] << s[i + 1]; } } if (flag == 0) cout << "NO"; return 0; ...
2
#include <bits/stdc++.h> using namespace std; const int N = 100000; struct Binary_Indexed_Tree { int tree[N + 1]; Binary_Indexed_Tree() { memset(tree, 0, sizeof(tree)); return; } int lowbit(int x) { return x & -x; } void add(int x, int y) { if (x == 0) return; for (int i = x; i <= N; i += lowb...
19
#include <bits/stdc++.h> using namespace std; int x, ans, n, t, mul = 1; string s; int main() { cin >> s; if (!(s[0] - 48 - 1)) { cout << long(pow(9, int(s.length() - 1))); exit(0); } for (int i = 0; i < s.length(); i++) { x = mul * (int(s[i]) - 49) * pow(9, int(s.length() - i - 1)); ans = max(a...
4
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long GCD(long long a, long long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } long long LCM(long long a, long long b) { return (a * b) / GCD(a, b); } long long bpow(long long x, long long v) { if (v ==...
5
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<pair<int, int> > M, F; cin >> n; for (int i = 0; i < n; i++) { char c; int a, b; cin >> c >> a >> b; if (c == 'M') { M.push_back(make_pair(a, b)); } else F.push_back(make_pair(a, b)); } int ans = 0; fo...
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int& x : a) scanf("%d", &x); vector<vector<int>> Idx(n); for (int i = 0; i < n; i++) Idx[a[i]].push_back(i); vector<int> Ans; for (int v = 0, i = 0; i < n; i++, v++) { while (v >= 0 && Idx[v].empty()) ...
11
#include <bits/stdc++.h> using namespace std; long long a[1000010]; int main() { long n, i; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; } if (n % 2 == 1) { cout << (n - 1) / 2 << endl; } else if (n % 2 == 0) { cout << (n - 2) / 2 << endl; } sort(a + 1, a + n + 1); if (n % 2 == 0) { ...
2