solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 1, msiz = 2, mod = 1e9 + 7; long long modplus(long long obj1, long long obj2) { long long res = obj1 + obj2; if (res >= mod) return res - mod; return res; } long long product(long long obj1, long long obj2) { return (obj1 * obj2) % mod; ...
12
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn]; vector<int> v; int main() { int n, k, mi; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int p = 0, f = 0, cnt = 0; for (int i = 0; i < n; i++) { if (a[i] == a[i - 1]) continue; ...
2
#include <bits/stdc++.h> using namespace std; const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230; const long long MOD = 1000000007LL; const long long INF = 1e9; const long long LINF = 1e18; const long double EPS = 1e-9; const long double GOLD = ((1 + sqrt(5)) / 2); template <...
10
#include <bits/stdc++.h> using namespace std; bool num[3010]; int main() { ios::sync_with_stdio(false); int n; while (cin >> n) { memset(num, false, sizeof(num)); int temp; for (int i = 0; i < n; i++) { cin >> temp; num[temp] = true; } for (int i = 1; i <= 3001; i++) if (!num...
4
#include <bits/stdc++.h> int main() { long long n, k, a = 1, c; std::cin >> n >> k; c = n; for (int i = 0; i < k; i++) a = a * 10; c *= a; while (n != 0 && a != 0) { if (n >= a) n = n % a; else a = a % n; } std::cout << c / (a + n); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T, class V> ostream& operator<<(ostream& s, pair<T, V> a) { s << a.first << ' ' << a.second; return s; } int cost[2][1005][1005]; pair<int, int> mcld[2][2005]; int dp[2][1005]; int p[2][2005]; int con[2][1005]; int n, a, b; int csolve(int t, int l, int r...
16
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, i, ans = 0, j; string s; cin >> n >> k; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; cin >> s; vector<long long int> b; b.push_back(a[0]); for (i = 1; i < n; i++) { if (s[i] != s[i - 1]) { sort(b.begin...
5
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> item[1100]; int S[1100]; double f[1100][1100]; double g[1100][1100]; double C[1100][1100]; int main() { cin >> n >> m; for (int i = (1); i <= (m); i++) { int k; scanf("%d", &k); while (k--) { int x; scanf("%d", &x); it...
18
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(int argc, const char* argv[]) { ll n, m; cin >> n >> m; ll fs[n]; ll ans = n; for (ll i = 0; i < n; i++) { fs[i] = 0; } for (ll i = 0; i < m; i++) { ll u, v; cin >> u >> v; if (u < v) { if (fs[u - 1] == 0) {...
6
#include <bits/stdc++.h> using namespace std; int n, a[4], b[4]; int main() { cin >> n; for (int i = 1; i < 4; ++i) cin >> a[i] >> b[i], n -= a[i], b[i] -= a[i]; for (int i = 1; i < 4; ++i) cout << a[i] + min(b[i], n) << " ", n -= min(b[i], n); cout << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int M = 300001; int n, m; vector<int> v[M], w[M]; int a[M], b[M], c[M]; int col[M], q[M], sto[M]; const int inf = 1e9; int can(int color) { for (int i = 1; i <= n; i++) v[i].clear(), w[i].clear(); memset(col, 0, sizeof(col)); for (int i = 1; i <= m; i++) { i...
14
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int n, yo, a; cin >> n >> yo; int ini = yo; int x = 1001; std::vector<int> v(x); for (int i = 1; i < n; ++i) { cin >> a; v[a]++; } int actual = 0; for (int i = x; i >= 0 && yo <= i; --i) { actual...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int n1, n2; cin >> n1; int a[n1]; for (int i = 0; i < n1; i++) cin >> a[i]; cin >> n2; int b[n2]; for (int i = 0; i < n2; i++) cin >> b[i]; int m[n1 + n2]; if (n1 == 100 && n2 == 100) cout << "I become the guy."; els...
0
#include <bits/stdc++.h> using namespace std; int n; int a[11111], b[11111], c; inline long long get(long long mid) { long long ans = n; for (int i = 0; i < n; ++i) { ans += mid * a[i] / b[i]; if (ans > c) return ans; } return ans; } int main() { scanf("%d%d", &n, &c); for (int i = 0; i < n; ++i) sc...
9
#include <bits/stdc++.h> using namespace std; int read_int() { char c = getchar(); while ((c < '0' || c > '9') && c != '-') { c = getchar(); } bool sign = 0; if (c == '-') { sign = 1; c = getchar(); } int ret = 0; while (c >= '0' && c <= '9') { ret = 10 * ret + c - 48; c = getchar();...
16
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int t; double r, v, f, s; int check(double t) { double dis = v * t; double len = fmod(dis, 2 * pi * r); double k = len / r / 2.0; double dis2 = 2.0 * abs(sin(k)) * r; if (dis + dis2 > f - s) return 1; return 0; } int main() { scan...
17
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int count_upp[s.size() + 1], count_low[s.size() + 1]; count_low[0] = 0; count_upp[0] = 0; for (int i = 0; i < s.size(); ++i) { if (isupper(s[i])) count_upp[i + 1] = 1 + count_upp[i]; else count_upp[i + 1] = co...
6
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 1; vector<int> num[40]; char s1[100], s2[100]; int n; int dfs(int d, int k) { if (d == n - 2) return num[k].size(); int n1 = num[k].size(); int res = 0; for (int i = 0; i < n1; i++) { res += dfs(d + 1, num[k][i]); } return res; } in...
5
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int a[N]; int main() { int n, q; cin >> n >> q; int s[2] = {0, 1}; for (int i = 0; i < q; ++i) { int tp, x; scanf("%d", &tp); if (tp == 1) { scanf("%d", &x); x = (-x + n) % n; if (x % 2 == 0) { s[0] = (s[...
10
#include <bits/stdc++.h> using namespace std; const int MAXW = 12; const int MAXN = 4e4; const int MAXM = 6e4; int W, N, M; int cnt[1 << MAXW]; int val[1 << MAXW][1 << MAXW]; int main() { (void)scanf("%d %d %d", &W, &N, &M); for (int i = 0; i < N; i++) { int a; (void)scanf("%d", &a); cnt[a]++; } for...
20
#include <bits/stdc++.h> using namespace std; int arr[200100]; multiset<int> v[200100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> arr[i]; } int ind = 1; string s; cin >> s; long long sum = 0; v[ind].i...
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int st[400000], lz[400000]; int query(int x, int n, int l, int r) { if (l == r) return lz[n]; else { int m = (l + r) / 2; if (lz[n]) { lz[2 * n] = (lz[2 * n] + lz[n]) % MOD; lz[2 * n + 1] = (lz[2 * n + 1] + lz[n]) % MOD; ...
9
#include <bits/stdc++.h> using namespace std; long long n, x, maxx = 0, ans, sum = 0; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x; sum += x; maxx = maxx < x ? x : maxx; } ans = (sum % (n - 1) ? 1 : 0) + sum / (n - 1); cout << (maxx > ans ? maxx : ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int n; pair<long long, long long> a[300004]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; pair<long long, int> le, ri; le.first = -INT_MAX, ri.first = INT_MAX; for (int i = (int)1; i <= (int)n; i++) { cin >> a[i].first >> a...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t))...
11
#include <bits/stdc++.h> using namespace std; int n; long long pw[15]; long long strtoll(string s) { stringstream st; st << s; long long x; st >> x; return x; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); pw[0] = 1; for (int i = 1; i < 15; i++) pw[i] = pw[i - 1] * 10; cin >>...
12
#include <bits/stdc++.h> using namespace std; const int Maxn = 2 * 1000 + 1000; bool rowup[Maxn], colright[Maxn], rowdown[Maxn], colleft[Maxn], a[Maxn][Maxn]; int n, out; void input() { ios_base::sync_with_stdio(false); string s; cin >> n; for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < n; j...
12
#include <cstdio> int read() { static int c, x; while ((c = getchar()) < 48) {} x = c & 15; while ((c = getchar()) >= 48) x = x * 10 + (c & 15); return x; } int readalpha() { static int c; while ((c = getchar()) < 48) {} return c; } #define MAXN 200010 const int P = 998244353; int a, b; char s[MAXN];...
25
#include <bits/stdc++.h> using namespace std; int n, p; vector<int> ve[310000]; int use[310000]; int tr[310000]; int lowbit(int x) { return x & (-x); } void add(int id, int x) { if (id == 0) { tr[0] += x; return; } while (id <= n) { tr[id] += x; id += lowbit(id); } } int sum(int id) { int re =...
11
#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}; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } void PLAY() { cout << fixed << setprecision(1...
8
#include <bits/stdc++.h> using namespace std; int inf = 1e9 + 10; vector<pair<int, int> > E[600000]; int res[600000]; int ma[600000], mb[600000], mc[600000]; bool used[600000]; int R[600000], H[600000]; int par[600000]; int parEdge[600000]; int find(int a) { if (a != R[a]) R[a] = find(R[a]); return R[a]; } void uni...
18
#include <bits/stdc++.h> using namespace std; int n, q, ans[500005], a[500005]; struct node { int l, r, id; }; vector<node> v[2000005]; int read() { int f = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { f =...
17
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; const int INF = 2e9 + 5; vvi rootify(int root, vvi &g) { vvi ans(g.size()); vector<bool> vis(g.size()); vis[0] = 1; function<void(int)> ...
11
#include <bits/stdc++.h> using namespace std; vector<pair<int, pair<long long, int> > > v[300010]; vector<pair<int, pair<int, long long> > > vv; vector<int> ans; set<pair<long long, pair<int, int> > > s; int parents[300010]; long long value[300010]; int edges[300010]; int main() { int n, m, i, x, y; long long a, z ...
12
#include <bits/stdc++.h> int n, m, a, b, c, x[1005], y[1005], ans; using namespace std; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d%d", &a, &b, &c); x[a] += c; y[b] += c; ans += c; } for (int i = 1; i <= n; i++) ans -= min(x[i], y[i]); printf("%d", ans); re...
5
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; const size_t N = 600 + 5; const vector<int> BLANK = vector<int>(); const ll MOD1 = 1e9 + 7; const ll MOD2 = 1e9 + 9; bool col[N]; int n, pa[N], dep[N]; vector<int> g[N], vert[2]; ll get_hsh(const vector<int> &vc) { ll hsh1 =...
20
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int gi() { int x = 0, o = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return x * o; } int n, m, tag; vector<pa...
26
#include <bits/stdc++.h> using namespace std; int main() { int n = 0, m = 0, x = 0, y = 0; scanf("%d", &n); scanf("%d", &m); y = (n + 1) * 2; x = n - 1; if (m < x || m > y) { printf("-1\n"); return 0; } x = m; y = n; while (n && m) { if (m > n) { n--; m -= 2; printf("11...
6
#include <bits/stdc++.h> using namespace std; const int MAXK = 5; const int MAXN = 19; const int xi[3] = {0, 1, 1}; const int yi[3] = {1, 0, 1}; int x[MAXN], y[MAXN]; int f[1 << MAXN]; int n; int rec(int m) { int &res = f[m]; if (res != -1) return res; int a[MAXK][MAXK]; memset(a, 0, sizeof(a)); for (int i = ...
12
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int BASE = 1e9 + 7; const int D = 6; const int LIM = 999999; void Add(int &a, int b) { a += b; if (a >= BASE) a -= BASE; if (a < 0) a += BASE; } int Mul(int a, int b) { return 1LL * a * b % BASE; } int C[N]; int s0[N], s1[N], s2[N], dp[N]...
19
#include <bits/stdc++.h> using namespace std; int n, m; long long arr[1005][1005], srow[1005], scol[1005]; void doit() { long long minval = -1, currval, minix, minjx, ret, dist; cin >> n >> m; memset(srow, 0, sizeof(srow)); memset(scol, 0, sizeof(scol)); for (int i = 0; i < n; i++) for (int j = 0; j < m; ...
10
#include <bits/stdc++.h> using namespace std; int n, a[100005]; set<int> s1, s2, ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= n; ++i) { int ret = a[i]; if (i == 1) { s1.insert(ret); ans.insert(ret); } else if (i % 2 == 0...
8
#include <bits/stdc++.h> using namespace std; int n, sq, s; vector<long long int> ans(200005, 0); long long int answ = 0; struct Block { vector<pair<int, list<int>>> R; int size = 0; long long int sum = 0; int lazy = 0; void up(long long int sign) { answ += sign * sum; } void add(int i, int r, int pt) { ...
25
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 10000 + 10; long long n, m; long long func(long long x) { long long temp1 = n % m, temp2 = n / m; if (temp1 == 0) { return temp2; } if (x == 0 || x > temp1) { return temp2; } return temp2 + 1; } int main...
8
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long pwmd(long long a, long long n) { if (!n) return 1; long long pt = pwmd(a, n / 2); pt *= pt, pt %= 1000000007; if (n & 1) pt *= a, pt %= 1000000007; return pt; } void coder_pulkit_c() { ...
4
#include <bits/stdc++.h> using namespace std; int a, b, c; int main() { scanf("%d %d", &a, &b); while (b) { c = c * 10 + b % 10; b /= 10; } printf("%d", c + a); }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7, inv2 = (MOD + 1) / 2; double eps = 1e-4; int INF = 0x7fffffff; int inf = 1e9; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; long long c[300][300], mi1[300], mi2[300]; int read() { int v = 0, f = 1; char c = getchar(); while (c < 48 || ...
15
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string my_str[n]; for (int i = 0; i < n; i++) { cin >> my_str[i]; } string result = ""; for (int row = 0; row < n; row++) { for (int column = 0; column < m; column++) { bool found_row = false; bool foun...
3
#include <bits/stdc++.h> using namespace std; int i, j, cnt = 0; int ar[200050], br[200050]; bool cmp(int a, int b) { return ar[a - 1] < ar[b - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, ans = 1, c; cin >> n; for (int i = 0; i < n; i++) { cin >> ar[i]; ...
9
#include <bits/stdc++.h> using namespace std; int main() { int k, n, l = 0, fl = 0; cin >> k >> n; while (n != 1) { if (n % k == 0) { n /= k; l++; } else { fl = 1; n = 1; } } if (fl == 0) { cout << "YES" << "\n" << l - 1; } else cout << "NO"; r...
2
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> G[150], W[150]; bool Odw[150]; int RevOdp[150]; vector<int> Kol; bool spr(int v) { bool WewOdw[150]; int Rev[150]; for (int i = (0); i < (n); i++) WewOdw[i + 1] = Rev[i + 1] = 0; Rev[v] = -1; bool ok = 1; WewOdw[v] = 1; Kol.push_back(v); ...
11
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cout.write(nam...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const long long mod = 1000000007; long long quc[maxn], cuq[maxn]; int n, m; pair<int, int> A[maxn]; long long ans; long long pow(int a, int n) { long long s = 1, t = a; for (; n; n >>= 1) { if (n & 1) s = s * t % mod; t = t * t % mod; ...
17
#include <bits/stdc++.h> using namespace std; char s[1000006]; bool is[1000006]; int main() { int i, j, len; while (gets(s)) { len = strlen(s); memset(is, false, sizeof(is)); stack<int> sd; for (i = 0; i < len; i++) { if (s[i] == ')') { if (!sd.empty()) { is[i] = is[sd.top()]...
11
#include <bits/stdc++.h> using namespace std; long long int n, d, m, i, j, a[200], ans, sum; int main() { cin >> n >> d; for (i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + 1 + n); cin >> m; for (i = 1; i <= m; i++) sum += a[i]; if (m <= n) cout << sum << endl; else cout << sum - (m - ...
2
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int R, gx[4] = {-1, 0, 1, 0}, gy[4] = {0, -1, 0, 1}, id[55 << 1][55 << 1], tot, pl[55 * 55 << 2][2], A[7850][7850]; long long P[4], d[55 * 55 << 2]; long long ksm(long long a, long long b) { long long r = 1; for (; b; b >>= 1) { if...
23
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; template <typename T> void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) { write(x / 10); } putchar(x % 10 + '0'); } template <typename T> void read(T &x) { x = 0; char ch = getchar(); long long f = 1; wh...
2
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:200000000") const double EPS = 1E-9; const int INF = 1000000000; const long long INF64 = (long long)1E18; const double PI = 3.1415926535897932384626433832795; pair<int, int> a[110000]; int main() { int n, m; cin >> n >> m; for (int i = ...
5
#include <bits/stdc++.h> using namespace std; void solve(long long TEST_CASE) { long long n, m; cin >> n >> m; vector<long long> p(n); for (long long i = 0; i < n; i++) { cin >> p[i]; p[i]--; } map<long long, long long> f; for (long long j = 0; j < n; j++) f[(n + j - p[j]) % n]++; set<long long>...
13
#include <bits/stdc++.h> using namespace std; long long pp, i, j, k, l, m, n, b, s, a, c, t; string p, q; int main() { cin >> a >> b >> c >> m >> n; s = min(a, b); s = min(s, c / 2); s = min(s, m / 7); s = min(s, n / 4); cout << s; }
9
#include <bits/stdc++.h> using namespace std; int jmp[111111][20]; int fun[111111][20][10]; int h[111111], ans[10], tmp[10]; int n, m, q; vector<int> a[111111], val[111111]; void dfs(int v, int pr = -1, int hgt = 0) { jmp[v][0] = pr; h[v] = hgt; for (int i = 0; i < (int)(a[v]).size(); ++i) { if (a[v][i] != pr...
14
#include <bits/stdc++.h> using namespace std; int len, x, y, curx, cury, g[510][510], a[100010]; string s; bool isvalid(int x0, int y0) { if (x0 <= x && x0 >= 1 && y0 <= y && y0 >= 1) return true; return false; } void calc(char ch) { int i, j, k, x, y, ans; x = curx; y = cury; if (ch == 'L') { if (isval...
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") 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(n...
9
#include <bits/stdc++.h> using namespace std; map<string, pair<int, int> > m; void print(int a, int b) { if (a == -1) { cout << "errtype" << endl; } else { string s(a, '&'), t(b, '*'); cout << s << "void" << t << endl; } } int main() { int n; cin >> n; m["void"] = make_pair(0, 0); for (int i =...
10
#include <bits/stdc++.h> using namespace std; void yahoo() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } bool fun(pair<long long, long long> x, pair<long long, long long> y) { if (x.first + x.second > y.first + y.second) { if (x.first > x.second) { return true; } if (x.f...
8
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); long long mo = 1000000007, out = 0; for (int i = 0; i < n; i++) { if (s[i] == '0') continue; long long ze = 1, on = 1; for (int j = 0; j < i; j++) ze = (ze * 2) % mo; for (int j = i + 1; j < n; j++...
8
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } struct Fenwick { vector<int>...
21
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; int i; int sum = 0; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } if (k < (n - 1) * 10 + sum) { cout << -1 << endl; } else { int d = k - sum - (n - 1) * 10; cout << (d / 5) + (n - ...
1
#include <bits/stdc++.h> using namespace std; const int mx1 = 15; const int mx2 = 505; const int mx3 = 5005; const int mx4 = 30005; const int mx5 = 100005; const int mx6 = 1000005; int _toggle(int N, int pos) { return N = N ^ (1 << pos); } int _set(int N, int pos) { return N = N | (1 << pos); } int _reset(int N, int po...
9
#include <bits/stdc++.h> int main() { int count = 0; scanf("%d", &count); int inp = 0; int first = 0, second = 0; for (int i = 0; i < count; i++) { scanf("%d", &inp); if (inp % 2) first++; else second++; } int answer = 0; if (first > second) { answer = second + (first - secon...
7
#include <bits/stdc++.h> using namespace std; unsigned long long MaxY(unsigned long long x, unsigned long long y) { unsigned long long min, max; unsigned long long r; max = x > y ? x : y; min = x < y ? x : y; if (max % min == 0) return min; while (max % min != 0) { r = max % min; max = min; min ...
10
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n = 5; int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int x; cin >> x; if (x == 1) { ans += abs(i - 2) + abs(j - 2); } } } cout << ans <...
0
#include <bits/stdc++.h> using namespace std; bool cmp(pair<int, int> &a, pair<int, int> &b) { return (a.first > b.first); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n, k; cin >> n >> k; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back({x...
4
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; long long a, b, k; long long ans; int main() { ans = 0; scanf("%I64d %I64d", &a, &b); while (1) { if (a > b) { k = a / b; ans += k; a -= k * b; } else { k = b / a; ans += k; b -= k * a; } if ...
8
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k, n, m; scanf("%lld", &n); m = n / 10; if (n % 10 <= 5) { n = m * 10; } else { n = (m + 1) * 10; } printf("%lld\n", n); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 1000 + 20; long long int p[N], h[N], ttt; int n, m; vector<int> e[N]; bool check(long long int mid) { long long int ptr = 0; for (int i = 0; i < n; i++) { long long int mm = mid; if (h[i] > p[ptr]) { if (h[i] - p[ptr] > mid) { ...
11
#include <bits/stdc++.h> using namespace std; unsigned long long int dist(unsigned long long int x1, unsigned long long int y1, unsigned long long int x2, unsigned long long int y2) { unsigned long long int ans = 0; if (x1 >= x2) ...
9
#include <bits/stdc++.h> using namespace std; int jml, dorm, me, a, lepas, ttl; double jawab; int main() { scanf("%d %d %d", &jml, &dorm, &me); for (int i = 1; i <= dorm; i++) { scanf("%d", &a); ttl += a; if (i != me) lepas += a; } if (jml > ttl) jawab = -1.0; else { if (lepas < jml - 1) ...
8
#include <bits/stdc++.h> using namespace std; int P; int g1() { double p = rand() / 32767.0, s = 0, g = 1; for (int k = 0;; k++) { if (k) g *= k; double c = pow(P, k) * exp(-P) / g; s += c; if (p <= s) return k; } } int g2() { double p = rand() / 32767.0, s = 0; for (int k = 0; k <= P + P; k++...
20
#include <bits/stdc++.h> using namespace std; int n, t[100100], ans, tot; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &t[i]); sort(t + 1, t + n + 1); for (int i = 1; i <= n; i++) if (tot <= t[i]) tot += t[i], ans++; printf("%d\n", ans); }
5
#include <bits/stdc++.h> const int mod = 1000000007; const int inf = 1000000009; const long long INF = 1000000000000000009; const long long big = 1000000000000000; const long double eps = 0.000000000000000000001; using namespace std; int T[5005]; pair<int, int> K[5005]; int L[5005], P[5005], W[5005][2], C[5005]; int A[...
17
#include <bits/stdc++.h> #pragma GCC optimize("O3") int const N = 1e5 * 4; using namespace std; long long n, c, a, b, o, q, summ, maxx, minn, arr[N]; vector<long long> v; map<long long, long long> vv; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; minn = 1e18; while (n--) { ...
3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAX = 1e5 + 5; int L, n, p, t, dp[MAX], val[MAX], R[MAX]; struct Section { int l, r; } sec[MAX]; bool cmp(Section a, Section b) { return a.r < b.r; } int get(int x) { int ret = n + 1; for (int l = x + 1, r = n, mid = (x + 1 + n) >...
19
#include <bits/stdc++.h> using namespace std; int rem; int n, q, nex[310]; long long t, arr[310]; long long cnt[100010]; bool mark[310]; int dfs(int idx, long long val) { int cc = 0; if (nex[idx] != -1) cc = dfs(nex[idx], val + arr[idx]); t -= cc * arr[idx]; arr[idx] += val; return cc + 1; } int main() { me...
13
#include <bits/stdc++.h> char st[4][4]; int a[4][4]; int main() { for (int i = 0; i < 4; i++) scanf("%s", st[i]); int ok = 0; for (int i = 0; i < 4; i++) for (int j = 0; j < 4; j++) { if (st[i][j] == 'x') a[i][j] = 1; else if (st[i][j] == 'o') a[i][j] = -1; else a[i][...
3
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y, long long int p) { long long int r = 1; x = x % p; while (y) { if (y & 1) r = r * x % p; y = y >> 1; x = x * x % p; } return r; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cou...
8
#include <bits/stdc++.h> template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; template <typename T> vector<vector<T>> vec2d(int n, int m...
14
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long inf = 1e15 + 15; int n, m, q; int a[N], b[N], c[N]; vector<int> v[N]; long long pot[N]; long long dist[N]; priority_queue<pair<long long, int> > pq; bool visited[N]; void solve() { for (int i = 1; i <= n; ++i) { dist[i] = inf; ...
26
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool smin(T &a, const T &b) { return a > b ? a = b : a;...
20
#include <bits/stdc++.h> const size_t maxN = 200005; const size_t ioSize = 1 << 21; char iBuf[ioSize], *iS, *iE; inline char getc() { return iS == iE ? iE = iBuf + fread(iS = iBuf, 1, ioSize, stdin), iS == iE ? EOF : *iS++ : *iS++; } void readi(int &x) { char c; for (c = getc(); !isdigit(c); c...
19
#include <bits/stdc++.h> using namespace std; 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; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cer...
11
#include <bits/stdc++.h> using namespace std; int a[1000000], b[1000000], dp[1000000][2]; int n, c; int main() { cin >> n >> c; for (int i = 0; i < n - 1; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) cin >> b[i]; for (int i = 0; i < 1000000; i++) for (int j = 0; j < 2; j++) dp[i][j] = 1e9; dp[0][0] =...
9
#include <bits/stdc++.h> using namespace std; int main() { cout << "Karen\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 1e9, mod = 1e9 + 7; int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') f = (c == '-') ? -1 : 1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int a[1000001], b[1000001]; ...
10
#include<bits/stdc++.h> #define int long long using namespace std; const int N=2e5+5; int read() { int s=0; char c=getchar(),lc='+'; while (c<'0'||'9'<c) lc=c,c=getchar(); while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar(); return lc=='-'?-s:s; } void write(int x) { if (x<0) putchar('-'),x=-x; if (x<10) putchar(x+...
22
#include <bits/stdc++.h> using namespace std; template <class T> bool checkmax(T &a, T b) { return b > a ? a = b, 1 : 0; } template <class T> bool checkmin(T &a, T b) { return b < a ? a = b, 1 : 0; } const int mod = 1e9 + 7; template <class T> void my_add(T &a, T b) { b %= mod; a = a + b > mod ? a + b - mod : a...
7
#include <bits/stdc++.h> using namespace std; int n, a[100005], b[100005], c; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[i] = a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) if (b[i] != a[i]) c++; if (c > 2) puts("NO"); else puts("YE...
5
#include <bits/stdc++.h> #pragma GCC target("avx") #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fipa-sra") #pragma GCC optimize("-ftree-pre") #pragma GCC optimize("-ftree-vrp") #pragma GCC opt...
27
#include <bits/stdc++.h> using namespace std; const long long int mod = 998244353; void print(long long int n) { cout << n << ' '; } void yes() { cout << "YES" << '\n'; } void no() { cout << "NO" << '\n'; } void print(long long int a[], int i, int n) { for (; i < n; i++) { cout << a[i] << ' '; } } void printv(v...
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; struct Hash { long long x, y; Hash(long long x = 0, long long y = 0) : x(x), y(y) {} Hash operator+(const Hash& a) const { return Hash((x + a.x) % MOD, y + a.y); } Hash operator-(const Hash& a) const { return Hash((x - a.x + MOD) %...
12