solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; inline bool ZXBs_fast_input(int& x) { x = 0; int c; int sign = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') sign = -1; x = c ^ '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ '0'); x *= sign; return 1; } inline bool ZXBs_fast_output(int x) { if (x >= 10) ZXBs_fast_output(x / 10); putchar(x % 10 + '0'); return 1; } const int rp = 666666; const bool debug = 1; const bool I_good_vegetable_a = 1; int n; vector<int> tree[100100]; int par[100100]; int cntdeg[100100]; bool used[100100]; bool done[100100]; int pos = -1; bool eql = 0; int dfs(int now, int lst) { par[now] = lst; int ans = 1; for (int i = 0; i < tree[now].size(); i++) { if (tree[now][i] != lst) { int dfsdone = dfs(tree[now][i], now); ans += dfsdone; } } if (ans >= (n - ans) && pos < 0) { pos = now; if (ans == (n - ans)) eql = 1; } return ans; } bool check(int now) { memset(used, 0, sizeof(used)); queue<int> q1, q2; q1.push(now); used[now] = 1; while (!q1.empty()) { int deg = tree[q1.front()].size(); while (!q1.empty()) { int wa = q1.front(); q1.pop(); used[wa] = 1; if (tree[wa].size() != deg) { return 0; } for (int i = 0; i < tree[wa].size(); i++) { if (!used[tree[wa][i]]) { q2.push(tree[wa][i]); } } } swap(q1, q2); } return 1; } bool single_check(int now, int pre) { int len = 1; while (tree[now].size() == 2) { int ha = now; if (tree[now][0] == pre) now = tree[now][1]; else now = tree[now][0]; pre = ha; len++; } if (tree[now].size() == 1) { if (done[len]) return 0; if (check(now)) { cout << now + 1 << endl; exit(0); } done[len] = 1; } return 0; } signed main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; tree[a].push_back(b); tree[b].push_back(a); } dfs(0, -1); if (!eql) { if (check(pos)) { cout << (pos + 1) << endl; return 0; } for (int i = 0; i < tree[pos].size(); i++) { int now = tree[pos][i]; if (tree[now].size() <= 2) { single_check(now, pos); } } cout << "-1" << endl; } else { int x = pos, y = par[pos]; memset(done, 0, sizeof(done)); single_check(x, y); memset(done, 0, sizeof(done)); single_check(y, x); cout << "-1" << endl; } return 0; }
2,400
CPP
x=0 for _ in range(int(input())): s=input() if s.count('+')==2: x+=1 elif s.count('-')==2: x-=1 print(x)
800
PYTHON3
import math n = int(input().strip()) if n%2 !=0: print(0) else: print(math.ceil(n/4)-1)
1,000
PYTHON3
s=input() n=len(s) i=0 while i<n and s[i]!='h': i=i+1 while i<n and s[i]!='e': i=i+1 while i<n and s[i]!='l': i=i+1 else: i=i+1 while i<n and s[i]!='l': i=i+1 while i<n and s[i]!='o': i=i+1 if i<n: print('YES') else: print('NO')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long p; long long a[200005]; long long f[200005]; long long Qpow(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % MOD; a = a * a % MOD; b >>= 1; } return ans; } long long C(long long x, long long y) { return f[y] * Qpow(f[x] * f[y - x] % MOD, MOD - 2) % MOD; } int main() { int n; scanf("%d", &n); if (n % 2 == 0) { for (int i = 1; i <= n; i++) scanf("%I64d", a + i); } else { long long temp; scanf("%I64d", &temp); if (n == 1) { printf("%I64d\n", temp % MOD); return 0; } for (int i = 2; i <= n; i++) { long long b; scanf("%I64d", &b); a[i - 1] = ((i % 2 == 0) ? ((temp + b) % MOD) : ((temp - b + MOD) % MOD)); temp = b; } n--; } p = n / 2 - 1; long long x = 0, y = 0; f[0] = 1; for (long long i = 1; i <= p; i++) { f[i] = f[i - 1] * i % MOD; } for (int i = 1; i <= n; i += 2) { long long q = C(i / 2, p) % MOD; x += (q * (a[i]) % MOD); y += (q * (a[i + 1]) % MOD); x %= MOD; y %= MOD; } if (p % 2 != 0) printf("%I64d\n", (x - y + MOD) % MOD); else printf("%I64d\n", (x + y) % MOD); return 0; }
2,200
CPP
def fact(n): if n == 0: return 1 return n*fact(n-1) n = int(input()) print(fact(2*(n-1)) // (fact(n-1))**2)
800
PYTHON3
def stabilize(a,n): if(n<=2): return 0 lar = max(a) small = min(a) diff = lar-small a.remove(small) diff1 = lar-min(a) a.remove(lar) diff2 = max(a) - small return min(diff,diff1,diff2) n = int(input()) a = [int(i) for i in range(n) and input().split()] print(stabilize(a,n))
900
PYTHON3
n=int(input()) x=0 y=0 z=0 for i in range(n): a,b,c=map(int,input().strip().split()) x+=a y+=b z+=c if(x == 0 and y==0 and z==0): print('YES') else: print('NO')
1,000
PYTHON3
n,m=input(),input() ans='' for i in range(len(n)): if(n[i]==m[i]): ans+='0' else: ans+='1' print(ans)
800
PYTHON3
n = 5 k = 0 for i in range(n): a = input().split() for j in range(5): if a[j] == '1': k = abs(2-i)+abs(2-j) print(k)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int find_first_num_index(long long &k, long long n) { if (n == 1) { return 0; } n--; int first_num_index; long long n_partial_fact = n; while (k >= n_partial_fact && n > 1) { n_partial_fact *= (n - 1); n--; } first_num_index = k / n_partial_fact; k %= n_partial_fact; return first_num_index; } vector<long long> find_kth_permutation(long long n, long long k) { vector<long long> Ans; set<long long> S; for (int i = 1; i <= n; i++) { S.insert(i); } auto itr = S.begin(); k -= 1; for (int i = 0; i < n; i++) { int index = find_first_num_index(k, n - i); advance(itr, index); Ans.push_back((*itr)); S.erase(itr); itr = S.begin(); } return Ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long n, q, t, x = 1; cin >> n >> q; long long suff_len = min(14LL, n); long long pref_len = n - suff_len; long long Sil[20]; Sil[0] = 1; for (int i = 1; i <= suff_len; i++) { Sil[i] = Sil[i - 1] * i; } vector<long long> Suff = find_kth_permutation(suff_len, 1); while (q--) { cin >> t; if (t == 1) { long long l, r, ans = 0; cin >> l >> r; int r_pr = min(r, pref_len); if (l <= r_pr) { ans += 1ll * (l + r_pr) * (r_pr - l + 1) / 2; } r -= pref_len + 1; l -= pref_len + 1; r_pr = max(r_pr - pref_len - 1, 0LL); for (int i = max(l, 0LL); i <= r; i++) { ans += pref_len + Suff[i]; } cout << ans << endl; } else { int dx; cin >> dx; x += dx; x = min(x, Sil[suff_len]); Suff = find_kth_permutation(suff_len, x); } } return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 10; int a[MAX]; vector<int> e[MAX]; int mx[MAX]; int ans[MAX]; void dfs1(int x, int f) { mx[x] = a[x] == 1 ? 1 : -1; for (auto v : e[x]) { if (v == f) continue; dfs1(v, x); if (mx[v] >= 0) mx[x] += mx[v]; } } void dfs2(int x, int f) { ans[x] = mx[x]; for (auto v : e[x]) { if (v == f) continue; int sv = mx[v], sx = mx[x]; if (mx[v] >= 0) mx[x] -= mx[v]; if (mx[x] >= 0) mx[v] += mx[x]; ans[v] = mx[v]; dfs2(v, x); mx[x] = sx, mx[v] = sv; } } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1, x, y; i < n; i++) { cin >> x >> y; e[x].push_back(y); e[y].push_back(x); } dfs1(1, 0); dfs2(1, 0); for (int i = 1; i <= n; i++) { cout << ans[i] << " \n"[i == n]; } }
1,800
CPP
#include <bits/stdc++.h> using namespace std; bool Judge(int a, int b, int x1, int y1, int x2, int y2) { if (a > b) swap(a, b); if (x1 + x2 <= a) { if (max(y1, y2) <= b) return 1; } if (x1 + x2 <= b) { if (max(y1, y2) <= a) return 1; } return 0; } int main() { int x0, y0, x1, y1, x2, y2; int flag = 0; scanf("%d%d%d%d%d%d", &x0, &y0, &x1, &y1, &x2, &y2); if (Judge(x0, y0, x1, y1, x2, y2)) flag = 1; if (Judge(x0, y0, y1, x1, x2, y2)) flag = 1; if (Judge(x0, y0, x1, y1, y2, x2)) flag = 1; if (Judge(x0, y0, y1, x1, y2, x2)) flag = 1; if (flag) printf("YES"); else printf("NO"); return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; string c; int a[n][27]; for (int i = 0; i < n; i++) { for (int j = 0; j < 26; j++) a[i][j] = 0; } for (int i = 0; i < n; i++) { cin >> c; for (int j = 0; j < c.size(); j++) a[i][c[j] - 'a'] = 1 - a[i][c[j] - 'a']; } unordered_map<int, int> m; int b[n]; for (int i = 0; i < n; i++) { int temp = 0; int po = 1; for (int j = 0; j < 26; j++) { temp += (a[i][j] * po); po *= 2; } a[i][26] = temp; b[i] = temp; m[temp]++; } sort(b, b + n); long long res = 0; int last = b[0]; int cnt = 1; for (int i = 1; i < n; i++) { if (b[i] == last) { cnt++; } else { if (cnt > 1) { long long x = cnt; x *= (x - 1); x /= 2; res += x; } last = b[i]; cnt = 1; } } if (cnt > 1) { long long x = cnt; x *= (x - 1); x /= 2; res += x; } int po = 1; for (int i = 0; i < 26; i++) { for (int j = 0; j < n; j++) { if (a[j][i] == 1) { res += m[a[j][26] - po]; } } po *= 2; } cout << res; }
1,600
CPP
m = input().split(" ") lim, bob, res = int(m[0]), int(m[1]), 0 while lim <= bob: lim *= 3 bob *= 2 res += 1 print(res)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e5; struct Line { long long k, b; long long get(long long x) { return k * x + b; } }; int n; long long delta = 0, pref[N], a[N]; struct ConvexHull { int sz; Line *hull; ConvexHull(int MX) { hull = new Line[MX]; sz = 0; } bool bad1(Line p, Line c, Line n) { return (n.b - p.b) * (p.k - c.k) <= (p.k - n.k) * (c.b - p.b); } bool bad2(Line p, Line c, Line n) { return (n.b - p.b) * (p.k - c.k) >= (p.k - n.k) * (c.b - p.b); } void add1(long long k, long long b) { Line next = (Line){k, b}; while (sz > 1 && bad1(hull[sz - 2], hull[sz - 1], next)) sz--; hull[sz++] = next; } void add2(long long k, long long b) { Line next = (Line){k, b}; while (sz > 1 && bad2(hull[sz - 2], hull[sz - 1], next)) sz--; hull[sz++] = next; } long long calc(long long x) { int l = -1, r = sz - 1; while (r - l > 1) { int m = (l + r) >> 1; if (hull[m].get(x) <= hull[m + 1].get(x)) l = m; else r = m; } return hull[r].get(x); } }; ConvexHull *ss; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; ConvexHull *ss = new ConvexHull(n); for (int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + a[i]; ss->add1(1, 0); for (int i = 2; i <= n; ++i) { delta = max(delta, ss->calc(a[i]) + pref[i - 1] - a[i] * i); ss->add1(i, -pref[i - 1]); } ss->sz = 0; ss->add2(n, -pref[n]); for (int i = n - 1; i >= 1; --i) { delta = max(delta, ss->calc(a[i]) - a[i] * i + pref[i]); ss->add2(i, -pref[i]); } long long ret = 0; for (int i = 1; i <= n; ++i) ret += a[i] * i; cout << ret + delta; return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long a[n], b[k]; for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < k; i++) { cin >> b[i]; } sort(a, a + n); sort(b, b + k); long long ans = 0; for (int i = 0; i < k; i++) { ans += a[n - i - 1]; if (b[i] == 1) { ans += a[n - i - 1]; } } long long s = 0; for (long long i = k - 1; i >= 0; i--) { if (b[i] == 1) continue; ans += a[s]; s += b[i] - 1; } cout << ans << endl; } return 0; }
1,400
CPP
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define all(x) (x).begin(), (x).end() #define sz(x) (int)((x).size()) #define jonathan ios_base::sync_with_stdio(0) #define livingston cin.tie(NULL); cout.tie(NULL) #define f first #define s second #define ull unsigned long long #define endl '\n' using namespace std; void yes() {cout << "Yes" << endl;} void no() {cout << "No" << endl;} int dx[] = {1, 0, -1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; const int INF = 1e9 + 9; const long long LINF = 1e18 + 8; const double EPS = 1e-7; const long long MOD = 1e9 + 7; //998244353 const double PI = acos(-1); vector<int> a; bool get(int i) { return (a[i] > a[i - 1] and a[i] > a[i + 1]) or (a[i] < a[i - 1] and a[i] < a[i + 1]); } void solve() { int n; cin >> n; a.clear(); a.resize(n + 10); for (int i = 1; i <= n; i++) { cin >> a[i]; } int cnt = 0; for (int i = 2; i <= n - 1; i++) { if (a[i] > a[i - 1] and a[i] > a[i + 1]) cnt++; if (a[i] < a[i - 1] and a[i] < a[i + 1]) cnt++; } if (n < 3) { cout << 0 << endl; return; } a[0] = a[1]; a[n + 1] = a[n]; int ans = cnt; for (int i = 1; i <= n - 1; i++) { int t = get(i - 1) + get(i) + get(i + 1); int z = a[i]; a[i] = a[i - 1]; int p = get(i - 1) + get(i) + get(i + 1); ans = min(ans, cnt - t + p); a[i] = a[i + 1]; p = get(i - 1) + get(i) + get(i + 1); ans = min(ans, cnt - t + p); a[i] = z; } cout << ans << endl; } int main() { jonathan; livingston; bool test = 1; int t = 1; if (test) cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; int m; cin >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; if (x - 1 >= 1) a[x - 1] += y - 1; if (x + 1 <= n) a[x + 1] += a[x] - y; a[x] = 0; } for (int i = 1; i <= n; i++) cout << a[i] << endl; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
800
CPP
n=int(input()) arr=sorted(list(map(int,input().split()))) ans=0 for i in range(1,n): ans+=arr[i]-arr[i-1]-1 print(ans)
800
PYTHON3
n = int(input()) f = map(int,input().split()) a = [] for i in f: a.append(i) l = [] t = 1 for i in range(n - 1): if a[i] <= a[i+1]: t = t + 1 else: l.append(t) t = 1 l.append(t) print(max(l))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const long long modd1 = 1e9 + 7, modd2 = 998244353, maxn = 10; const double pi = acos(-1); void solve() { long long n, i, j, m; cin >> n; long long b[n / 2 + 3]; for (i = 1; i <= n / 2; ++i) { cin >> b[i]; } long long mn = 0, a[n + 4]; for (i = 1; i <= n / 2; ++i) { a[i] = mn; a[n + 1 - i] = b[i] - a[i]; if (a[n + 1 - i] > a[n + 2 - i]) { long long xet = a[n + 1 - i] - a[n + 2 - i]; a[i] += xet; a[n + 1 - i] = a[n + 2 - i]; mn = a[i]; } } for (i = 1; i <= n; ++i) { cout << a[i] << ' '; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const int mo = 1000000007; const int P[3] = {1004535809, 998244353, 469762049}; int m, M, U[30005], V[30005], rev[66000], a[3][66000], b[3][66000], W[3][2][16][32768], g[66000], f[66000]; long long n, ans; long long XX(long long x, long long y, long long mo) { x %= mo; y %= mo; return ((x * y - (long long)((long double)x / mo * y) * mo) % mo + mo) % mo; } long long po(long long x, int y, long long mo) { long long z = 1; for (; y; y >>= 1, x = x * x % mo) if (y & 1) z = z * x % mo; return z; } void fft(int a[], int o, int d) { for (int i = 0; i < M; ++i) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int m = 2, k = 1, I = 0; m <= M; k = m, m <<= 1, ++I) for (int i = 0; i < M; i += m) for (int j = i; j < i + k; ++j) { int u = a[j], v = 1ll * a[j + k] * W[o][d][I][j - i] % P[o]; a[j] = (u + v) % P[o]; a[j + k] = (u - v) % P[o]; } if (d) { long long x = po(M, P[o] - 2, P[o]); for (int i = 0; i < M; ++i) a[i] = x * a[i] % P[o]; } } int CRT(long long x, long long y, long long z) { long long p = 1ll * P[0] * P[1], s = (XX(x * P[1], po(P[1], P[0] - 2, P[0]), p) + XX(y * P[0], po(P[0], P[1] - 2, P[1]), p)) % p; return (((z - s % P[2]) * po(p % P[2], P[2] - 2, P[2]) % P[2] + P[2]) % P[2] * (p % mo) + s % mo) % mo; } void mul(int f[], int g[], int S) { for (int i = 0; i < M; ++i) { long long x = po(2, 1ll * i * S % (mo - 1), mo); for (int o = 0; o < 3; ++o) a[o][i] = x * f[i] % mo, b[o][i] = g[i]; } for (int o = 0; o < 3; ++o) { fft(a[o], o, 0); fft(b[o], o, 0); for (int i = 0; i < M; ++i) a[o][i] = 1ll * a[o][i] * b[o][i] % P[o]; fft(a[o], o, 1); } for (int i = 0; i <= m; ++i) f[i] = CRT(a[0][i], a[1][i], a[2][i]); } int main() { scanf("%lld%d", &n, &m); if (n > m) { puts("0"); return 0; } for (M = 1; M <= m + m; M <<= 1) ; for (int i = 0; i < M; ++i) rev[i] = rev[i >> 1] >> 1 | (i & 1) * (M >> 1); for (int o = 0; o < 3; ++o) { for (int I = 0, m = 2, k = 1; m <= M; ++I, k = m, m <<= 1) { long long x = po(3, (P[o] - 1) / m, P[o]), y = 1; for (int i = 0; i < k; ++i) W[o][0][I][i] = y, y = y * x % P[o]; } for (int I = 0, m = 2, k = 1; m <= M; ++I, k = m, m <<= 1) { long long x = po(3, (1 - P[o]) / m % (P[o] - 1) + P[o] - 1, P[o]), y = 1; for (int i = 0; i < k; ++i) W[o][1][I][i] = y, y = y * x % P[o]; } } U[0] = 1; for (int i = 1; i <= m; ++i) U[i] = 1ll * U[i - 1] * i % mo; V[m] = po(U[m], mo - 2, mo); for (int i = m - 1; ~i; --i) V[i] = 1ll * V[i + 1] * (i + 1) % mo; g[0] = 1; for (int i = 1; i <= m; ++i) f[i] = V[i]; for (int S = 1; S <= n; S <<= 1) { if (n & S) mul(g, f, S); mul(f, f, S); } for (int i = n; i <= m; ++i) (ans += 1ll * g[i] * U[m] % mo * V[m - i] % mo) %= mo; printf("%lld\n", (ans + mo) % mo); return 0; }
3,300
CPP
#include <bits/stdc++.h> using namespace std; int dfn[100009], low[100009], head[100009], tot, cnt, num, st[100009], top, x, nu[100009], n, m, t[100009], a, b, h, c[100009], du[100009], ans, ji; bool vis[100009]; struct fef { int n, to, u; } an[100009 << 1]; inline void add(int u, int v) { an[++tot].n = head[u]; an[tot].to = v; head[u] = tot; an[tot].u = u; } void tarjan(int u) { vis[u] = 1; st[++top] = u; dfn[u] = low[u] = ++cnt; for (int i = head[u]; i; i = an[i].n) { int v = an[i].to; if (!dfn[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (vis[v]) low[u] = min(low[u], dfn[v]); } if (low[u] == dfn[u]) { num++; do { x = st[top--]; vis[x] = 0; nu[num]++; c[x] = num; } while (x != u); } } int main() { scanf("%d%d%d", &n, &m, &h); for (int i = 1; i <= n; ++i) scanf("%d", &t[i]); for (int i = 1; i <= m; ++i) { scanf("%d%d", &a, &b); if ((t[a] + 1) % h == t[b]) add(a, b); if ((t[b] + 1) % h == t[a]) add(b, a); } for (int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i); for (int i = 1; i <= tot; ++i) if (c[an[i].u] != c[an[i].to]) du[c[an[i].u]]++; ans = 0x3f3f3f3f; for (int i = 1; i <= num; ++i) if (!du[i] && nu[i] < ans) { ans = nu[i]; ji = i; } cout << ans << endl; for (int i = 1; i <= n; ++i) if (c[i] == ji) printf("%d ", i); return 0; }
1,900
CPP
def hamsters(n, k, capacities): INF = n best = (None,INF) i = 0 while i<k: t = n%capacities[i] if t<best[1]: best = (i,t) i += 1 if best[1]==INF: return (1,0) return (best[0]+1,n//capacities[best[0]]) n,k = list(map(int, input().split())) best = hamsters(n, k, list(map(int, input().split()))) print (f"{best[0]} {best[1]}")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; vector<long long> vec(n + 1); vector<long double> pref(n + 1); map<int, int> m; long double ans = 0; for (int i = 1; i <= n; i++) { cin >> vec[i]; long long x = vec[i]; if (m.count(x - 1) == 0) m.insert(pair<int, int>(x - 1, 0)); if (m.count(x)) m.insert({x, 0}); if (m.count(x + 1)) m.insert({x + 1, 0}); long double sum = (i - 1) * x - pref[i - 1] + (m[x - 1] * (x - 1) + m[x] * x + m[x + 1] * (x + 1)) - (x * (m[x - 1] + m[x] + m[x + 1])); ans += sum; m[vec[i]]++; pref[i] = pref[i - 1] + vec[i]; } cout << fixed << setprecision(0) << ans; return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main(int argc, char **argv) { const long long nmin = 1, nmax = 1000000; long long n, m, mt, res, rt, i, i0, i1, j, k, t, ax; string s, st; cin >> n; t = nmax; for (i = n / 7; (i >= 0) && (t == nmax); i--) { m = 7 * i; if ((n - m) % 4 == 0) { i0 = (n - m) / 4; if (i0 + i <= t) { i1 = i; t = i0 + i1; } } } if (t == nmax) cout << "-1" << endl; else { s = ""; for (i = 0; i < i0; i++) s += '4'; for (i = i0; i < t; i++) s += '7'; cout << s << endl; } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; map<string, pair<int, int> > ma; int main() { ma["ABU"] = make_pair(1, 3); ma["AIDS"] = make_pair(3, 4); ma["Action"] = make_pair(3, 6); ma["Agreement"] = make_pair(3, 4); ma["Agriculture"] = make_pair(3, 7); ma["American"] = make_pair(3, 27); ma["Arab"] = make_pair(1, 13); ma["Armacost"] = make_pair(3, 3); ma["Assets"] = make_pair(2, 3); ma["Bahrain"] = make_pair(1, 7); ma["Baldrige"] = make_pair(3, 9); ma["Bangemann"] = make_pair(1, 5); ma["Bond"] = make_pair(1, 3); ma["Both"] = make_pair(3, 3); ma["Bouldikian"] = make_pair(1, 3); ma["Brien"] = make_pair(3, 4); ma["Broadly"] = make_pair(2, 3); ma["Byrd"] = make_pair(3, 5); ma["CDs"] = make_pair(2, 7); ma["Cable"] = make_pair(3, 3); ma["Chartered"] = make_pair(2, 6); ma["Chicago"] = make_pair(2, 3); ma["Chirac"] = make_pair(3, 10); ma["Clark"] = make_pair(3, 3); ma["Clayton"] = make_pair(3, 4); ma["Clercq"] = make_pair(3, 8); ma["Commerce"] = make_pair(3, 5); ma["Commission"] = make_pair(3, 3); ma["Conable"] = make_pair(3, 5); ma["Congress"] = make_pair(3, 20); ma["Congressmen"] = make_pair(3, 5); ma["Croo"] = make_pair(3, 5); ma["DHABI"] = make_pair(1, 3); ma["DRAM"] = make_pair(3, 5); ma["Dattel"] = make_pair(1, 3); ma["Dealers"] = make_pair(1, 11); ma["Democrat"] = make_pair(3, 3); ma["Democratic"] = make_pair(3, 11); ma["Department"] = make_pair(3, 4); ma["Deputy"] = make_pair(3, 3); ma["Development"] = make_pair(3, 3); ma["ECGD"] = make_pair(3, 3); ma["EEP"] = make_pair(3, 7); ma["Economic"] = make_pair(3, 8); ma["Edwards"] = make_pair(3, 5); ma["Electric"] = make_pair(3, 3); ma["Electronic"] = make_pair(3, 3); ma["Europe"] = make_pair(3, 8); ma["FECs"] = make_pair(1, 6); ma["FOMC"] = make_pair(2, 8); ma["FSO"] = make_pair(3, 6); ma["Faced"] = make_pair(3, 3); ma["Fitzwater"] = make_pair(3, 3); ma["Fleming"] = make_pair(3, 3); ma["GATT"] = make_pair(3, 17); ma["General"] = make_pair(3, 5); ma["Giordano"] = make_pair(1, 8); ma["Gulf"] = make_pair(1, 4); ma["Heller"] = make_pair(1, 4); ma["Herstatt"] = make_pair(1, 4); ma["Hitachi"] = make_pair(3, 3); ma["Horner"] = make_pair(1, 5); ma["IDB"] = make_pair(3, 4); ma["India"] = make_pair(3, 10); ma["Indian"] = make_pair(3, 5); ma["Industries"] = make_pair(3, 3); ma["Islamic"] = make_pair(3, 4); ma["Jardine"] = make_pair(3, 3); ma["Johnson"] = make_pair(2, 3); ma["July"] = make_pair(3, 4); ma["Kyodo"] = make_pair(3, 4); ma["LDP"] = make_pair(3, 11); ma["Leader"] = make_pair(3, 3); ma["Lebanese"] = make_pair(1, 5); ma["Liberal"] = make_pair(3, 3); ma["Local"] = make_pair(2, 3); ma["London"] = make_pair(3, 11); ma["Lyng"] = make_pair(3, 3); ma["MITI"] = make_pair(3, 17); ma["MMTC"] = make_pair(3, 3); ma["MPs"] = make_pair(3, 4); ma["Malcolm"] = make_pair(3, 4); ma["Margaret"] = make_pair(3, 3); ma["Matsunaga"] = make_pair(3, 5); ma["McCarthy"] = make_pair(2, 3); ma["Melzer"] = make_pair(2, 3); ma["Miller"] = make_pair(3, 3); ma["Most"] = make_pair(3, 3); ma["NEC"] = make_pair(3, 6); ma["Nakasone"] = make_pair(3, 47); ma["Nashashibi"] = make_pair(1, 3); ma["News"] = make_pair(3, 4); ma["Notes"] = make_pair(2, 3); ma["November"] = make_pair(2, 22); ma["OTTAWA"] = make_pair(2, 4); ma["Officials"] = make_pair(3, 5); ma["PHLX"] = make_pair(1, 10); ma["PSL"] = make_pair(2, 3); ma["Partly"] = make_pair(1, 3); ma["Party"] = make_pair(3, 3); ma["Philadelphia"] = make_pair(1, 4); ma["Plaza"] = make_pair(1, 4); ma["Policy"] = make_pair(3, 8); ma["Prime"] = make_pair(3, 17); ma["Representative"] = make_pair(3, 3); ma["Republican"] = make_pair(3, 7); ma["Rich"] = make_pair(3, 3); ma["Richard"] = make_pair(3, 4); ma["Ronald"] = make_pair(3, 4); ma["Rostenkowski"] = make_pair(3, 3); ma["Russell"] = make_pair(2, 4); ma["Ryzhkov"] = make_pair(3, 4); ma["Saba"] = make_pair(3, 6); ma["Savings"] = make_pair(2, 3); ma["Senate"] = make_pair(3, 9); ma["Service"] = make_pair(3, 3); ma["Shiratori"] = make_pair(3, 3); ma["Shoichi"] = make_pair(3, 3); ma["Smart"] = make_pair(3, 5); ma["Stephens"] = make_pair(3, 5); ma["Sumita"] = make_pair(1, 6); ma["Tamura"] = make_pair(3, 4); ma["Tariffs"] = make_pair(3, 8); ma["Thatcher"] = make_pair(3, 4); ma["Their"] = make_pair(3, 4); ma["Third"] = make_pair(3, 5); ma["Though"] = make_pair(1, 3); ma["Tian"] = make_pair(1, 4); ma["Toshiba"] = make_pair(3, 4); ma["Vice"] = make_pair(1, 5); ma["Volcker"] = make_pair(2, 3); ma["Washington"] = make_pair(3, 38); ma["While"] = make_pair(3, 7); ma["White"] = make_pair(3, 12); ma["Willy"] = make_pair(3, 3); ma["Wireless"] = make_pair(3, 3); ma["Wolf"] = make_pair(3, 5); ma["World"] = make_pair(3, 9); ma["Wright"] = make_pair(3, 3); ma["Yasuhiro"] = make_pair(3, 12); ma["Yeutter"] = make_pair(3, 11); ma["abide"] = make_pair(3, 3); ma["able"] = make_pair(1, 3); ma["acceptances"] = make_pair(2, 10); ma["acquisition"] = make_pair(3, 3); ma["act"] = make_pair(3, 8); ma["add"] = make_pair(1, 4); ma["adverse"] = make_pair(3, 3); ma["aggregate"] = make_pair(2, 4); ma["aggregates"] = make_pair(2, 6); ma["agricultural"] = make_pair(3, 3); ma["alleged"] = make_pair(3, 13); ma["amendment"] = make_pair(3, 3); ma["amounts"] = make_pair(1, 4); ma["anger"] = make_pair(3, 4); ma["angry"] = make_pair(3, 4); ma["announcement"] = make_pair(3, 3); ma["annualised"] = make_pair(2, 5); ma["annualized"] = make_pair(2, 5); ma["anti"] = make_pair(3, 3); ma["application"] = make_pair(3, 5); ma["areas"] = make_pair(3, 5); ma["assistance"] = make_pair(1, 14); ma["attempt"] = make_pair(3, 4); ma["auto"] = make_pair(3, 3); ma["averaged"] = make_pair(2, 4); ma["band"] = make_pair(1, 9); ma["bankers"] = make_pair(1, 5); ma["barriers"] = make_pair(3, 6); ma["bearing"] = make_pair(2, 4); ma["bilateral"] = make_pair(3, 12); ma["booked"] = make_pair(2, 4); ma["books"] = make_pair(2, 5); ma["borrowings"] = make_pair(2, 8); ma["bringing"] = make_pair(2, 3); ma["broader"] = make_pair(2, 3); ma["car"] = make_pair(3, 4); ma["carefully"] = make_pair(1, 4); ma["cause"] = make_pair(3, 3); ma["cent"] = make_pair(3, 3); ma["certain"] = make_pair(3, 5); ma["chance"] = make_pair(3, 5); ma["changed"] = make_pair(2, 3); ma["chartered"] = make_pair(2, 7); ma["chequable"] = make_pair(2, 4); ma["chip"] = make_pair(3, 30); ma["chips"] = make_pair(3, 21); ma["circular"] = make_pair(1, 3); ma["claims"] = make_pair(3, 3); ma["closely"] = make_pair(1, 4); ma["coins"] = make_pair(2, 4); ma["come"] = make_pair(3, 14); ma["comment"] = make_pair(1, 4); ma["committed"] = make_pair(1, 4); ma["commodity"] = make_pair(1, 11); ma["common"] = make_pair(1, 3); ma["company"] = make_pair(3, 7); ma["compares"] = make_pair(1, 5); ma["computer"] = make_pair(3, 16); ma["concerned"] = make_pair(3, 4); ma["concerns"] = make_pair(3, 3); ma["conciliatory"] = make_pair(3, 4); ma["confident"] = make_pair(3, 3); ma["considering"] = make_pair(3, 3); ma["consultations"] = make_pair(3, 4); ma["contract"] = make_pair(1, 4); ma["contractionary"] = make_pair(2, 3); ma["contracts"] = make_pair(1, 5); ma["controls"] = make_pair(1, 8); ma["convince"] = make_pair(3, 5); ma["cooperate"] = make_pair(1, 3); ma["counter"] = make_pair(1, 4); ma["countertrade"] = make_pair(3, 7); ma["cross"] = make_pair(3, 3); ma["curbs"] = make_pair(3, 6); ma["currencies"] = make_pair(1, 24); ma["customer"] = make_pair(1, 3); ma["date"] = make_pair(3, 3); ma["deal"] = make_pair(3, 3); ma["dealers"] = make_pair(1, 16); ma["deals"] = make_pair(3, 4); ma["defined"] = make_pair(2, 9); ma["defuse"] = make_pair(3, 3); ma["developed"] = make_pair(3, 3); ma["difficult"] = make_pair(3, 5); ma["dinar"] = make_pair(1, 3); ma["directive"] = make_pair(2, 3); ma["discuss"] = make_pair(3, 3); ma["dispute"] = make_pair(3, 24); ma["doing"] = make_pair(3, 4); ma["dollars"] = make_pair(1, 8); ma["downwards"] = make_pair(2, 3); ma["dumping"] = make_pair(3, 16); ma["duties"] = make_pair(3, 6); ma["eased"] = make_pair(1, 3); ma["efforts"] = make_pair(3, 7); ma["elected"] = make_pair(1, 3); ma["elections"] = make_pair(3, 3); ma["electronic"] = make_pair(3, 14); ma["electronics"] = make_pair(3, 8); ma["emphasis"] = make_pair(3, 3); ma["ensure"] = make_pair(3, 4); ma["estimate"] = make_pair(1, 7); ma["exchequer"] = make_pair(1, 3); ma["excluding"] = make_pair(2, 6); ma["expanding"] = make_pair(3, 5); ma["expectations"] = make_pair(2, 3); ma["export"] = make_pair(3, 10); ma["expressed"] = make_pair(3, 3); ma["extended"] = make_pair(2, 4); ma["fallen"] = make_pair(1, 3); ma["fear"] = make_pair(3, 3); ma["figure"] = make_pair(2, 5); ma["final"] = make_pair(3, 6); ma["fire"] = make_pair(3, 3); ma["float"] = make_pair(2, 6); ma["focus"] = make_pair(3, 5); ma["forced"] = make_pair(3, 4); ma["foster"] = make_pair(1, 5); ma["foundation"] = make_pair(3, 3); ma["friction"] = make_pair(3, 4); ma["fund"] = make_pair(1, 4); ma["futures"] = make_pair(1, 4); ma["gold"] = make_pair(1, 5); ma["goods"] = make_pair(3, 33); ma["group"] = make_pair(3, 3); ma["guilders"] = make_pair(2, 3); ma["hand"] = make_pair(1, 4); ma["hard"] = make_pair(3, 5); ma["hearing"] = make_pair(3, 5); ma["heated"] = make_pair(3, 4); ma["hedging"] = make_pair(1, 6); ma["hope"] = make_pair(3, 6); ma["hoped"] = make_pair(3, 3); ma["house"] = make_pair(1, 3); ma["how"] = make_pair(3, 3); ma["huge"] = make_pair(3, 8); ma["implement"] = make_pair(3, 3); ma["import"] = make_pair(3, 7); ma["important"] = make_pair(3, 3); ma["impose"] = make_pair(3, 15); ma["imposed"] = make_pair(3, 4); ma["imposing"] = make_pair(3, 3); ma["include"] = make_pair(3, 10); ma["index"] = make_pair(1, 3); ma["industrialized"] = make_pair(1, 3); ma["industries"] = make_pair(3, 4); ma["intend"] = make_pair(1, 3); ma["interbank"] = make_pair(1, 3); ma["interests"] = make_pair(3, 3); ma["investigation"] = make_pair(3, 4); ma["irrational"] = make_pair(3, 3); ma["island"] = make_pair(1, 3); ma["issue"] = make_pair(3, 11); ma["join"] = make_pair(3, 4); ma["joint"] = make_pair(3, 9); ma["keep"] = make_pair(3, 3); ma["kind"] = make_pair(3, 3); ma["leaders"] = make_pair(3, 5); ma["legislation"] = make_pair(3, 8); ma["liabilities"] = make_pair(2, 3); ma["licences"] = make_pair(3, 4); ma["list"] = make_pair(3, 4); ma["live"] = make_pair(3, 10); ma["look"] = make_pair(3, 4); ma["loss"] = make_pair(3, 3); ma["makers"] = make_pair(3, 12); ma["manufacturers"] = make_pair(3, 3); ma["maturing"] = make_pair(1, 3); ma["memory"] = make_pair(3, 4); ma["microchip"] = make_pair(3, 7); ma["microchips"] = make_pair(3, 11); ma["minimum"] = make_pair(1, 3); ma["minus"] = make_pair(2, 3); ma["minutes"] = make_pair(2, 4); ma["missiles"] = make_pair(3, 3); ma["morning"] = make_pair(1, 7); ma["moves"] = make_pair(3, 4); ma["narrowly"] = make_pair(2, 4); ma["newly"] = make_pair(1, 3); ma["normally"] = make_pair(1, 3); ma["offers"] = make_pair(1, 3); ma["offsetting"] = make_pair(1, 3); ma["opened"] = make_pair(3, 4); ma["opposition"] = make_pair(3, 4); ma["output"] = make_pair(3, 8); ma["outstanding"] = make_pair(2, 9); ma["package"] = make_pair(3, 4); ma["packages"] = make_pair(3, 4); ma["parliament"] = make_pair(3, 3); ma["parties"] = make_pair(3, 5); ma["parts"] = make_pair(3, 6); ma["passed"] = make_pair(3, 5); ma["per"] = make_pair(3, 5); ma["persuade"] = make_pair(3, 5); ma["pesos"] = make_pair(2, 9); ma["planned"] = make_pair(3, 4); ma["pledged"] = make_pair(3, 4); ma["plus"] = make_pair(2, 19); ma["polled"] = make_pair(2, 4); ma["power"] = make_pair(3, 4); ma["practices"] = make_pair(3, 6); ma["predicted"] = make_pair(3, 3); ma["pressing"] = make_pair(1, 3); ma["products"] = make_pair(3, 17); ma["programme"] = make_pair(1, 3); ma["projections"] = make_pair(2, 4); ma["proposal"] = make_pair(3, 3); ma["propose"] = make_pair(3, 7); ma["provided"] = make_pair(1, 9); ma["provisional"] = make_pair(2, 22); ma["purchased"] = make_pair(1, 4); ma["quickly"] = make_pair(3, 4); ma["ranges"] = make_pair(1, 3); ma["react"] = make_pair(3, 4); ma["ready"] = make_pair(3, 3); ma["recommendation"] = make_pair(3, 3); ma["recommendations"] = make_pair(3, 5); ma["reform"] = make_pair(3, 3); ma["regret"] = make_pair(3, 3); ma["relations"] = make_pair(3, 3); ma["relationship"] = make_pair(3, 4); ma["replied"] = make_pair(3, 4); ma["report"] = make_pair(3, 10); ma["request"] = make_pair(3, 3); ma["resist"] = make_pair(3, 3); ma["resolve"] = make_pair(3, 4); ma["restraint"] = make_pair(2, 4); ma["restrictions"] = make_pair(3, 3); ma["retaliate"] = make_pair(3, 8); ma["retaliation"] = make_pair(3, 19); ma["retaliatory"] = make_pair(3, 8); ma["round"] = make_pair(1, 4); ma["row"] = make_pair(3, 7); ma["ruling"] = make_pair(3, 3); ma["run"] = make_pair(3, 3); ma["sanctions"] = make_pair(3, 26); ma["says"] = make_pair(1, 7); ma["seasonal"] = make_pair(2, 3); ma["secretary"] = make_pair(3, 3); ma["see"] = make_pair(3, 4); ma["sell"] = make_pair(3, 4); ma["semiconductor"] = make_pair(3, 33); ma["semiconductors"] = make_pair(3, 23); ma["send"] = make_pair(3, 5); ma["separate"] = make_pair(3, 5); ma["serious"] = make_pair(3, 3); ma["serve"] = make_pair(1, 6); ma["services"] = make_pair(3, 9); ma["sessions"] = make_pair(1, 3); ma["settle"] = make_pair(3, 6); ma["settlement"] = make_pair(3, 6); ma["she"] = make_pair(3, 8); ma["shipments"] = make_pair(3, 3); ma["sight"] = make_pair(2, 5); ma["signed"] = make_pair(3, 10); ma["significantly"] = make_pair(3, 3); ma["slash"] = make_pair(3, 3); ma["smallest"] = make_pair(2, 3); ma["something"] = make_pair(3, 4); ma["soon"] = make_pair(3, 5); ma["source"] = make_pair(3, 3); ma["specific"] = make_pair(3, 5); ma["speech"] = make_pair(3, 3); ma["spokeswoman"] = make_pair(3, 3); ma["stability"] = make_pair(1, 6); ma["steady"] = make_pair(2, 3); ma["steep"] = make_pair(1, 3); ma["step"] = make_pair(3, 4); ma["steps"] = make_pair(3, 3); ma["stop"] = make_pair(3, 11); ma["stopped"] = make_pair(3, 5); ma["strengthen"] = make_pair(1, 3); ma["studying"] = make_pair(3, 3); ma["success"] = make_pair(3, 4); ma["summer"] = make_pair(3, 3); ma["sure"] = make_pair(3, 6); ma["survey"] = make_pair(1, 3); ma["technology"] = make_pair(3, 5); ma["television"] = make_pair(3, 3); ma["tell"] = make_pair(3, 3); ma["test"] = make_pair(3, 4); ma["textile"] = make_pair(3, 6); ma["textiles"] = make_pair(3, 3); ma["thus"] = make_pair(2, 3); ma["tied"] = make_pair(1, 3); ma["totalled"] = make_pair(2, 6); ma["tough"] = make_pair(3, 3); ma["trader"] = make_pair(1, 3); ma["transactions"] = make_pair(1, 6); ma["tried"] = make_pair(3, 3); ma["trip"] = make_pair(3, 8); ma["try"] = make_pair(3, 11); ma["trying"] = make_pair(3, 4); ma["unadjusted"] = make_pair(2, 5); ma["unchanged"] = make_pair(2, 6); ma["underlying"] = make_pair(1, 4); ma["unfair"] = make_pair(3, 7); ma["urge"] = make_pair(3, 3); ma["violating"] = make_pair(3, 3); ma["visit"] = make_pair(3, 14); ma["volume"] = make_pair(3, 3); ma["voted"] = make_pair(2, 3); ma["ward"] = make_pair(3, 3); ma["warned"] = make_pair(3, 3); ma["warning"] = make_pair(3, 3); ma["weakened"] = make_pair(3, 3); ma["willing"] = make_pair(3, 4); ma["yield"] = make_pair(1, 3); ma["yields"] = make_pair(1, 3); string s; int a[4]; memset(a, 0, sizeof a); while (cin >> s) { if (ma.find(s) != ma.end()) { a[ma[s].first] += ma[s].second; } } int m = max(max(a[1], a[2]), a[3]); if (a[1] == m) { cout << 1 << endl; } else { if (a[2] == m) { cout << 2 << endl; } else { cout << 3 << endl; } } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int AX = 1e5 + 666; int n; long long a[AX]; long long b[AX]; int mark[AX]; long long x, y; int main() { scanf("%d%I64d%I64d", &n, &x, &y); long long res = 0LL; for (int i = 0; i < n; i++) { scanf("%I64d%I64d", &a[i], &b[i]); res = (res + x % MOD + 1LL * y * (b[i] - a[i])) % MOD; } memset(mark, 0, sizeof(mark)); sort(a, a + n); sort(b, b + n); for (int i = n - 1; i >= 0; i--) { int pos = upper_bound(a, a + n, b[i]) - a; if (pos < 0 || pos >= n || (1LL * y * (a[pos] - b[i]) > x)) { continue; } while (pos <= n && mark[pos] && (1LL * y * (a[pos] - b[i]) < x)) pos++; if (pos >= n) continue; if (1LL * y * (a[pos] - b[i]) < x) { mark[pos] = 1; res = (res + MOD - x + 1LL * y * (a[pos] - b[i])) % MOD; } } printf("%I64d\n", res); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> people(n); for (int i = 0; i < n; i++) { scanf("%d", &people[i]); } int mincost = 999999999; for (int x = 0; x < n; x++) { int cost = 0; for (int i = 0; i < n; i++) { cost += 2 * people[i] * (abs(x - i) + i + x); } mincost = min(cost, mincost); } printf("%d\n", mincost); return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; map<int, int> mp; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; } sort(a, a + n); if (mp.size() == 1) { cout << 0; } else if (mp.size() == 2) { int diff = a[n - 1] - a[0]; if (diff & 1) cout << a[n - 1] - a[0]; else cout << diff / 2; } else if (mp.size() == 3) { int diff = a[n - 1] - a[0]; if (diff & 1) { cout << -1; return 0; } int mid = (a[n - 1] + a[0]) / 2; bool flag = false; for (int i = 0; i < n; i++) { if (a[i] == a[0] || a[i] == a[n - 1] || a[i] == mid) continue; else { flag = true; break; } } if (flag) cout << "-1"; else cout << diff / 2; } else cout << -1; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, x, k; cin >> n >> k >> x; vector<vector<long long> > dp(5001, vector<long long>(5001, -1)); vector<long long> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= k; i++) { dp[i][1] = a[i]; } for (int j = 2; j <= x; j++) { stack<pair<long long, long long> > s1, s2; for (int i = 1; i <= n; i++) { long long mx = -1; if (s1.size()) { mx = max(mx, s1.top().second); } if (s2.size()) { mx = max(mx, s2.top().second); } if (mx != -1) { dp[i][j] = mx + a[i]; } if (s1.size() + s2.size() < k) { if (s1.size()) { s1.push({dp[i][j - 1], max(s1.top().second, dp[i][j - 1])}); } else { s1.push({dp[i][j - 1], dp[i][j - 1]}); } } else { if (!s2.size()) { mx = -1; while (s1.size()) { s2.push({s1.top().first, max(mx, s1.top().first)}); mx = s2.top().second; s1.pop(); } } if (s1.size()) { s1.push({dp[i][j - 1], max(dp[i][j - 1], s1.top().second)}); } else { s1.push({dp[i][j - 1], dp[i][j - 1]}); } s2.pop(); } } } long long ans = -1; for (int i = n - k + 1; i <= n; i++) { ans = max(ans, dp[i][x]); } cout << ans; return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-10; int a[1010], n, ans = -1; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); for (int i = 0; i < n; i++) for (int j = i + 1; j < n - 1; j++) { int c = a[i] - 1, t = 0, x = 0, y = 0; for (int k = i + 1; k < j + 1; k++) x += c / a[k], t += c / a[k] * a[k], c %= a[k]; c = t + a[j]; for (int k = 0; k < n; k++) y += c / a[k], c %= a[k]; if (x + 1 < y) ans == -1 ? ans = t + a[j] : ans = min(ans, t + a[j]); } printf("%d\n", ans); return 0; }
2,600
CPP
for x in range(int(input())): n=int(input()) s=input() no=0 count=0 for x in s: if x=='(': no+=1 else: if no>0: no-=1 else: count+=1 print(count)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int M = 2e5 + 5; int q, n, l, r; char x, t; map<int, int> book; int main(void) { ios::sync_with_stdio(false); cin.tie(0); cin >> q; l = r = M; for (int k = 1; k <= q; k++) { cin >> t >> n; if (k == 1) { if (t == 'L') book[n] = l--, r++; else book[n] = r++, l--; } else if (t == 'L') book[n] = l--; else if (t == 'R') book[n] = r++; else cout << min(book[n] - l - 1, r - book[n] - 1) << '\n'; } return 0; }
1,400
CPP
# The idea for this particular solution was taken #from another user to test time execution on the server #get n-number of problems from the user n = int(input()) cnt = 0 #for each problem decide if you will implement it: for i in range(n): s = sum(map(int, input().split())) #p,v,t = map(int, input().split()) #temp = p + v + t if s > 1: cnt += 1 #if temp >= 2: # sum += 1 print(cnt)
800
PYTHON3
mod = 1000000007 ii = lambda : int(input()) si = lambda : input() dgl = lambda : list(map(int, input())) f = lambda : map(int, input().split()) il = lambda : list(map(int, input().split())) ls = lambda : list(input()) n,k=f() s=ls() if n==1 and s[0]==0: print(0) exit() elif n==1 and k>=1: print(0) exit() elif n==1 and k==0: print(s[0]) exit() for i in range(n): if k==0: break if i==0: if s[i]!='1': s[i]='1' k-=1 else: if s[i]!='0': s[i]='0' k-=1 print(''.join(s))
1,000
PYTHON3
for _ in range(int(input())): S = input() Alice = 1 Score = 0 while(S.count("1")!=0): N = len(S) start = False time = 0 Index = -1 Max = 0 for i in range(N): if(S[i]=="1"): if(start): time+=1 else: time+=1 start = True else: if(start): start = False if(time>Max): Index = i-time Max = time time = 0 else: continue if(time>Max): Index = N-time Max = time if(Alice): Score += Max S = S[:Index] + S[Index + Max+1:] Alice = 1 - Alice print(Score)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, l[4096], p2 = 1, ans; int dfs(int v) { if (v >= p2) return l[v]; int x = dfs(2 * v), y = dfs(2 * v + 1); ans += abs(x - y); return max(x, y) + l[v]; } int main() { cin >> n; for (int i = 0; i < n; i++) p2 *= 2; for (int i = 2; i < p2 * 2; i++) cin >> l[i]; int h = dfs(1); cout << ans; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k; int x; scanf("%d", &x); if (x == 3) printf("5\n"); else { x *= 2; x--; int y = sqrt(x); if (y * y < x) y++; if (y % 2 == 0) y++; printf("%d\n", y); } return 0; }
1,700
CPP
t = int(input()) while t > 0: t -= 1 n, a, b = map(int, input().split()) kol = 0 s = ''; mp = {} for i in range(26): x = chr(i + 97) mp[x] = 0 for i in range(1, n + 1): if i <= a: if kol != b: s += chr(kol + 97) mp[chr(kol + 97)] += 1 kol += 1 else: s += 'a' mp['a'] += 1 else: j = i - a - 1 s += s[j] print(s)
900
PYTHON3
#include <bits/stdc++.h> using ll = long long; using ull = __int128; class FenwickTree { private: std::vector<ll> aib; int n; public: FenwickTree(int n_) { n = n_; aib.resize(1 + n); } void update(int pos, ll val) { aib[0] += val; for (int x = pos; x <= n; x += (x ^ (x & (x - 1)))) aib[x] += val; } ll query(int pos) { ll result = 0; for (int x = pos; 0 < x; x = (x & (x - 1))) result += aib[x]; return result; } void update2(int x, int y, int val) { update(x, val); update(y + 1, -val); } ll query2(ll x, ll y) { return query(y) - query(x - 1); } void print() { std::cout << "Print\n"; for (int i = 1; i <= n; i++) std::cout << query(i) << " "; std::cout << '\n'; } }; int const nmax = 600000; int const sigma = 26; char v[1 + nmax]; std::vector<std::pair<int, int>> st; struct Event { int start; int l; int dist; bool operator<(Event const oth) const { return start < oth.start; } }; void printlong(ull number) { std::string s; if (number == 0) std::cout << 0 << '\n'; while (0 < number) { s = s + (char)((number % 10) + '0'); number /= 10; } std::reverse(s.begin(), s.end()); std::cout << s << '\n'; } int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); int n; std::cin >> n; FenwickTree points(n), colors(n); std::vector<Event> events, newevents; int smin = -1; ull result = 0; ll resultmod = 0; ll sol = 0; for (int i = 1; i <= n; i++) { char ch; ll val; std::cin >> ch >> val; ll key = (result & ((1LL << 30) - 1)); ch = ((ch - 'a') + resultmod) % sigma + 'a'; val ^= key; v[i] = ch; if (i == 1) { st.push_back({i, val}); colors.update2(i, i, val); result += val; resultmod = (resultmod + val) % sigma; smin = val; } else { smin = std::min(1LL * smin, 1LL * val); result += smin; resultmod = (resultmod + smin) % sigma; int last = i; colors.update2(last, last, val); sol += 1LL * val * points.query2(last, last); while (0 < st.size() && val < st.back().second) { colors.update2(st.back().first, last - 1, val - st.back().second); sol += 1LL * (val - st.back().second) * points.query2(st.back().first, last - 1); last = st.back().first; st.pop_back(); } st.push_back({last, val}); for (int j = 0; j < events.size(); j++) { Event e = events[j]; if (v[(i - e.start) + 1] == v[i]) newevents.push_back(e); else { int pos = e.start; for (int h = 0; h < e.l; h++) { sol -= colors.query(pos); points.update(pos, -1); pos += e.dist; } } } if (v[1] == v[i]) { newevents.push_back({i, 1, 1}); points.update(i, 1); sol += colors.query(i); } events.clear(); for (int j = 0; j < newevents.size(); j++) { Event e = newevents[j]; if (0 == events.size()) { events.push_back({e.start, 1, nmax}); if (1 < e.l) events.push_back({e.start + e.dist, e.l - 1, e.dist}); } else { int last = events.back().start + (events.back().l - 1) * events.back().dist; if ((e.start - last) == e.dist) { if (events.back().dist == e.dist) events.back().l += e.l; else { events.push_back(e); } } else { events.push_back({e.start, 1, e.start - last}); if (1 < e.l) events.push_back({e.start + e.dist, e.l - 1, e.dist}); } } } newevents.clear(); } result += sol; resultmod = (resultmod + sol) % sigma; printlong(result); } return 0; }
3,200
CPP
def f(m): for i in range(0,len(m)): if m[i]!='4' and m[i]!='7': return False return True n=int(input()) cond=False for i in range (4,n+1): if f(str(i)) and n%i==0: cond=True break elif i==n: break if cond: print("YES") else: print("NO")
1,000
PYTHON3
t = int(input()) for _ in range(t): n, k = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] arrS = set(arr) if len(arrS) > k: print (-1) else: req = list(arrS) the = -1 for i in range(1, n + 1): if i not in req: the = i break for _ in range(len(req), k): req.append(the) req.sort() newArr = req * n print(len(newArr)) print(' '.join([str(x) for x in newArr]))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; bool b[200000][26]; string mrg(string a, string b) { int fi = -1; for (int j = 0; j < a.length(); j++) { int l = min(b.length(), a.length() - j); if (a.substr(j, l) == b.substr(0, l)) { fi = j; break; } } if (fi == -1) { swap(a, b); for (int j = 0; j < a.length(); j++) { int l = min(b.length(), a.length() - j); if (a.substr(j, l) == b.substr(0, l)) { fi = j; break; } } } if (fi == -1) { cout << "NO" << endl; exit(0); } if (fi + b.length() <= a.length()) return a; return a.substr(0, fi) + b; } int main() { cin.sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s[2 * n]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) { if (s[i].length() > 26) { cout << "NO" << endl; return 0; } for (int x = 0; x < s[i].length(); x++) { for (int j = x + 1; j < s[i].length(); j++) { if (s[i][x] == s[i][j]) { cout << "NO" << endl; return 0; } } } for (char c : s[i]) b[i][c - 'a'] = 1; } set<int> q; int iter = n; for (int i = 0; i < n; i++) { string tp = s[i]; set<int> p = q; for (int j : p) { for (char c : s[i]) { if (b[j][c - 'a']) { tp = mrg(tp, s[j]); q.erase(q.find(j)); break; } } } if (tp != s[i]) { s[iter] = tp; if (s[iter].length() > 26) { cout << "NO" << endl; return 0; } for (int x = 0; x < s[iter].length(); x++) { for (int j = x + 1; j < s[iter].length(); j++) { if (s[iter][x] == s[iter][j]) { cout << "NO" << endl; return 0; } } } for (char c : s[iter]) b[iter][c - 'a'] = 1; q.insert(iter); iter++; } else { q.insert(i); } } string cp[q.size()]; int qo = 0; for (int i : q) cp[qo++] = s[i]; int sz = q.size(); for (int i = 0; i < sz; i++) { s[i] = cp[i]; } sort(s, s + sz); string ans = ""; for (int i = 0; i < sz; i++) { int fi = -1; for (int j = 0; j < ans.length(); j++) { int l = min(s[i].length(), ans.length() - j); if (ans.substr(j, l) == s[i].substr(0, l)) { fi = j; break; } } if (fi == -1) ans += s[i]; else { if (fi + s[i].length() <= ans.length()) continue; else ans = ans.substr(0, fi) + s[i]; } if (ans.length() > 26) break; } cout << ans << endl; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; long long sum = 0, res = 0; for (int i = 1; i <= k; i++) { int a, b; cin >> a >> b; sum += b; res += 1LL * a * b; } if (sum > n) { cout << -1; } else if (sum < n) { cout << 1; } else { if (res % n == 1LL * n * (n + 1) / 2 % n) { cout << 1; } else { cout << -1; } } return 0; }
2,700
CPP
n = int(input()) lst = [int(x) for x in input().split()] ct = 0 mi = lst[0] mx = lst[0] for x in lst[1:]: if x > mx or x < mi: ct += 1 if x > mx: mx = x if x < mi: mi = x print (ct)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, cnt = 0, st, en, ans = 0; char s[105][105]; cin >> n >> m; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> s[i][j]; } } for (j = 0; j < m; j++) { if (s[n - 1][j] == 'B') { cnt++; en = j; } else if (s[n - 1][j] == '.') { if (cnt > 0) { ans++; } cnt = 0; } } if (en == m - 1) ans++; cout << ans << endl; }
1,200
CPP
n=int(input()) for i in range(n): letter=input() if len(letter)>10: print(f'{letter[0]}{len(letter)-2}{letter[-1]}') else: print(letter)
800
PYTHON3
n = int(input()) a = dict() for i in range(n): x = int(input()) if x in a: a[x] += 1 else: a[x] = 1 c, d = False, False for key in a: for k in a: if a[key] == a[k] and a[key] + a[k] == n and key != k: c, d = key, k if c and d: print("YES") print(c, d, sep=" ") else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 1e9 + 9; const int MAXN = 1e5 + 5; const int BASE = uniform_int_distribution<int>(MAXN, MOD)(rng); vector<int> BPOW; inline void init_bpow() { BPOW.resize(MAXN, 1); for (int i = 1; i < MAXN; i++) BPOW[i] = BPOW[i - 1] * 1LL * BASE % MOD; } namespace Treap { const int POOL_SZ = 8e6; const int EMPTY = 0; struct node { int val, hash; unsigned prio; int lt, rt, sz; int used = 0; } P[POOL_SZ]; int ptr = 1, used_cntr = 1; inline int newnode(int val) { while (ptr < POOL_SZ && P[ptr].used == used_cntr) ptr++; assert(ptr < POOL_SZ); P[ptr].hash = P[ptr].val = val; P[ptr].prio = rng(); P[ptr].sz = 1; P[ptr].lt = P[ptr].rt = EMPTY; return ptr++; } inline int copynode(int i) { while (ptr < POOL_SZ && P[ptr].used == used_cntr) ptr++; assert(ptr < POOL_SZ); P[ptr] = P[i]; return ptr++; } inline int getsz(int i) { return i ? P[i].sz : 0; } inline int gethash(int i) { return i ? P[i].hash : 0; } inline void update(int i) { if (!i) return; int lsz = getsz(P[i].lt); P[i].sz = lsz + 1 + getsz(P[i].rt); P[i].hash = (gethash(P[i].lt) + P[i].val * 1LL * BPOW[lsz] + gethash(P[i].rt) * 1LL * BPOW[lsz + 1]) % MOD; } void merge(int lt, int rt, int& i) { if (!lt) i = rt; else if (!rt) i = lt; else if (P[lt].prio > P[rt].prio) { i = copynode(lt); merge(P[i].rt, rt, P[i].rt); } else { i = copynode(rt); merge(lt, P[i].lt, P[i].lt); } update(i); } void split(int i, int pos, int& lt, int& rt) { if (!i) { lt = rt = EMPTY; return; } int lsz = getsz(P[i].lt); i = copynode(i); if (pos <= lsz) split(P[i].lt, pos, lt, P[i].lt), rt = i; else split(P[i].rt, pos - lsz - 1, P[i].rt, rt), lt = i; update(i); } int prefix_hash(int i, int r) { if (!i) return 0; if (r >= P[i].sz) return P[i].hash; int res = prefix_hash(P[i].lt, r); int lsz = getsz(P[i].lt); if (r > lsz) { res = (res + P[i].val * 1LL * BPOW[lsz] + prefix_hash(P[i].rt, r - lsz - 1) * 1LL * BPOW[lsz + 1]) % MOD; } return res; } bool almost_full() { return ptr + MAXN / 100 > POOL_SZ; } void reset() { used_cntr++; ptr = 1; } void mark_used(int i) { if (!i || P[i].used == used_cntr) return; P[i].used = used_cntr; mark_used(P[i].lt); mark_used(P[i].rt); } } // namespace Treap struct SegTree { struct node { int lt, rt; bool bad; node() : lt(Treap::EMPTY), rt(Treap::EMPTY), bad(false) {} node(int lt_, int rt_, int bad_) { lt = lt_; rt = rt_; bad = bad_; } }; int n; vector<node> tree; SegTree(int n_) { n = 1; while (n < n_) n *= 2; tree.resize(n * 2); } node merge(node& lt, node& rt) { if (lt.bad || rt.bad) return node(Treap::EMPTY, Treap::EMPTY, true); int lrsz = Treap::getsz(lt.rt); int rlsz = Treap::getsz(rt.lt); int mn = min(lrsz, rlsz); int lrhash = Treap::prefix_hash(lt.rt, mn); int rlhash = Treap::prefix_hash(rt.lt, mn); if (lrhash != rlhash) return node(Treap::EMPTY, Treap::EMPTY, true); node new_(lt.lt, rt.rt, false); if (lrsz < rlsz) { int _, nlr; Treap::split(rt.lt, mn, _, nlr); Treap::merge(new_.lt, nlr, new_.lt); } else { int _, nrl; Treap::split(lt.rt, mn, _, nrl); Treap::merge(new_.rt, nrl, new_.rt); } return new_; } void maybe_free() { if (!Treap::almost_full()) return; Treap::reset(); for (int i = 1; i < 2 * n; i++) { Treap::mark_used(tree[i].lt); Treap::mark_used(tree[i].rt); } } void update(int i, int v) { maybe_free(); tree[i + n] = v > 0 ? node(Treap::EMPTY, Treap::newnode(v), false) : node(Treap::newnode(-v), Treap::EMPTY, false); for (i = (i + n) / 2; i > 0; i /= 2) tree[i] = merge(tree[i * 2], tree[i * 2 + 1]); } bool query(int i, int j) { maybe_free(); node lt, rt; for (i += n, j += n; i <= j; i = (i + 1) / 2, j = (j - 1) / 2) { if (i % 2) lt = merge(lt, tree[i]); if (~j % 2) rt = merge(tree[j], rt); } node res = merge(lt, rt); return !res.bad && !Treap::getsz(res.lt) && !Treap::getsz(res.rt); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); init_bpow(); int n, k; cin >> n >> k; vector<int> a(n); for (int& ai : a) cin >> ai; SegTree st(n); for (int i = 0; i < n; i++) st.update(i, a[i]); int q; cin >> q; while (q--) { int typ; cin >> typ; if (typ == 1) { int i, t; cin >> i >> t; st.update(i - 1, t); } else { int l, r; cin >> l >> r; bool ok = st.query(l - 1, r - 1); cout << (ok ? "Yes" : "No") << endl; } } }
3,300
CPP
p,q = map(int,input().split()) base = 1.000000011 print(p*base**q)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); auto cmp = [](const pair<long long, long long> &p1, const pair<long long, long long> &p2) { return p1.second > p2.second; }; long long n, k, x; scanf("%lld %lld %lld", &n, &k, &x); long long neg = 0; vector<pair<long long, long long> > numbers(n); vector<bool> signs(n); for (long long i = 0; i < n; i++) { long long d; scanf("%lld", &d); if (d < 0) ++neg; numbers[i] = make_pair(i, abs(d)); signs[i] = (d >= 0); } bool increase = (neg % 2 != 0); make_heap(numbers.begin(), numbers.end(), cmp); while (k--) { pair<long long, long long> smallest = numbers.front(); if (increase) { smallest.second += x; } else { smallest.second -= x; if (smallest.second <= 0) { increase = true; smallest.second *= -1; signs[smallest.first] = !signs[smallest.first]; } } pop_heap(numbers.begin(), numbers.end(), cmp); numbers.pop_back(); numbers.push_back(smallest); push_heap(numbers.begin(), numbers.end(), cmp); } sort(numbers.begin(), numbers.end(), [](const pair<long long, long long> &p1, const pair<long long, long long> &p2) { return p1.first <= p2.first; }); for (long long i = 0; i < n; i++) { printf("%lld ", signs[i] ? numbers[i].second : -numbers[i].second); } printf("\n"); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; void file() {} void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } const int N = (1e5 * 2) + 20; vector<int> cmpnt; vector<vector<int>> gr(N); int vis[N]; void dfs(int node) { if (vis[node]) { return; } vis[node] = 1; cmpnt.push_back(node); for (int i = 0; i < gr[node].size(); i++) { int nxt = gr[node][i]; dfs(nxt); } } int main() { file(); fast(); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; gr[u].push_back(v); gr[v].push_back(u); } int cnt = 0; vector<vector<int>> cmpontnets; vector<int> vis1(n + 10); for (int i = 1; i <= n; i++) { if (!vis[i]) { cmpnt.clear(); dfs(i); cmpontnets.push_back(cmpnt); int sz = cmpnt.size(); if (sz == 1) { continue; } int cur = cmpnt[0]; int prev = -1; while (sz--) { if (gr[cur].size() > 2 || gr[cur].size() == 1) { sz = 2; break; } int nxt = gr[cur][0]; if (nxt == prev) { nxt = gr[cur][1]; } if (vis1[nxt]) { sz = 2; break; } vis1[nxt] = 1; prev = cur; cur = nxt; } if (sz == 2) { continue; } cnt++; } } cout << cnt << endl; }
1,500
CPP
n = int(input()) b = input().split() b = list(map(int, b)) sum = 0 for i in range(n): b[i] = b[i] + sum sum = max(b[i], sum) print(" ".join(map(str, b)))
900
PYTHON3
a,b,c,d = input().strip().split(" ") p1 = int(a)/int(b) p2 = int(c)/int(d) p = (1-p1)*(1-p2) ans = p1/(1-p) print(ans)
1,300
PYTHON3
st = input() st = st.split(' ') k = int(st[0]) n = int(st[1]) w = int(st[2]) for i in range(1,w+1): n = n - (k * i) if n < 0: print(n * -1) else: print(0)
800
PYTHON3
arr = [] for i in range (5): arr.append([int(x) for x in input().split()]) #print(arr) #print(arr.index(1)) '''for i in range(len(arr)): for j in range(len(arr)): a = arr[i][j] ''' val = 1 a = ([(abs((index+1)-3) + abs((row.index(val)+1)-3)) for index, row in enumerate(arr) if val in row]) b = str(a) print(b[1])
800
PYTHON3
from fractions import Fraction import sys sys.setrecursionlimit(1000*100) #c=int(input()) #a,b=tuple(map(int,input().split())) #edges=dict((i,[]) for i in range(1,c+1)) #children=filter(lambda x: x != p, edges[r]) #cs.sort(key=lambda x:Fraction(x[0],x[1]),reverse=True) #if dp[r] is not None: #chr(ord('a')+1) #''.join(['a','b','c']) #sys.exit() n,m,a,b,c=tuple(map(int,input().split())) err='IMPOSSIBLE' if n%2==1 and m%2==1: print(err) sys.exit() ret=[['.' for _ in range(m)] for _ in range(n)] def col(i,j): return chr(ord('a')+4*(i%4)+j%4) if n%2==1: for i in range(0,m,2): if a==0: print(err) sys.exit() ret[n-1][i]=ret[n-1][i+1]=col(n-1,i) a-=1 n-=1 if m%2==1: for i in range(0,n,2): if b==0: print(err) sys.exit() ret[i][m-1]=ret[i+1][m-1]=col(i,m-1) b-=1 m-=1 for i in range(0,n,2): for j in range(0,m,2): if a>=2: a-=2 ret[i][j]=ret[i][j+1]=col(i,j) ret[i+1][j]=ret[i+1][j+1]=col(i+1,j) elif b>=2: b-=2 ret[i][j]=ret[i+1][j]=col(i,j) ret[i][j+1]=ret[i+1][j+1]=col(i,j+1) elif c>=1: c-=1 ret[i][j]=ret[i+1][j]=ret[i][j+1]=ret[i+1][j+1]=col(i,j) else: print(err) sys.exit() for l in ret: print(''.join(l))
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-9; const long double pi = acos(-1.0); const long long inf = 1e+9; #pragma comment(linker, "/STACK:36777216") template <typename T> istream &operator>>(istream &, vector<T> &); template <typename T> ostream &operator<<(ostream &, const vector<T> &); vector<string> s; int g[126][126] = {}; void go(int l, int r, int t) { vector<char> q; int last = l; bool fl = 0; for (int i = l; i <= r; i++) { q.push_back(s[i][t]); if (s[i][t] != '#') fl = 1; if (s[i][t] == '#' && fl) { cout << "Impossible" << endl; exit(0); } if (s[i][t] != s[last][t]) { go(last, i - 1, t + 1); last = i; } } if (last != r) go(last, r, t + 1); for (int i = 0; i < q.size(); i++) for (int k = i + 1; k < q.size(); k++) if (q[i] != q[k]) g[q[i]][q[k]] = 1; } int used[126] = {}; vector<int> order; void dfs(int v) { used[v] = 1; for (int i = 0; i < 126; i++) if (g[v][i]) { if (used[i] == 0) dfs(i); if (used[i] == 1) { cout << "Impossible" << endl; exit(0); } } used[v] = 2; order.push_back(v); } int main() { int n; cin >> n; s.resize(n); for (int i = 0; i < n; i++) { cin >> s[i]; while (s[i].size() < 105) s[i].push_back('#'); } go(0, n - 1, 0); for (int i = 0; i < 126; i++) if (!used[i]) dfs(i); reverse(order.begin(), order.end()); bool fl = 0; string ans; for (int i = 0; i < order.size(); i++) { if ('a' <= order[i] && order[i] <= 'z') ans.push_back(char(order[i])); } cout << ans << endl; return 0; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (int i = 0; i < v.size(); ++i) is >> v[i]; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); ++i) os << v[i] << " "; return os; }
1,600
CPP
n =int(input()) li = list(map(int,input().split())) cnt=[0]*101 mx=0 for i in li: cnt[i]=cnt[i]+1 if cnt[i]>mx: mx=cnt[i] print(mx)
800
PYTHON3
v = int(input()) if v>2 and v % 2==0: print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); string a; cin >> a; int j = -1; string s; for (int i = 0; i < a.size(); ++i) { if (a[i] == '-') continue; if (a[i] == '.') { j = i + 1; break; } s += a[i]; } deque<char> d; int kol = 1; for (int i = s.size() - 1; i >= 0; --i) { if (kol > 3) { d.push_front(','); kol = 1; } d.push_front(s[i]); ++kol; } d.push_front('$'); if (j == -1) { d.push_back('.'); d.push_back('0'); d.push_back('0'); } else if (j + 2 > a.size()) { d.push_back('.'); d.push_back(a.back()); d.push_back('0'); } else { d.push_back('.'); d.push_back(a[j]); d.push_back(a[j + 1]); } if (a[0] == '-') { d.push_front('('); d.push_back(')'); } for (char i : d) cout << i; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; struct node { string l, r; long long tot; } f[205]; int n; long long query(string s) { int l = s.size(); if (l == 1) { if (s[0] == '0') f[0].tot = 1, f[1].tot = 0; else f[0].tot = 0, f[1].tot = 1; } else f[0].tot = f[1].tot = 0; for (int i = 2; i <= n; i++) { f[i].tot = f[i - 1].tot + f[i - 2].tot; string u = f[i - 2].r + f[i - 1].l; int sl = max(0, (int)f[i - 2].r.size() - l + 1); int k, sr = f[i - 2].r.size(); for (int j = sl; j < sr; j++) { for (k = 0; k < l; k++) { if (j + k == u.size()) break; if (u[j + k] != s[k]) break; } f[i].tot += k == l; } if (f[i].tot > 1e18) return f[i].tot; } return f[n].tot; } int cqq(string s) { if (s.size() == 0) return 0; if (f[n].r.size() >= s.size()) { int i; for (i = 1; i <= s.size(); i++) { if (s[s.size() - i] != f[n].r[f[n].r.size() - i]) break; } return i > s.size(); } else return 0; } int main() { int m, i, j; long long k; scanf("%d%lld%d", &n, &k, &m); f[0].l = f[0].r = "0"; f[1].l = f[1].r = "1"; for (i = 2; i <= n; i++) { if (f[i - 1].r.size() < m) { f[i].r = f[i - 2].r + f[i - 1].r; if (f[i].r.size() > m) f[i].r = f[i].r.substr(f[i].r.size() - m, m); } else f[i].r = f[i - 1].r; if (f[i - 2].l.size() < m) { f[i].l = f[i - 2].l + f[i - 1].l; if (f[i].l.size() > m) f[i].l = f[i].l.substr(0, m); } else f[i].l = f[i - 2].l; } string w = ""; for (i = 0; i < m; i++) { int dd = cqq(w); if (dd == k) break; k -= dd; string s = w + "0"; long long cc = query(s); if (cc == 0) { s = w + "1"; if (query(s) == 0) break; } if (cc >= k) w += "0"; else { k -= cc; w += "1"; } } cout << w << endl; return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long int base = 1; long long int sum = (10 * pow(16, s.size() - 1)); for (int i = s.size() - 1; i >= 1; i--) { sum += (s[i] - '0') * base; base *= 16; } cout << sum % 2; }
0
CPP
#include <bits/stdc++.h> int getint() { int x = 0, s = 1; char d = getchar(); while (d < '0' || d > '9') { if (d == '-') s = -1; d = getchar(); } while (d >= '0' && d <= '9') x = x * 10 + d - '0', d = getchar(); return x * s; } using namespace std; int n, m, a[2010 + 2010]; int l1, r1, l2, r2; void solve() { n = getint(), m = getint(); l1 = getint(), r1 = getint(); l2 = getint(), r2 = getint(); if (l2 < l1 || (l1 == l2 && r2 > r1)) { swap(l1, l2); swap(r1, r2); } if (r2 <= r1) { long long now = 1LL * n * (r2 - l2); if (now >= m) cout << 0 << endl; else { m -= now; long long mm = 1LL * (l2 - l1 + r1 - r2) * n; if (mm >= m) cout << m << endl; else cout << mm + (m - mm) * 2 << endl; } } else if (l2 <= r1) { long long now = 1LL * n * (r1 - l2); if (now >= m) cout << 0 << endl; else { m -= now; long long mm = 1LL * (l2 - l1 + r2 - r1) * n; if (mm >= m) cout << m << endl; else cout << mm + (m - mm) * 2 << endl; } } else { long long t = r2 - l1, d = l2 - r1; if (t >= m) cout << d + m << endl; else if (d + t >= 2 * t) cout << 2LL * (m - t) + t + d << endl; else { m -= t; long long ans = d + t; if (m / t >= n - 1) { ans += 1LL * (n - 1) * (d + t); m -= (n - 1) * t; ans += m * 2; } else { ans += m / t * (d + t); m %= t; ans += min(2LL * m, d + m); } cout << ans << endl; } } } int main() { int t = getint(); while (t--) solve(); }
2,100
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> std::ostream &operator<<(std::ostream &out, vector<T> &v) { for (typename vector<T>::size_type i = 0; i < v.size(); ++i) out << v[i] << " "; out << "\n"; return out; } template <typename T> std::ostream &operator<<(std::ostream &out, vector<pair<T, T> > &v) { for (size_t i = 0; i < v.size(); ++i) out << "(" << v[i].first << ", " << v[i].second << ") "; out << "\n"; return out; } template <typename T> std::ostream &operator<<(std::ostream &out, vector<vector<T> > &v) { for (size_t i = 0; i < v.size(); ++i) { for (size_t j = 0; j < v[i].size(); ++j) { out << v[i][j] << " "; } out << "\n"; } return out; } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; if (n == 1) { cout << "1\n"; return 0; } string num = ""; if (n & 1) { num.push_back('7'); n -= 3; } for (int i = 0; i < n / 2; ++i) { num.push_back('1'); } cout << num << "\n"; return 0; }
1,200
CPP
a=int(input()) for i in range(a): b=input().split() if 2*int(b[1])<=int(b[2]): print(int(b[0])*int(b[1])) else: if int(b[0])%2==0: print((int(b[0])//2)*int(b[2])) else: print((int(b[0])//2)*int(b[2])+int(b[1]))
800
PYTHON3
t=int(input()) arr=list(map(int, input().split())) i=t-1 temp=arr[i] while i>=0: if(temp!=arr[i]): print(i+1) break i-=1
800
PYTHON3
#include <bits/stdc++.h> using namespace std; bool isluck(long long n) { while (n) { if (n % 10 != 7 && n % 10 != 4) return false; n /= 10; } return true; } long long lu[] = { 4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777, 4444, 4447, 4474, 4477, 4744, 4747, 4774, 4777, 7444, 7447, 7474, 7477, 7744, 7747, 7774, 7777, 44444, 44447, 44474, 44477, 44744, 44747, 44774, 44777, 47444, 47447, 47474, 47477, 47744, 47747, 47774, 47777, 74444, 74447, 74474, 74477, 74744, 74747, 74774, 74777, 77444, 77447, 77474, 77477, 77744, 77747, 77774, 77777, 444444, 444447, 444474, 444477, 444744, 444747, 444774, 444777, 447444, 447447, 447474, 447477, 447744, 447747, 447774, 447777, 474444, 474447, 474474, 474477, 474744, 474747, 474774, 474777, 477444, 477447, 477474, 477477, 477744, 477747, 477774, 477777, 744444, 744447, 744474, 744477, 744744, 744747, 744774, 744777, 747444, 747447, 747474, 747477, 747744, 747747, 747774, 747777, 774444, 774447, 774474, 774477, 774744, 774747, 774774, 774777, 777444, 777447, 777474, 777477, 777744, 777747, 777774, 777777, 4444444, 4444447, 4444474, 4444477, 4444744, 4444747, 4444774, 4444777, 4447444, 4447447, 4447474, 4447477, 4447744, 4447747, 4447774, 4447777, 4474444, 4474447, 4474474, 4474477, 4474744, 4474747, 4474774, 4474777, 4477444, 4477447, 4477474, 4477477, 4477744, 4477747, 4477774, 4477777, 4744444, 4744447, 4744474, 4744477, 4744744, 4744747, 4744774, 4744777, 4747444, 4747447, 4747474, 4747477, 4747744, 4747747, 4747774, 4747777, 4774444, 4774447, 4774474, 4774477, 4774744, 4774747, 4774774, 4774777, 4777444, 4777447, 4777474, 4777477, 4777744, 4777747, 4777774, 4777777, 7444444, 7444447, 7444474, 7444477, 7444744, 7444747, 7444774, 7444777, 7447444, 7447447, 7447474, 7447477, 7447744, 7447747, 7447774, 7447777, 7474444, 7474447, 7474474, 7474477, 7474744, 7474747, 7474774, 7474777, 7477444, 7477447, 7477474, 7477477, 7477744, 7477747, 7477774, 7477777, 7744444, 7744447, 7744474, 7744477, 7744744, 7744747, 7744774, 7744777, 7747444, 7747447, 7747474, 7747477, 7747744, 7747747, 7747774, 7747777, 7774444, 7774447, 7774474, 7774477, 7774744, 7774747, 7774774, 7774777, 7777444, 7777447, 7777474, 7777477, 7777744, 7777747, 7777774, 7777777, 44444444, 44444447, 44444474, 44444477, 44444744, 44444747, 44444774, 44444777, 44447444, 44447447, 44447474, 44447477, 44447744, 44447747, 44447774, 44447777, 44474444, 44474447, 44474474, 44474477, 44474744, 44474747, 44474774, 44474777, 44477444, 44477447, 44477474, 44477477, 44477744, 44477747, 44477774, 44477777, 44744444, 44744447, 44744474, 44744477, 44744744, 44744747, 44744774, 44744777, 44747444, 44747447, 44747474, 44747477, 44747744, 44747747, 44747774, 44747777, 44774444, 44774447, 44774474, 44774477, 44774744, 44774747, 44774774, 44774777, 44777444, 44777447, 44777474, 44777477, 44777744, 44777747, 44777774, 44777777, 47444444, 47444447, 47444474, 47444477, 47444744, 47444747, 47444774, 47444777, 47447444, 47447447, 47447474, 47447477, 47447744, 47447747, 47447774, 47447777, 47474444, 47474447, 47474474, 47474477, 47474744, 47474747, 47474774, 47474777, 47477444, 47477447, 47477474, 47477477, 47477744, 47477747, 47477774, 47477777, 47744444, 47744447, 47744474, 47744477, 47744744, 47744747, 47744774, 47744777, 47747444, 47747447, 47747474, 47747477, 47747744, 47747747, 47747774, 47747777, 47774444, 47774447, 47774474, 47774477, 47774744, 47774747, 47774774, 47774777, 47777444, 47777447, 47777474, 47777477, 47777744, 47777747, 47777774, 47777777, 74444444, 74444447, 74444474, 74444477, 74444744, 74444747, 74444774, 74444777, 74447444, 74447447, 74447474, 74447477, 74447744, 74447747, 74447774, 74447777, 74474444, 74474447, 74474474, 74474477, 74474744, 74474747, 74474774, 74474777, 74477444, 74477447, 74477474, 74477477, 74477744, 74477747, 74477774, 74477777, 74744444, 74744447, 74744474, 74744477, 74744744, 74744747, 74744774, 74744777, 74747444, 74747447, 74747474, 74747477, 74747744, 74747747, 74747774, 74747777, 74774444, 74774447, 74774474, 74774477, 74774744, 74774747, 74774774, 74774777, 74777444, 74777447, 74777474, 74777477, 74777744, 74777747, 74777774, 74777777, 77444444, 77444447, 77444474, 77444477, 77444744, 77444747, 77444774, 77444777, 77447444, 77447447, 77447474, 77447477, 77447744, 77447747, 77447774, 77447777, 77474444, 77474447, 77474474, 77474477, 77474744, 77474747, 77474774, 77474777, 77477444, 77477447, 77477474, 77477477, 77477744, 77477747, 77477774, 77477777, 77744444, 77744447, 77744474, 77744477, 77744744, 77744747, 77744774, 77744777, 77747444, 77747447, 77747474, 77747477, 77747744, 77747747, 77747774, 77747777, 77774444, 77774447, 77774474, 77774477, 77774744, 77774747, 77774774, 77774777, 77777444, 77777447, 77777474, 77777477, 77777744, 77777747, 77777774, 77777777, 444444444, 444444447, 444444474, 444444477, 444444744, 444444747, 444444774, 444444777, 444447444, 444447447, 444447474, 444447477, 444447744, 444447747, 444447774, 444447777, 444474444, 444474447, 444474474, 444474477, 444474744, 444474747, 444474774, 444474777, 444477444, 444477447, 444477474, 444477477, 444477744, 444477747, 444477774, 444477777, 444744444, 444744447, 444744474, 444744477, 444744744, 444744747, 444744774, 444744777, 444747444, 444747447, 444747474, 444747477, 444747744, 444747747, 444747774, 444747777, 444774444, 444774447, 444774474, 444774477, 444774744, 444774747, 444774774, 444774777, 444777444, 444777447, 444777474, 444777477, 444777744, 444777747, 444777774, 444777777, 447444444, 447444447, 447444474, 447444477, 447444744, 447444747, 447444774, 447444777, 447447444, 447447447, 447447474, 447447477, 447447744, 447447747, 447447774, 447447777, 447474444, 447474447, 447474474, 447474477, 447474744, 447474747, 447474774, 447474777, 447477444, 447477447, 447477474, 447477477, 447477744, 447477747, 447477774, 447477777, 447744444, 447744447, 447744474, 447744477, 447744744, 447744747, 447744774, 447744777, 447747444, 447747447, 447747474, 447747477, 447747744, 447747747, 447747774, 447747777, 447774444, 447774447, 447774474, 447774477, 447774744, 447774747, 447774774, 447774777, 447777444, 447777447, 447777474, 447777477, 447777744, 447777747, 447777774, 447777777, 474444444, 474444447, 474444474, 474444477, 474444744, 474444747, 474444774, 474444777, 474447444, 474447447, 474447474, 474447477, 474447744, 474447747, 474447774, 474447777, 474474444, 474474447, 474474474, 474474477, 474474744, 474474747, 474474774, 474474777, 474477444, 474477447, 474477474, 474477477, 474477744, 474477747, 474477774, 474477777, 474744444, 474744447, 474744474, 474744477, 474744744, 474744747, 474744774, 474744777, 474747444, 474747447, 474747474, 474747477, 474747744, 474747747, 474747774, 474747777, 474774444, 474774447, 474774474, 474774477, 474774744, 474774747, 474774774, 474774777, 474777444, 474777447, 474777474, 474777477, 474777744, 474777747, 474777774, 474777777, 477444444, 477444447, 477444474, 477444477, 477444744, 477444747, 477444774, 477444777, 477447444, 477447447, 477447474, 477447477, 477447744, 477447747, 477447774, 477447777, 477474444, 477474447, 477474474, 477474477, 477474744, 477474747, 477474774, 477474777, 477477444, 477477447, 477477474, 477477477, 477477744, 477477747, 477477774, 477477777, 477744444, 477744447, 477744474, 477744477, 477744744, 477744747, 477744774, 477744777, 477747444, 477747447, 477747474, 477747477, 477747744, 477747747, 477747774, 477747777, 477774444, 477774447, 477774474, 477774477, 477774744, 477774747, 477774774, 477774777, 477777444, 477777447, 477777474, 477777477, 477777744, 477777747, 477777774, 477777777, 744444444, 744444447, 744444474, 744444477, 744444744, 744444747, 744444774, 744444777, 744447444, 744447447, 744447474, 744447477, 744447744, 744447747, 744447774, 744447777, 744474444, 744474447, 744474474, 744474477, 744474744, 744474747, 744474774, 744474777, 744477444, 744477447, 744477474, 744477477, 744477744, 744477747, 744477774, 744477777, 744744444, 744744447, 744744474, 744744477, 744744744, 744744747, 744744774, 744744777, 744747444, 744747447, 744747474, 744747477, 744747744, 744747747, 744747774, 744747777, 744774444, 744774447, 744774474, 744774477, 744774744, 744774747, 744774774, 744774777, 744777444, 744777447, 744777474, 744777477, 744777744, 744777747, 744777774, 744777777, 747444444, 747444447, 747444474, 747444477, 747444744, 747444747, 747444774, 747444777, 747447444, 747447447, 747447474, 747447477, 747447744, 747447747, 747447774, 747447777, 747474444, 747474447, 747474474, 747474477, 747474744, 747474747, 747474774, 747474777, 747477444, 747477447, 747477474, 747477477, 747477744, 747477747, 747477774, 747477777, 747744444, 747744447, 747744474, 747744477, 747744744, 747744747, 747744774, 747744777, 747747444, 747747447, 747747474, 747747477, 747747744, 747747747, 747747774, 747747777, 747774444, 747774447, 747774474, 747774477, 747774744, 747774747, 747774774, 747774777, 747777444, 747777447, 747777474, 747777477, 747777744, 747777747, 747777774, 747777777, 774444444, 774444447, 774444474, 774444477, 774444744, 774444747, 774444774, 774444777, 774447444, 774447447, 774447474, 774447477, 774447744, 774447747, 774447774, 774447777, 774474444, 774474447, 774474474, 774474477, 774474744, 774474747, 774474774, 774474777, 774477444, 774477447, 774477474, 774477477, 774477744, 774477747, 774477774, 774477777, 774744444, 774744447, 774744474, 774744477, 774744744, 774744747, 774744774, 774744777, 774747444, 774747447, 774747474, 774747477, 774747744, 774747747, 774747774, 774747777, 774774444, 774774447, 774774474, 774774477, 774774744, 774774747, 774774774, 774774777, 774777444, 774777447, 774777474, 774777477, 774777744, 774777747, 774777774, 774777777, 777444444, 777444447, 777444474, 777444477, 777444744, 777444747, 777444774, 777444777, 777447444, 777447447, 777447474, 777447477, 777447744, 777447747, 777447774, 777447777, 777474444, 777474447, 777474474, 777474477, 777474744, 777474747, 777474774, 777474777, 777477444, 777477447, 777477474, 777477477, 777477744, 777477747, 777477774, 777477777, 777744444, 777744447, 777744474, 777744477, 777744744, 777744747, 777744774, 777744777, 777747444, 777747447, 777747474, 777747477, 777747744, 777747747, 777747774, 777747777, 777774444, 777774447, 777774474, 777774477, 777774744, 777774747, 777774774, 777774777, 777777444, 777777447, 777777474, 777777477, 777777744, 777777747, 777777774, 777777777, 4444444444}; int main() { long long l, r; cin >> l >> r; long long sm = 0; int size; size = sizeof lu / sizeof(long long); int st = 0; for (int i = 0; i < size - 1; ++i) { if (l > lu[i]) { st = i + 1; } } sm = (lu[st] - l + 1) * lu[st]; if (r <= lu[st]) { sm -= (lu[st] - r) * lu[st]; } else { for (int i = st; i < size - 1; ++i) { sm += (min(lu[i + 1], r) - lu[i]) * lu[i + 1]; if (r < lu[i + 1]) break; } } cout << sm << endl; return 0; }
1,100
CPP
n=input() str1=input() numbers= list(map(int,str1.split( ))) numbers.sort() sum1=0 sum2=0 m=0 for i in numbers: sum1=sum1+i while sum1>sum2 or sum1==sum2: sum1=sum1-numbers[len(numbers)-1-m] sum2=sum2+numbers[len(numbers)-1-m] m=m+1 print(m)
900
PYTHON3
r1, r2 = map(int, input().split()) c1, c2 = map(int, input().split()) d1, d2 = map(int, input().split()) def ok(a, b, c, d): if a <= 0 or b <= 0 or c <= 0 or d <= 0: return 0 if a >= 10 or b >= 10 or c >= 10 or d >= 10: return 0 if a == b or a == c or a == d or b == c or b == d or c == d: return 0 if a + b == r1 and c + d == r2 and a + c == c1 and b + d == c2 and a + d == d1 and b + c == d2: return 1 return 0 for i in range(1, 10): a = i b = r1 - a c = c1 - a d = d1 - a if ok(a, b, c, d): print(str(a) + " " + str(b) ) print(str(c) + " " + str(d) ) exit(0) print(-1)
1,000
PYTHON3
n,k=map(int, input().split()) if k==1: print('Yes') else: if k>=43: print('No') exit(0) for i in range(2,k+1): if n%i!=i-1: print('No') exit(0) print('Yes')
1,600
PYTHON3
table_card = input() hand_cards = input().split() match = 0 for card in hand_cards: if card[0] == table_card[0] or card[1] == table_card[1]: match += 1 break if match: print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int fastMax(long long int x, long long int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } long long int fastMin(long long int x, long long int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } const long long int MAXN = 6e5 + 10; bool mark[MAXN]; char inv(char ch) { return ((ch == 'B') ? 'W' : 'B'); } void solve() { long long int n, k; cin >> n >> k; string second; cin >> second; second = second + second + second; bool alt = 1; long long int n_ = 3 * n; for (long long int i = 0; i < n_; i++) { if (second[i] == second[(n_ + i - 1) % n_]) { mark[i] = mark[(n_ + i - 1) % n_] = 1; alt = 0; } } if (alt) { for (long long int i = n; i < 2 * n; i++) { if (k % 2) cout << inv(second[i]); else cout << second[i]; } return; } for (long long int l = 0; l < n_; l++) { if (!mark[l] && mark[(n_ + l - 1) % n_]) { long long int r = l; long long int d1 = 1, d2 = 1; while (!mark[(r + 1) % n_]) r = (r + 1) % n_, d2++; for (long long int i = l; i != (r + 1) % n_; i = (i + 1) % n_) { if (d1 < d2) { if (d1 <= k) { second[i] = second[(n_ + l - 1) % n_]; } else if (k % 2) second[i] = inv(second[i]); } else { if (d2 <= k) { second[i] = second[(r + 1) % n_]; } else if (k % 2) second[i] = inv(second[i]); } d2--; d1++; } } } cout << second.substr(n, n); } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t; t = 1; for (long long int i = (1); i <= (t); ++i) { solve(); } }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int v, i, j, mini = 1e9, val = 1e9; cin >> v; vector<int> a(10, -1); for (i = 1; i < 10; i++) { cin >> a[i]; if (a[i] <= mini) { mini = a[i]; val = i; } } int x = v / mini; int y = v % mini; vector<int> num(x, val); for (i = 0; i < x; i++) { for (j = 9; j > val; j--) { if (y >= a[j] - a[val]) { num[i] = j; y -= a[j] - a[val]; break; } } } if (num.size() == 0) cout << -1; else for (auto x : num) cout << x; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int t, n; int a[300005]; int deal() { int i; for (i = n; i >= 2; i--) { if (a[i - 1] < a[i]) { break; } } for (i; i >= 2; i--) { if (a[i - 1] > a[i]) { break; } } return i - 1; } int main(int argc, char** argv) { cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } if (n < 3) { cout << 0 << endl; continue; } cout << deal() << endl; } return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; const int N = 230; const int M = 3500; int m, n; int a[N], b[N]; long long dp[M][N]; int main() { cin >> m >> n; memset(dp, 0, sizeof(dp)); for (int i = 0; i < m; ++i) { cin >> a[i] >> b[i]; dp[a[i]][i]++; if (a[i] != b[i]) { a[i + m] = b[i]; b[i + m] = a[i]; dp[a[i + m]][i + m]++; } } for (int i = 1; i <= n; ++i) { for (int j = 0; j < m * 2; ++j) { if (!dp[i][j]) continue; for (int k = 0; k < 2 * m; ++k) { if (j % m != k % m && a[k] == b[j]) dp[i + a[k]][k] = (dp[i + a[k]][k] + dp[i][j]) % inf; } } } long long ans = 0; for (int i = 0; i < 2 * m; ++i) ans = (ans + dp[n][i]) % inf; cout << ans << endl; return 0; }
1,800
CPP
t = int(input()) l = [] for i in range(t): s = input() l.append(s) x = set(l) if len(x) == 1: print(l[0]) else: x = list(x) if l.count(x[0]) > l.count(x[1]): print(x[0]) else: print(x[1])
1,000
PYTHON3
n,m = map(int,input().split()) a = [None]*n for i in range(n): row = list(map(int,input().split())) a[i] = row ops = [] for i in range(n): for j in range(m): #print(i,j) #print(a) if i+1<n and j+1<m and (a[i][j] == 1 or a[i][j+1]==1 or a[i+1][j]==1 or a[i+1][j+1]==1): if a[i][j]>0 and a[i][j+1]>0 and a[i+1][j]>0 and a[i+1][j+1]>0: ops[len(ops):] = [(i,j)] a[i][j] += 1 a[i][j+1] += 1 a[i+1][j] += 1 a[i+1][j+1] += 1 possible = True for i in range(n): for j in range(m): if a[i][j] == 1: possible = False if not possible: print(-1) else: print(len(ops)) for op in ops: print(op[0]+1,op[1]+1)
1,200
PYTHON3
def cake_problem(): n = int(input()) a = [] for i in range(n+1): a.append([-1, -1]) l = input().split(' ') for i in range(len(l)): l[i] = int(l[i]) b = l[i] if a[b-1][0] == -1: a[b-1][0] = i else: a[b-1][1] = i len1, len2 = a[0][0], a[0][1] for i in range(1, n): len1 += abs(a[i][0] - a[i-1][0]) len2 += abs(a[i][1] - a[i-1][1]) print(len1 + len2) return if __name__ == '__main__': cake_problem()
1,200
PYTHON3
z=int(input()) x=input() v=input() d=0 w=0 c=0 for i in x: a=int(i) b=int(i) for o in range(18): if a==int(v[c]) or b==int(v[c]): w+=d d=0 break if a==9: a=-1 if b==0: b=10 a+=1 b-=1 d+=1 c+=1 print(w)
800
PYTHON3
n = int(input()) a = list(map(int, input().split())) # greedy used = {} x,y = [],[] for i in range(n): if not used: used[a[i]] = 1 x.append(i) elif a[i] in used and used[a[i]] == 1: y.append(i) used = {} else: used[a[i]] = used.get(a[i],0) + 1 if not y: print(-1) else: if len(x) > len(y): del x[-1] y[-1] = n-1 print(len(x)) #print(x) #print(len(y)) #print(y) for i in range(len(x)): #print("index ", i) print(x[i]+1,y[i]+1)
1,600
PYTHON3
import sys import math from collections import defaultdict,Counter # input=sys.stdin.readline # def print(x): # sys.stdout.write(str(x)+"\n") # sys.stdout=open("CP2/output.txt",'w') # sys.stdin=open("CP2/input.txt",'r') def find(n): p=math.factorial(n) q=math.factorial(n//2) return p//(q*q),q # while p<=n//2: # q*=n-p+1 # c*=p # p+=1 # return q//c # l=[] # c=0 # while n%2==0: # n//=2 # c+=1 # l.append([2,c]) # for i in range(3,int(math.sqrt(n))+1,2): # if n%i==0: # c=0 # while n%i==0: # n//=i # c+=1 # l.append([i,c]) # # l.append(i) # if n>2: # l.append([n,1]) # return l # mod=pow(10,9)+7 n=int(input()) ma,mi=find(n) # print(ma,mi) d=mi//(n//2) print(ma*d*d//2) # print(find(8)) # print(1260/70) # print(find(20)) # print(12164510040883200/184756) # print(65840947200.0/3628800)
1,300
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) bad = [0, 0] for i, x in enumerate(map(int, input().split())): if x % 2 != i % 2: bad[x%2] += 1 if bad[0] != bad[1]: print(-1) else: print(bad[0])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int sz = 1e5 + 10; const int mod = 998244353; void solve() { long long int l, r, i, col, ans = 0, j = 0; cin >> l >> r; for (i = 0; i <= 63; i++) { if ((l & (1LL << i)) == 0) { if ((l | (1LL << i)) > r) { break; } l |= ((1LL << i)); } } cout << l << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; string s; int getidx(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'A' && c <= 'Z') return c - 'A' + 10; if (c >= 'a' && c <= 'z') return c - 'a' + 36; if (c == '-') return 62; if (c == '_') return 63; } int main() { cin >> s; long long ans = 1; for (int i = 0; i < s.size(); i++) { int p = getidx(s[i]); for (int j = 0; j < 6; j++) if (!((p >> j) & 1)) ans = ans * 3 % mod; } cout << ans << endl; }
1,500
CPP
n, k = [int(v) for v in input().split()] a = [int(v) for v in input().split()] while k > 0 and a[k - 1] == 0: k -= 1 if k == 0: print(0) else: while k < n and a[k] == a[k - 1]: k += 1 print(k)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; string s; long cmpduong(string s1, string s2) { long len1 = s1.size(); long len2 = s2.size(); if (len1 < len2) return 2; if (len1 == len2) { if (s1 > s2) return 1; else return 2; } if (len1 > len2) return 1; } long cmp_am(string s1, string s2) { long len1 = s1.size(); long len2 = s2.size(); if (len1 < len2) return 1; if (len1 == len2) { if (s1 >= s2) return 2; else return 1; } if (len1 > len2) return 2; } long check(string s) { string tam = '-' + s; if (s[0] == '-') { if (cmp_am(s, "-128") == 1) return 1; if (cmp_am(s, "-32768") == 1) return 2; if (cmp_am(s, "-2147483648") == 1) return 3; if (cmp_am(s, "-9223372036854775808") == 1) return 4; return 5; } else { if (cmpduong(s, "127") == 2) return 1; if (cmpduong(s, "32767") == 2) return 2; if (cmpduong(s, "2147483647") == 2) return 3; if (cmpduong(s, "9223372036854775807") == 2) return 4; return 5; } } int main() { cin >> s; if (check(s) == 1) cout << "byte" << endl; if (check(s) == 2) cout << "short" << endl; if (check(s) == 3) cout << "int" << endl; if (check(s) == 4) cout << "long" << endl; if (check(s) == 5) cout << "BigInteger" << endl; return 0; }
1,300
CPP
#include <bits/stdc++.h> int a[111][111], v[111][111], n, sum = 0; int judge(int x, int y) { if (a[x][y] && a[x - 1][y] && a[x + 1][y] && a[x][y - 1] && a[x][y + 1] && !v[x][y]) return 1; return 0; } int dfs(int x, int y) { int i, j; if (!sum) return 1; for (i = 1; i < n - 1; i++) { for (j = 1; j < n - 1; j++) { if (judge(i, j)) { a[i][j] = a[i - 1][j] = a[i + 1][j] = a[i][j - 1] = a[i][j + 1] = 0; sum -= 5; v[i][j] = 1; return dfs(i, j); a[i][j] = a[i - 1][j] = a[i + 1][j] = a[i][j - 1] = a[i][j + 1] = 1; sum += 5; v[i][j] = 0; } } } } int main() { int i, j, flag = 1; char ch; scanf("%d", &n); getchar(); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { ch = getchar(); v[i][j] = 0; sum += (a[i][j] = (ch == '#') ? 1 : 0); } getchar(); } if (sum % 5) printf("NO\n"); else { if (dfs(1, 1) == 1) printf("YES\n"); else printf("NO\n"); } return 0; }
1,100
CPP
""" NTC here """ def iin(): return int(input()) def lin(): return list(map(int, input().split())) def main(): t = iin() while t: t-=1 n, s= input().split() s = int(s) n = list(map(int, n)) sm = sum(n) ans = 0 n = n[::-1] ch = 0 pv = 0 ln = len(n) pw = 0 while sm>s: # pw = 0 # for i in range(ln): # if n[pw]<n[i]: # pw = i if pw>=ln:break val = n[pw] if val==0: pw += 1 continue ans += ( 10-val if val else 0)*pow(10, pw) n[pw] = 0 pv = 1 for j in range(pw+1, ln): if n[j] == 9: n[j] = 0 else: n[j] += 1 break else: n.append(1) ln += 1 sm = sum(n) pw += 1 # print(n) print(ans) main()
1,500
PYTHON3
i=lambda:[*map(int,input().split())] for _ in[0]*i()[0]: n,d=i();a=i();k=1 while k<n and d>0:l=min(d//k,a[k]);a[0]+=l;d-=a[k]*k;k+=1 print(a[0])
800
PYTHON3
#include<iostream> #include <stdlib.h> #include<math.h> #include<cmath> #include<vector> #include<queue> #include<algorithm> #include<list> #include<stack> #include<map> #include<deque> #include<string> #include<set> #include<unordered_map> #include<unordered_set> #include<functional> #include <iomanip> typedef long long ll; typedef unsigned long long ull; typedef double dou; typedef long double ld; typedef int I; using namespace std; #define E endl /* _ _ _ _ _ _ | \ |`\ /`| | /`\ \ | \ ------ / | | / \ / | \ \ / / | | \_ _/ / | \ \ / / | | / | \ \/ / | | / | \ / | | \ | |`\ \ / /`| | | /``\ \ | | \ \/ / | | | / \ \ | | \____/ | | | \ / \ | | | | | \__/ / | | | | |_ _ _ _ _ _ _ _/ |_ | | _| */ void yes() { cout << "YES" << E; } void no() { cout << "NO" << E; } ll t, mod = 1e9 + 7; #define loop(a,n) for(ll i=a;i<n;i++) #define loopj(a,n) for(int j=a;j<n;j++) #define loopk(a,n) for(int k=a;k<n;k++) #define loopl(a,n) for(int l=a;l<n;l++) #define loopp(a,n) for(int p=a;p<n;p++) #define loop_r(a,n) for(int i=n-1;i>=a;i--) #define loop_Jr(a,n) for(int j=n-1;j>=a;j--) #define loop_it(m,it) for(it=m.begin();it!=m.end();it++) #define speed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define TC cin>>t ;while(t--) #define TC cin>>t ;while(t--) ll n,m,k; bool solve(ll x,ll y,ll sm){ if (x == n&&y == m){ if (sm == k)return 1; return 0; } if (x + 1 <= n) return solve(x + 1, y, sm + y); if (y+1<=m) return solve(x , y+1, sm + x); } int main() { speed; TC { cin >> n >> m >> k; if( solve(1,1,0))yes(); else no(); } return 0; }
800
CPP
t=int(input()) for _ in range(t): x=0 n=int(input()) l=list(map(int,input().split())) for i in range(n): if l[i]%2==1: x+=1 if x==n and n%2==0: print("NO") elif x > 0: print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MaxN = 10005; const double eps = 1e-12; struct Point { double x, y, z; Point() {} Point(double x, double y, double z) : x(x), y(y), z(z) {} Point operator+(const Point& p) const { return Point(x + p.x, y + p.y, z + p.z); } Point operator-(const Point& p) const { return Point(x - p.x, y - p.y, z - p.z); } Point operator*(const double& d) const { return Point(x * d, y * d, z * d); } Point operator/(const double& d) const { return Point(x / d, y / d, z / d); } double norm() const { return sqrt(x * x + y * y + z * z); } Point getOne() { Point ret = *this; ret = ret / norm(); return ret; } void init() { scanf("%lf%lf%lf", &x, &y, &z); } } p[MaxN], o; double vp, vs; double maxD; int n; Point getP(double t) { int pt = 0; double tmp; while (pt < n) { tmp = (p[pt + 1] - p[pt]).norm() / vs; if (t > tmp) { t -= tmp; pt++; } else { return p[pt] + (p[pt + 1] - p[pt]).getOne() * vs * t; } } return p[n]; } void doit() { double l = 0, r = maxD, mid; Point d; if ((p[n] - o).norm() / vp - r > eps) { puts("NO"); return; } for (int i = 0; i < 100; i++) { mid = (l + r) / 2.0; d = getP(mid); if ((d - o).norm() / vp - mid <= eps) { r = mid; } else { l = mid; } } puts("YES"); printf("%.10f\n", r); d = getP(r); printf("%.10f %.10f %.10f\n", d.x, d.y, d.z); } int main() { while (scanf("%d", &n) == 1) { maxD = 0; for (int i = 0; i <= n; i++) { p[i].init(); if (i) maxD += (p[i] - p[i - 1]).norm(); } scanf("%lf%lf", &vp, &vs); maxD /= vs; o.init(); doit(); } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N]; int main() { int n; while (cin >> n) { int v[10]; for (int i = 0; i < 10; i++) v[i] = 0; string s; cin >> s; for (auto i : s) { if (i == 'L') { for (int i = 0; i < 10; i++) { if (v[i] == 0) { v[i] = 1; break; } } } else if (i == 'R') { for (int i = 9; i >= 0; i--) { if (v[i] == 0) { v[i] = 1; break; } } } else v[i - '0'] = 0; } for (int i = 0; i < 10; i++) cout << v[i]; cout << '\n'; } }
800
CPP
def func(li,s): for i in li: if i > s//2: return print("T") if s%2==0 :return print("HL") else:return print("T") for _ in range(int(input())): n=int(input()) ai=list(map(int,input().split())) summ=sum(ai) func(ai,summ)
1,800
PYTHON3
from collections import Counter for ti in range(int(input())): # n, m = [int(x) for x in input().split()] # print(n,m) n = int(input()) l1 = list(map(int,input().split(' '))) # print(l1) if (sum(l1))% 2 == 1: print("YES") else: count1 = 0 count2 = 0 for i in l1: if i%2 == 0: count1 = count1 + 1 if i%2 == 1: count2 = count2 + 1 if count1>0 and count2>0: print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e18 + 7; int n, m; map<char, int> ch_num; int main() { int n; cin >> n; vector<string> ee(n); for (int i = 0; i < n; i++) { cin >> ee[i]; } vector<int> can_zero(27, -1); vector<pair<int, int> > let_sum(27); for (int i = 0; i < 26; i++) { let_sum[i] = make_pair(0, i); } for (auto cp : ee) { for (int i = 0; i < (int)cp.size(); i++) { if (i == 0) { can_zero[cp[i] - 'a'] = 0; } else if (can_zero[cp[i] - 'a'] != 0) { can_zero[cp[i] - 'a'] = 1; } let_sum[cp[i] - 'a'].first += pow(10, (int)cp.size() - i - 1); } } sort(let_sum.begin(), let_sum.end()); reverse(let_sum.begin(), let_sum.end()); bool z_used = false; int cur_n = 1; for (auto cp : let_sum) { if (can_zero[cp.second] == -1) { break; } if (can_zero[cp.second] && !z_used) { ch_num[(char)('a' + cp.second)] = 0; z_used = true; } else { ch_num[(char)('a' + cp.second)] = cur_n; cur_n++; } } long long ans = 0; for (auto hui : ee) { for (int i = 0; i < (int)hui.size(); i++) { ans += ch_num[hui[i]] * pow(10, (int)hui.size() - i - 1); } } cout << ans; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int asd[100008][2]; vector<int> v[100008]; vector<int> cost[100008]; vector<pair<int, int> > p[100008]; int n, k; bool used[100008]; int qwe[100008]; bool is[100008]; int dfs(int x, int y) { if (asd[x][y] != -1) { return asd[x][y]; } asd[x][y] = 0; if (y == 1) { is[x] = 1; vector<int> q; q.clear(); int res = 0; for (int i = 0; i < v[x].size(); i++) { if (is[v[x][i]]) continue; q.push_back(dfs(v[x][i], 1) + cost[x][i]); } sort(q.begin(), q.end()); reverse(q.begin(), q.end()); for (int i = 0; i < min((int)q.size(), k - 1); i++) { res += q[i]; } asd[x][y] = res; is[x] = 0; return res; } is[x] = 1; vector<pair<int, int> > q; q.clear(); int res = 0; for (int i = 0; i < v[x].size(); i++) { if (is[v[x][i]]) continue; used[v[x][i]] = 0; q.push_back(make_pair(dfs(v[x][i], 1) + cost[x][i], v[x][i])); qwe[v[x][i]] = dfs(v[x][i], 1) + cost[x][i]; } sort(q.begin(), q.end()); reverse(q.begin(), q.end()); for (int i = 0; i < min((int)q.size(), k - 1); i++) { res += q[i].first; used[q[i].second] = 1; } asd[x][y] = res; for (int i = 0; i < v[x].size(); i++) { if (is[v[x][i]]) continue; int uiop = max(dfs(v[x][i], 1), dfs(v[x][i], 0)) + cost[x][i] + res; if (used[v[x][i]] and q.size() >= k) uiop = uiop + q[k - 1].first; if (used[v[x][i]]) uiop = uiop - qwe[v[x][i]]; asd[x][y] = max(asd[x][y], uiop); } is[x] = 0; return asd[x][y]; } int main() { memset(asd, -1, sizeof(asd)); cin >> n >> k; for (int i = 0; i < n - 1; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); v[a].push_back(b); cost[a].push_back(c); swap(a, b); v[a].push_back(b); cost[a].push_back(c); } cout << max(dfs(0, 1), dfs(0, 0)) << endl; return 0; }
2,100
CPP