solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
for t in range(int(input())): a = input() i = 0 ans = "" while(i < len(a)): cnt = 1 j = i+1 while(j < len(a) and a[i] == a[j]): j += 1 cnt += 1 if (cnt & 1 == 1): ans += a[i] i = j s = set(ans) print(''.join(c for c in sorted(s)))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long i, n, a[100010], b[100010], sum = 0; int main() { scanf("%lld", &n); if (n <= 2) { printf("YES"); return 0; } for (i = 1; i <= n; i++) scanf("%lld", &a[i]), sum += a[i]; for (i = 1; i <= n; i++) scanf("%lld", &b[i]); sort(b + 1, b + n + 1); if (sum <= b[n] + b[n - 1]) printf("YES"); else printf("NO"); return 0; }
7
CPP
mum = int(input()) lst = list(map(int, input().split())) result = 0 if sum(lst) < mum: print(-1) else: while mum > 0: mum -= max(lst) lst.remove(max(lst)) result += 1 print(result)
7
PYTHON3
t = int(input()) for i in range(t): res=0 n = int(input()) l1 = list(map(int,input().split(" "))) m = int(input()) l2 = list(map(int,input().split(" "))) ch,ch1=0,0 nech,nech1 =0,0 for i in l1: if not(i%2): ch+=1 else: nech+=1 for j in l2: if not(j%2): ch1+=1 else: nech1+=1 print(nech*nech1+ch*ch1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int x, y, x1, y1; scanf("%d%d%d%d", &x, &y, &x1, &y1); int dx = abs(x - x1); int dy = abs(y - y1); printf("%d", 2 * (max(dx + 1, 2) + max(dy + 1, 2))); }
20
CPP
#include <iostream> #include <vector> #include <cstring> #include <queue> using namespace std; int n; int g[101][101] = {}; int sp[101] = {}; void bfs(){ queue<int> q; q.push(1); while( !q.empty() ){ int v = q.front(); q.pop(); for(int i=1; i<=n; i++){ if(g[v][i] == 1 && sp[i] == -1){ q.push(i); sp[i] = sp[v]+1; } } } } int main(){ memset(g,0,sizeof(g)); memset(sp,-1,sizeof(sp)); cin >> n; sp[1] = 0; for(int i=1; i<=n; i++){ int u,v,k; cin >> u >> k; for(int j=0; j<k; j++){ cin >> v; g[u][v] = 1; } } bfs(); for(int i=1; i<=n; i++) cout << i << " " << sp[i] << endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, pmax, pmin, t, ans = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> t; if (t == 1) pmin = i; if (t == n) pmax = i; } ans = max(ans, pmax); ans = max(ans, pmin); ans = max(ans, n - pmax - 1); ans = max(ans, n - pmin - 1); cout << ans << endl; return 0; }
7
CPP
a=input() b=sum((a[i] in '47') for i in range(len(a))) print(['NO','YES'][b==4 or b==7])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int pos[110], a[110]; int main() { int n, m, k, i, j, x, ans = 0; cin >> n >> m >> k; for (i = 1; i <= k; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } for (; n--;) for (j = 1; j <= m; j++) { scanf("%d", &x); ans += pos[x]; for (i = pos[x]; i > 1; i--) { a[i] = a[i - 1]; pos[a[i]] = i; } a[1] = x, pos[x] = 1; } cout << ans << endl; return 0; }
8
CPP
""" UNSAAC E.P.I.I.S. NOMBRE: Robert Andres Quispe Uscamayta CODIGO: 134033 """ a=input() print(a[0].capitalize()+a[1:])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool isprime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } vector<long long int> prime; void sieve(int n) { bool bakh[n + 1]; memset(bakh, true, sizeof(bakh)); for (int p = 2; p * p <= n; p++) { if (bakh[p] == true) { for (int i = p * p; i <= n; i += p) bakh[i] = false; } } for (int p = 2; p <= n; p++) if (bakh[p]) prime.push_back(p); } long long int eulertotient(long long int z) { long long int fac = z; for (long long int i = 0; prime[i] * prime[i] <= z; i++) { if (z % prime[i] == 0) { fac -= (fac / prime[i]); while (z % prime[i] == 0) z /= prime[i]; } } if (z > 1) fac -= (fac / z); return fac; } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { long long int g = gcd(a, b); long long int ans = (a * b) / g; return ans; } long long int modInverse(long long int a, long long int m) { long long int hvf = gcd(a, m); if (hvf == 1) return power(a, m - 2, m); return -1; } void multiply(long long int F[2][2], long long int M[2][2]) { long long int x = F[0][0] * M[0][0] + F[0][1] * M[1][0]; long long int y = F[0][0] * M[0][1] + F[0][1] * M[1][1]; long long int z = F[1][0] * M[0][0] + F[1][1] * M[1][0]; long long int w = F[1][0] * M[0][1] + F[1][1] * M[1][1]; F[0][0] = x; F[0][1] = y; F[1][0] = z; F[1][1] = w; } void powermat(long long int F[2][2], long long int n) { if (n == 0 || n == 1) return; long long int M[2][2] = {{1, 1}, {1, 0}}; powermat(F, n / 2); multiply(F, F); if (n % 2 != 0) multiply(F, M); } long long int fib(long long int n) { long long int F[2][2] = {{1, 1}, {1, 0}}; if (n == 0) return 0; powermat(F, n - 1); return F[0][0]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n, m; cin >> n >> m; long long int ar[n]; for (long long int i = 0; i < n; i++) cin >> ar[i]; vector<long long int> s(n); s[0] = ar[0]; for (long long int i = 1; i <= n - 1; i++) s[i] = s[i - 1] + ar[i]; long long int que[m]; for (long long int i = 0; i < m; i++) cin >> que[i]; long long int ans[m][2]; for (long long int i = 0; i < m; i++) { long long int tx = upper_bound(s.begin(), s.end(), que[i] - 1) - s.begin(); tx -= 1; if (tx == -1) ans[i][0] = 1, ans[i][1] = que[i]; else if (tx == (n - 1)) { ans[i][0] = n, ans[i][1] = ar[n - 1]; } else ans[i][0] = tx + 2, ans[i][1] = que[i] - s[tx]; } for (long long int i = 0; i < m; i++) cout << ans[i][0] << " " << ans[i][1] << endl; }
9
CPP
s1=input() s2=input() cnt=0 for i in s2: if i!=' ': if s2.count(i)>s1.count(i): cnt+=1 if cnt==0: print("YES") else: print("NO")
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int T; int N; int P[4005], Q[4005]; bool vis[4005]; bool F[8005], tmp[8005]; int main() { scanf("%d", &T); while (T--) { scanf("%d", &N); for (int i = 1; i <= 2 * N; i++) scanf("%d", &P[i]), Q[P[i]] = i, vis[i] = 0; int lstpos = 2 * N + 1; for (int i = 0; i <= 4 * N; i++) F[i] = 0; F[2 * N] = 1; for (int i = 2 * N; i; i--) if (!vis[i]) { int nowpos = Q[i]; for (int j = nowpos; j < lstpos; j++) vis[P[j]] = 1; for (int i = 0; i <= 4 * N; i++) { tmp[i] = 0; if (i + lstpos - nowpos <= 4 * N) tmp[i] |= F[i + lstpos - nowpos]; if (i - lstpos + nowpos >= 0) tmp[i] |= F[i - lstpos + nowpos]; } for (int i = 0; i <= 4 * N; i++) F[i] = tmp[i]; lstpos = nowpos; } if (F[2 * N]) printf("YES\n"); else printf("NO\n"); } }
8
CPP
#include<bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using P=pair<ll,ll>; #define MOD 1000000007ll #define INF 1000000000ll #define EPS 1e-7 #define FOR(i,n,m) for(ll i=n;i<(ll)m;i++) #define REP(i,n) FOR(i,0,n) #define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;} #define ALL(v) v.begin(),v.end() #define UNIQUE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end()); #define pb push_back int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n,p,q; cin>>n>>p>>q; vector<ll> c(n); REP(i,n) cin>>c[i]; ll sum=0; REP(i,n) sum+=c[i]; ll ans=sum; vector<ll> diff(n); REP(i,n) diff[i]=p*(q-i)-c[i]; sort(ALL(diff)); ll buf=0; FOR(i,1,n+1) { buf+=diff[n-i]+2*p*(i-1); ans=max(ans,sum+buf); } cout<<ans<<endl; return 0; }
0
CPP
n=int(input()) l=[0]+[int(input()) for _ in [0]*n] a=1 c=0 while a!=2 and c<n: a=l[a];c+=1 if c==n and a!=2:print(-1) else:print(c)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[400005], b[400005], c[400005], d[400005]; int main() { int n, a1 = 0, b1 = 0, c1 = 0, d1 = 0; scanf("%d", &n); while (n--) { int u, v; scanf("%d %d", &u, &v); if (u == 11) a[a1++] = v; if (u == 10) b[b1++] = v; if (u == 1) c[c1++] = v; if (u == 0) d[d1++] = v; } sort(b, b + b1); sort(c, c + c1); sort(d, d + d1); int cnt = 0; int ans = 0; for (int i = 0; i < a1; i++) { ans += a[i]; } cnt = a1; reverse(b, b + b1); reverse(c, c + c1); reverse(d, d + d1); for (int i = 0; i < min(b1, c1); i++) { ans += b[i]; ans += c[i]; } if (b1 < c1) { for (int i = b1; i < c1; i++) b[i] = c[i]; swap(b1, c1); } int i = 0, j = c1; while (i < d1 && j < b1 && cnt > 0) { if (b[j] > d[i]) { ans += b[j++]; } else { ans += d[i++]; } cnt--; } while (i < d1 && cnt > 0) { ans += d[i++]; cnt--; } while (j < b1 && cnt > 0) { ans += b[j++]; cnt--; } printf("%d\n", ans); }
12
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long INF = (long long)1000000007 * 1000000007; const long double eps = 1e-8; const long double pi = acos(-1.0); int n; const int MAX_N = (1 << 18); int nn, dat[3][2 * MAX_N - 1]; void init(int n_) { nn = 1; while (nn < n_) nn *= 2; } void update(int id, int k, int a) { k += nn - 1; dat[id][k] = a; while (k > 0) { k = (k - 1) / 2; dat[id][k] = dat[id][k * 2 + 1] + dat[id][k * 2 + 2]; } } int query(int id, int a, int b, int k, int l, int r) { if (r <= a || b <= l) return 0; if (a <= l && r <= b) return dat[id][k]; else { int vl = query(id, a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(id, a, b, k * 2 + 2, (l + r) / 2, r); return vl + vr; } } int exile(int id) { if (query(id, 0, n, 0, 0, nn) == 0) return n; int k = 0; while (k < nn - 1) { int vl = dat[id][k * 2 + 1], vr = dat[id][k * 2 + 2]; if (vl) k = k * 2 + 1; else k = k * 2 + 2; } return k - (nn - 1); } int exiri(int id) { if (query(id, 0, n, 0, 0, nn) == 0) return -1; int k = 0; while (k < nn - 1) { int vl = dat[id][k * 2 + 1], vr = dat[id][k * 2 + 2]; if (vr) k = k * 2 + 2; else k = k * 2 + 1; } return k - (nn - 1); } int trans(char t) { if (t == 'R') return 0; else if (t == 'S') return 1; else return 2; } int le[3][2], ri[3][2]; int ans() { int res = 0; for (int i = 0; i < 3; i++) { le[i][0] = exile((i + 1) % 3); ri[i][0] = exiri((i + 1) % 3); le[i][1] = exile((i + 2) % 3); ri[i][1] = exiri((i + 2) % 3); if (le[i][0] <= le[i][1]) { res += query(i, 0, ri[i][0], 0, 0, nn); res += query(i, ri[i][1], n, 0, 0, nn); if (ri[i][1] <= ri[i][0]) res -= query(i, ri[i][1], ri[i][0], 0, 0, nn); } else if (ri[i][1] <= ri[i][0]) { res += query(i, 0, le[i][1], 0, 0, nn); res += query(i, le[i][0], n, 0, 0, nn); if (le[i][0] <= le[i][1]) res -= query(i, le[i][0], le[i][1], 0, 0, nn); } else { int lele = max(le[i][0], ri[i][1]); int riri = min(le[i][1], ri[i][0]); int lel = min(le[i][0], ri[i][1]); int rir = max(le[i][1], ri[i][0]); res += query(i, 0, riri, 0, 0, nn); res += query(i, lele, n, 0, 0, nn); res += query(i, lel, rir, 0, 0, nn); } } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); int q; cin >> n >> q; init(n); string s; cin >> s; for (int i = 0; i < n; i++) { update(trans(s[i]), i, 1); } cout << ans() << endl; for (int i = 0; i < q; i++) { int x; char y; cin >> x >> y; x--; update(trans(s[x]), x, 0); s[x] = y; update(trans(s[x]), x, 1); cout << ans() << endl; } return 0; }
10
CPP
#include<algorithm> #include<cstdio> #include<vector> using namespace std; typedef pair<int, int> P; int n, k; pair<int, int> p[50]; int main() { scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%d %d", &p[i].first, &p[i].second); sort(p, p + n); long ans = 4e18; for (int i = 0; i <= n - k; i++) { vector<int> y; for (int j = i; j < i + k - 1; j++) y.push_back(p[j].second); for (int j = i + k - 1; j < n; j++) { y.push_back(p[j].second); sort(y.begin(), y.end()); int my = 2e9; for (int l = 0; l <= j - i + 1 - k; l++) { my = min(my, y[l + k - 1] - y[l]); } ans = min(ans, static_cast<long>(p[j].first - p[i].first) * my); } } printf("%ld\n", ans); return 0; }
0
CPP
#!/usr/bin/env python3 from collections import deque t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [] for i in range(n): a.append(list(input())) g = [] b = [] for i in range(n): for j in range(m): if a[i][j] == 'G': g.append((i, j)) elif a[i][j] == 'B': b.append((i, j)) if len(g) == 0: print("Yes") continue possible = True for b_i in b: i, j = b_i if i != 0 and a[i - 1][j] == '.': a[i - 1][j] = '#' if i != n - 1 and a[i + 1][j] == '.': a[i + 1][j] = '#' if j != 0 and a[i][j - 1] == '.': a[i][j - 1] = '#' if j != m - 1 and a[i][j + 1] == '.': a[i][j + 1] = '#' if a[n - 1][m - 1] == '#': print("No") continue visited = set() d = deque() d.append((n - 1, m - 1)) visited.add((n - 1, m - 1)) while len(d) != 0: i, j = d[0] d.popleft() if a[i][j] == 'G': g.remove((i, j)) if a[i][j] == 'B': possible = False break if i != 0 and a[i - 1][j] != '#' and not (i - 1, j) in visited: d.append((i - 1, j)) visited.add((i - 1, j)) if i != n - 1 and a[i + 1][j] != '#' and not (i + 1, j) in visited: d.append((i + 1, j)) visited.add((i + 1, j)) if j != 0 and a[i][j - 1] != '#' and not (i, j - 1) in visited: d.append((i, j - 1)) visited.add((i, j - 1)) if j != m - 1 and a[i][j + 1] != '#' and not (i, j + 1) in visited: d.append((i, j + 1)) visited.add((i, j + 1)) if not possible or len(g) != 0: print("No") else: print("Yes")
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, i, j, x, y, X, Y, ver[2000][2000], gor[2000][2000], cx, cy, a[2000][2000], mn; bool flag, flag1; string s; void init() { int i, j; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) if (a[i][j] == 2) a[i][j] = 1; } bool run() { int i, j, nx = x, ny = y; for (i = nx; i <= nx + X - 1; i++) for (j = ny; j <= ny + Y - 1; j++) { if (a[i][j] == 0) { init(); return false; } a[i][j] = 2; } while (1) { if (a[nx + X][ny] == 1) { for (j = ny; j <= ny + Y - 1; j++) { if (a[nx + X][j] == 0) { init(); return false; } a[nx + X][j] = 2; } nx++; continue; } if (a[nx][ny + Y] == 1) { for (i = nx; i <= nx + X - 1; i++) { if (a[i][ny + Y] == 0) { init(); return false; } a[i][ny + Y] = 2; } ny++; continue; } break; } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (a[i][j] == 1) { init(); return false; } } } init(); return true; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; flag = false; for (i = 1; i <= n; i++) { cin >> s; for (j = 0; j < m; j++) { if (s[j] == 'X') { a[i][j + 1] = 1; if (!flag) { flag = true; x = i; y = j + 1; } } else a[i][j + 1] = 0; } } if (!flag) { cout << -1 << endl; return 0; } for (i = 1; i <= n; i++) { gor[i][1] = a[i][1]; for (j = 2; j <= m; j++) { gor[i][j] = gor[i][j - 1] + a[i][j]; } } for (j = 1; j <= m; j++) { ver[j][1] = a[1][j]; for (i = 2; i <= n; i++) { ver[j][i] = ver[j][i - 1] + a[i][j]; } } i = x; while (i <= n && a[i][y] == 1) i++; i--; j = y; while (j <= m && a[x][j] == 1) j++; j--; cx = i; cy = j; flag = false; mn = 2 * 1000 * 1000 * 1000; for (j = y; j <= cy; j++) { if (ver[j][cx] - ver[j][x - 1] < cx - x + 1) { goto l1; } if (ver[j][cx] - ver[j][x - 1] == cx - x + 1 && a[cx + 1][j] == 1) { flag = true; X = cx - x + 1; Y = cy - j + 1; break; } } if (flag) { if (run()) { mn = min(mn, X * Y); } else { goto l1; } } X = cx - x + 1; Y = 1; if (run()) { mn = min(mn, X * Y); } else { goto l1; } l1: flag = false; for (i = x; i <= cx; i++) { if (gor[i][cy] - gor[i][y - 1] < cy - y + 1) { if (mn == 2 * 1000 * 1000 * 1000) cout << -1 << endl; else cout << mn << endl; return 0; } if (gor[i][cy] - gor[i][y - 1] == cy - y + 1 && a[i][cy + 1] == 1) { flag = true; X = cx - i + 1; Y = cy - y + 1; break; } } if (flag) { if (run()) { mn = min(mn, X * Y); } else { if (mn == 2 * 1000 * 1000 * 1000) cout << -1 << endl; else cout << mn << endl; return 0; } } X = 1; Y = cy - y + 1; if (run()) { mn = min(mn, X * Y); } else { if (mn == 2 * 1000 * 1000 * 1000) cout << -1 << endl; else cout << mn << endl; return 0; } if (mn == 2 * 1000 * 1000 * 1000) cout << -1 << endl; else cout << mn << endl; return 0; }
9
CPP
#include <bits/stdc++.h> int main() { int n, i, j, k, q, count = 0, z; scanf("%d %d", &n, &k); int arr[n]; for (i = 0; i < n; i++) scanf("%d", &arr[i]); for (i = 0; i < n; i++) { q = 0; for (j = 0; j < 11; j++) { z = arr[i] % 10; arr[i] /= 10; if (z == 4 || z == 7) q++; if (arr[i] == 0) break; } if (q <= k) count++; } printf("%d", count); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int deg[5]; int main() { for (int i = 1; i <= 3; i++) { int x, y; cin >> x >> y; deg[x]++; deg[y]++; } for (int i = 1; i <= 4; i++) if (deg[i] == 3) return cout << "NO", 0; cout << "YES"; return 0; }
0
CPP
inp = input().split() l = int(inp[0]) w = int(inp[1]) color = inp[2] rows=[] ans =set() for r in range(l): row = input() rows.append(row) for r in range(l): for c in range(w): row = rows[r] if row[c] == color : if r !=0 and (rows[r-1][c] != '.' and rows[r-1][c] != color): ans.add(rows[r-1][c]) if c!=0 and (row[c-1] != '.' and row[c-1]!=color): ans.add(row[c-1]) if r != l-1 and (rows[r+1][c] != '.' and rows[r+1][c]!=color): ans.add(rows[r+1][c]) if c!=w-1 and (row[c+1] != '.' and row[c+1]!=color): ans.add(row[c+1]) print(len(ans))
8
PYTHON3
a = input().split(' ') n = int(a[0]) m = int(a[1]) jj = list(map(int, input().split(' '))) jj.sort() s=0 h=[0]*n for i in range(n): s=s+jj[i] if(i >= m): h[i%m] = h[i%m] + jj[i-m] s = s + h[i%m] print(s,end=' ')
9
PYTHON3
#include<iostream> #include<cstdio> using namespace std; int x[10000000]; int G[20]; int v; int w; int cnt=0; int m; int n; void sort1(int x[],int n,int g){ for(int i=g;i<n;i++){ v=x[i]; w=i-g; while(w>=0 && x[w]>v){ x[w+g]=x[w]; w=w-g; cnt++; } x[w+g]=v; } } void sort2(int x[],int n){ cnt=0; for(int h=0;h<20;h++){ if(G[h]>n){ m=h; break; } } cout<<m<<endl; for(int h=m-1;h>=0;h--){ cout<<G[h]; if(h){ cout<<' '; } sort1(x,n,G[h]); } cout<<endl; } int main(){ G[0]=1; for(int i=1;i<20;i++){ G[i]=G[i-1]*3+1; } cin>>n; for(int i=0;i<n;i++){ scanf("%d",&x[i]); } sort2(x,n); cout<<cnt<<endl; for(int i=0;i<n;i++){ printf("%d\n",x[i]); } return 0; }
0
CPP
t = int(input()) for i in range(t): x,y = map(int,input().split());m = max(2*min(x,y),max(x,y));print(m**2,end='\n')
7
PYTHON3
import math t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) if any(i < 0 for i in a): print("NO") continue g = a[0] for i in a: g = math.gcd(g, i) m = max(a) ans = [i for i in range(min(min(a), g), max(a)+1, g)] if len(ans) <= 300: print("Yes") print(len(ans)) print(*ans) else: print("No")
7
PYTHON3
T=int(input()) II=0 from math import * def mindivisor(n): i=1 s=[] while i<=sqrt(n): if n%i==0: s.append(i) i+=1 q=[d for d in s] for x in q: s.append(n//x) i=0 while i<len(s): if s[i]>k: s[i]=0 i+=1 return(n//max(s)) while II<T: s=input().split() n=int(s[0]) k=int(s[1]) if n<=k: print(1) elif k==1: print(n) else: print(mindivisor(n)) II+=1
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int v[112345]; int cnt[6]; map<string, int> mapa; map<int, string> mapa2; int s, t; int flow[20][20]; vector<int> g[20]; int pai[20]; bool bfs() { queue<int> q; q.push(s); memset(pai, -1, sizeof pai); pai[s] = -2; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < g[x].size(); i++) { int y = g[x][i]; if (pai[y] == -1 && flow[x][y]) { pai[y] = x; q.push(y); if (y == t) return true; } } } return false; } int maxflow() { int total = 0; while (bfs()) { int f = 112345; int x = t; while (x != s) { f = min(f, flow[pai[x]][x]); x = pai[x]; } x = t; while (x != s) { flow[pai[x]][x] -= f; flow[x][pai[x]] += f; x = pai[x]; } total += f; } return total; } int main() { mapa["S"] = 0; mapa["M"] = 1; mapa["L"] = 2; mapa["XL"] = 3; mapa["XXL"] = 4; mapa["XXXL"] = 5; mapa["S,M"] = 6; mapa["M,L"] = 7; mapa["L,XL"] = 8; mapa["XL,XXL"] = 9; mapa["XXL,XXXL"] = 10; mapa2[0] = "S"; mapa2[1] = "M"; mapa2[2] = "L"; mapa2[3] = "XL"; mapa2[4] = "XXL"; mapa2[5] = "XXXL"; pair<int, int> ord[112345]; for (int i = 0; i < 6; i++) scanf("%d", &cnt[i]); bool flag = true; scanf("%d", &n); int cntAux = 0; for (int i = 0; i < n; i++) { string s; cin >> s; int aux = mapa[s]; if (aux < 6) { if (cnt[aux] <= 0) { printf("NO\n"); return 0; } else { cnt[aux]--; } cntAux++; } else { flow[0][aux - 5]++; } v[i] = aux; ord[i] = make_pair(aux, i); } g[0].push_back(1); g[0].push_back(2); g[0].push_back(3); g[0].push_back(4); g[0].push_back(5); g[1].push_back(0); g[1].push_back(6); g[1].push_back(7); g[2].push_back(0); g[2].push_back(7); g[2].push_back(8); g[3].push_back(0); g[3].push_back(8); g[3].push_back(9); g[4].push_back(0); g[4].push_back(9); g[4].push_back(10); g[5].push_back(0); g[5].push_back(10); g[5].push_back(11); g[6].push_back(1); g[6].push_back(12); g[7].push_back(1); g[7].push_back(2); g[7].push_back(12); g[8].push_back(2); g[8].push_back(3); g[8].push_back(12); g[9].push_back(3); g[9].push_back(4); g[9].push_back(12); g[10].push_back(4); g[10].push_back(5); g[10].push_back(12); g[11].push_back(5); g[11].push_back(12); sort(ord, ord + n); for (int i = 1; i <= 5; i++) { flow[i][i + 5] = 112345; flow[i][i + 6] = 112345; } for (int i = 0; i < 6; i++) { flow[i + 6][12] = cnt[i]; } s = 0, t = 12; int mf = maxflow(); if (mf + cntAux == n) { puts("YES"); for (int i = 0; i < n; i++) { if (ord[i].first >= 6) { int aux = ord[i].first - 6; if (cnt[aux] > 0) { cnt[aux]--; v[ord[i].second] = aux; } else { cnt[aux + 1]--; v[ord[i].second] = aux + 1; } } } for (int i = 0; i < n; i++) { cout << mapa2[v[i]] << endl; } } else { puts("NO"); } return 0; }
10
CPP
a ,b,c = map(int, input().split()) if a<= b and a<= c: print('YES') else: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2 * 1e5 + 5; deque<int> q; vector<char> ans, k1, k2, k; void g_left(int v) { int x = v; deque<int> t = q; x = t.front(); t.pop_front(); k1.push_back('L'); while (t.size() > 0) { int a = t.front(), b = t.back(); if (a > x) { k1.push_back('L'); x = a; t.pop_front(); } else return; } return; } void g_right(int v) { int x = v; deque<int> t = q; x = t.back(); t.pop_back(); k2.push_back('R'); while (t.size() > 0) { int a = t.front(), b = t.back(); if (b > x) { k2.push_back('R'); x = b; t.pop_back(); } else return; } return; } int32_t main() { ios_base::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; ++i) { int t; cin >> t; q.push_back(t); } int x = -1; while (q.size() > 0) { int a = q.front(); int b = q.back(); if (a < b && x < a || a > x && b <= x) { ans.push_back('L'); x = a; q.pop_front(); } else if (b < a && x < b || b > x && a <= x) { ans.push_back('R'); x = b; q.pop_back(); } else if (a == b && x < a) { g_left(x); g_right(x); if (k1.size() > k2.size()) { for (char i : k1) ans.push_back(i); } else { for (char i : k2) ans.push_back(i); } cout << ans.size() << endl; for (auto i : ans) cout << i; return 0; } else break; } if (q.size() == 1 && x < q.back()) ans.push_back('R'); cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { cout << ans[i]; } }
9
CPP
n,m = map(int,input().split()) # n = int(n) # m = int(m) a=[] ipa=[] b=[] ipb=[] for i in range(n): y,x=map(str,input().split()) a.append(y) ipa.append(x) for i in range(m): y,x=map(str,input().split()) for j in range(n): if x[:len(x)-1]==ipa[j]: print(y+" "+x+" #"+a[j])
8
PYTHON3
def solve(n): count = 0 flag = True while (flag): if n == 1: return count elif n%3==0 or n%6==0: if n%6==0: n = n/6 else: n *= 2 count+=1 elif n%3!=0 and n%6!=0: return -1 #print(n) tc = int(input()) while tc>0: n=int(input()) print(solve(n)) tc-=1
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; int kx[] = {-2, -1, 1, 2, 2, 1, -1, -2}; int ky[] = {1, 2, 2, 1, -1, -2, -2, -1}; bool com(pair<int, int> a, pair<int, int> b) { return abs(a.first - a.second) < abs(b.first - b.second); } void solved() { int n, x, y; cin >> n; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { cin >> x >> y; v.push_back({x, y}); } sort(v.begin(), v.end(), com); int ar[n + 1]; memset(ar, 0, sizeof(ar)); for (auto it : v) { int a = it.first, b = it.second; if (ar[a] == 0) { cout << a << ' ' << b << ' ' << a << endl; ar[a] = 1; } else if (ar[b] == 0) { cout << a << ' ' << b << ' ' << b << endl; ar[b] = 1; } else { for (int i = a; i <= b; i++) { if (ar[i] == 0) { cout << a << ' ' << b << ' ' << i << endl; ar[i] = 1; break; } } } } } int main() { long long int tc; cin >> tc; while (tc--) { solved(); } }
8
CPP
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) typedef long long ll; int main(){ string s; int k; ll cnt1=0,cnt2=0; cin >> s >> k; if(s == string(s.size(),s[0])) { cout << s.size() * k / 2 << endl; return 0; } string t = s+s; rep(i,s.size()-1) if(s[i] == s[i+1]) s[i+1] = '#',cnt1++; rep(i,t.size()-1) if(t[i] == t[i+1]) t[i+1] = '#',cnt2++; cout << cnt1 + (cnt2-cnt1)*(k-1) << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; bool isPrime(int n) { if (n <= 1) return false; for (int i = 2; i < n; i++) if (n % i == 0) return false; return true; } void solve() { int n; cin >> n; int sum = 0; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; } if (sum % 2 == 0 || !isPrime(sum)) { cout << n << endl; for (int i = 1; i <= n; i++) { cout << i << " "; } cout << endl; return; } long long temp = -1; for (int i = 0; i < n; i++) { if ((sum - arr[i]) % 2 == 0) { temp = arr[i]; break; } } bool flag = 1; cout << n - 1 << endl; for (int i = 0; i < n; i++) { if (flag && arr[i] == temp) { flag = 0; } else { cout << i + 1 << " "; } } cout << endl; } int main() { int t; cin >> t; while (t--) { solve(); } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; } const int maxn = 300010; int n, W; int w[maxn], v[maxn]; vector<int> A, B, C; int nA, nB, nC; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> W; for (int i = (0); i < (n); i++) { cin >> w[i] >> v[i]; if (w[i] == 1) A.push_back(v[i]); if (w[i] == 2) B.push_back(v[i]); if (w[i] == 3) C.push_back(v[i]); } sort(A.begin(), A.end()); reverse(A.begin(), A.end()); sort(B.begin(), B.end()); reverse(B.begin(), B.end()); sort(C.begin(), C.end()); reverse(C.begin(), C.end()); nA = A.size(); nB = B.size(); nC = C.size(); long long dp[maxn], suma[maxn]; pair<int, int> r[maxn]; memset(dp, 0LL, sizeof dp); dp[0] = 0; r[0] = pair<int, int>(0, 0); for (int i = (1); i < (W + 1); i++) { pair<int, int> x = r[i - 1]; pair<int, int> y; if (i >= 2) y = r[i - 2]; if (x.first < nA) { if (dp[i - 1] + A[x.first] > dp[i]) { dp[i] = dp[i - 1] + A[x.first]; r[i] = pair<int, int>(x.first + 1, x.second); } } if (i >= 2 and y.second < nB) { if (dp[i - 2] + B[y.second] > dp[i]) { dp[i] = dp[i - 2] + (int)B[y.second]; r[i] = pair<int, int>(y.first, y.second + 1); } } if (dp[i - 1] > dp[i]) { dp[i] = dp[i - 1]; r[i] = r[i - 1]; } if (i >= 2 and dp[i - 2] > dp[i]) { dp[i] = dp[i - 2]; r[i] = r[i - 2]; } } for (int i = (0); i < (nC); i++) { suma[i] = C[i]; if (i > 0) suma[i] += suma[i - 1]; } long long maxi = 0; for (int i = (0); i < (W); i++) { if (W - (i + 1) * 3 < 0) break; maxi = max(maxi, suma[i] + dp[W - (i + 1) * 3]); } maxi = max(maxi, dp[W]); cout << maxi << '\n'; return 0; }
11
CPP
n = int(input()) groups = input() groups = [int(group) for group in groups.strip().split()] d = {i: groups.count(i) for i in range(1, 5)} res4 = d[4] if d[3] >= d[1]: res3 = d[3] d[1] = 0 else: res3 = d[3] d[1] = d[1] - d[3] if d[2] % 2 == 0: res2 = d[2] // 2 d[2] = 0 else: res2 = d[2] // 2 d[2] = 1 res1 = 0 if d[2]: if d[1] <= 2: res1 = 1 d[1] = 0 else: res1 = 1 d[1] -= 2 res0 = 0 if d[1]: if d[1] > 4: tmp = 1 if d[1] % 4 else 0 res0 = d[1] // 4 + tmp else: res0 = 1 print(res0+res1+res2+res3+res4)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long powmod(long long b, long long p) { long long r = 1; for (; p; p >>= 1, b = b * b % mod) if (p & 1) r = b * r % mod; return r; } long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } const int N = 2024; long long dp[N][N], dp2[N][N]; int main() { int k, a, b; cin >> k >> a >> b; long long p = powmod(a + b, mod - 2), q = p; p = (p * a) % mod; q = (q * b) % mod; long long ip = powmod(p, mod - 2), iq = powmod(q, mod - 2); dp[1][k] = 1; long long ans = 0; long long cur = (p - 1) * (p - 1) % mod; cur = (p * powmod(cur, mod - 2)) % mod; for (int i = 1; i <= k; ++i) { for (int j = k; j >= 0; --j) { dp[i + 1][j] = (dp[i + 1][j] + dp[i][j] * p) % mod; if (j - i >= i) { dp[i][j - i] = (dp[i][j - i] + dp[i][j] * q) % mod; } if (j - i >= 1) dp2[i][j - i] = (dp2[i][j - i] + dp[i][j] * q) % mod; if (i > j) { long long used = (k - j + i - 1) * 1LL * dp2[i][j] % mod; used = (used * p) % mod; ans = (ans + used * iq) % mod; ans = (ans + cur * dp2[i][j]) % mod; } else if (i == j) { long long used = (k - j + i - 1) * 1LL * dp[i][j] % mod; used = (used * p) % mod; ans = (ans + used * iq) % mod; ans = (ans + cur * dp[i][j]) % mod; } } } ans = (ans * q) % mod; ans = (ans * ip) % mod; ans %= mod; if (ans < 0) ans += mod; cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using ll = long long; using namespace std; template <typename... Args> void read(Args &...args) { ((cin >> args), ...); } template <typename... Args> void write(Args... args) { ((cout << args << " "), ...); } template <typename... Args> void writeln(Args... args) { ((cout << args << " "), ...); cout << "\n"; } template <typename T> void read(vector<T> &a) { for (auto &ele : a) cin >> ele; } template <typename T> void writeln(vector<T> &a) { for (auto &ele : a) cout << ele << ' '; cout << "\n"; } const pair<int, int> dxy[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; const pair<int, int> fxy[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}}; void solve() { ll n; read(n); ll res = 0; while (n > 0) { if (n % 4 == 0 && n != 4) { res++; n -= 1; } else if (n % 2 == 0) { n /= 2; res += n; } else { res++; n -= 1; } if (n % 4 == 0 && n != 4) { n -= 1; } else if (n % 2 == 0) { n /= 2; } else { n -= 1; } } writeln(res); } int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t--) solve(); return 0; }
7
CPP
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; string input; long long int kansu( long long int x ) { vector< long long int > list; long long int k = 0; while( true ) { if ( input[x] == '[' ) { list.push_back( kansu(x+1) ); long long int f = 0; while( true ) { if ( input[x] == '[' ) f++; if ( input[x] == ']' ) { f--; if ( f == 0 ) break; } x++; } x++; if ( x == input.size() ) break; }else if ( input[x] == ']' ) { if ( k > 0 ) list.push_back( k / 2 + 1 ); break; }else { k = k * 10 + input[x] - '0'; x++; } } sort( list.begin(), list.end() ); long long int ans = 0; for ( long long int i = 0; i < list.size() / 2 + 1; i++ ) { ans += list[i]; } return ans; } int main () { long long int n; cin >> n; for ( long long int i = 0; i < n; i++ ) { cin >> input; cout << kansu( 0 ) << endl; } return 0; }
0
CPP
import logging import sys from collections import Counter from inspect import currentframe # sys.setrecursionlimit(10 ** 6) #Pypyだと256MB上限を超える input = sys.stdin.readline logging.basicConfig(level=logging.DEBUG) def dbg(*args): id2names = {id(v): k for k, v in currentframe().f_back.f_locals.items()} logging.debug(", ".join(id2names.get(id(arg), "???") + " = " + repr(arg) for arg in args)) def solve(): n, m = map(int, input().split()) a = list(map(int, input().split())) ra = [ai % m for ai in a] c = Counter(ra) ans = 0 for k, v in c.items(): if k == 0: ans += 1 elif k * 2 == m: ans += 1 elif not m - k in c: ans += v elif m - k in c: if k >= m - k: # doublecount continue if abs(v - c[m - k]) <= 1: ans += 1 else: ans += abs(v - c[m - k]) return ans def main(): t = int(input()) for _ in range(t): print(solve()) if __name__ == "__main__": main()
8
PYTHON3
from fractions import Fraction from collections import defaultdict import math import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") #----------------------------------------Code Starts Here--------------------------------------------# # def isPrime(n): # f = 0 # i = 2 # while(i*i <= n+1): # if(n % i == 0): # f = 1 # break # if(f == 0): # return True # else: # return False t = int(input()) for _ in range(t): n = int(input()) if(n < 31): print('NO') else: l = n-30 if(l == 6 or l == 10 or l == 14): print('YES') print(6, 10, 15, l-1) else: print('YES') print(6, 10, 14, l)
7
PYTHON3
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=200005; typedef long long ll; int n,m,V,E,cnt[3],col[N]; bool vis[N],vise[N*2],colored; ll ans; struct node{ int u,v,w,nxt; }edge[N*2]; int head[N],mcnt=1; void add_edge(int u,int v,int w){ mcnt++; edge[mcnt].u=u; edge[mcnt].v=v; edge[mcnt].w=w; edge[mcnt].nxt=head[u]; head[u]=mcnt; } void dfs(int u) { V++; vis[u]=true; cnt[col[u]]++; for(int i=head[u];i;i=edge[i].nxt){ if(vise[i|1]) continue ; vise[i|1]=1,E++; int v=edge[i].v,w=edge[i].w; if(!vis[v]){ col[v]=(col[u]+w+3)%3; dfs(v); } else if((col[u]+w+3)%3!=col[v]) colored=true; } } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); add_edge(x,y,1); add_edge(y,x,-1); } for(int i=1;i<=n;i++) if(!vis[i]) { V=E=cnt[0]=cnt[1]=cnt[2]=colored=0; dfs(i); if(colored) ans+=1ll*V*V; else if(!cnt[0]||!cnt[1]||!cnt[2]) ans+=1ll*E; else ans+=1ll*cnt[0]*cnt[1]+1ll*cnt[1]*cnt[2]+1ll*cnt[0]*cnt[2]; } printf("%lld\n",ans); }
0
CPP
n = input() a = [int(t) for t in input().split()] s = set() for x in a: s.add(x) t = {0} if s | t == t: print(0) elif s & t == t: print(len(s) - 1) else: print(len(s))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100100; struct point { int v, ii; } a[N]; bool cmp(point ca, point cb) { return ca.v < cb.v; } int main() { int n, sum(0); scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i].v), a[i].ii = i, sum += a[i].v; sort(a + 2, a + n + 1, cmp); int now(a[1].v); if (2 * now > sum) { printf("1\n1"); return 0; } for (int i = 2; i <= n && 2 * a[i].v <= a[1].v; ++i) { now += a[i].v; if (2 * now > sum) { printf("%d\n", i); for (int j = 1; j <= i; ++j) printf("%d ", a[j].ii); return 0; } } printf("0"); return 0; }
7
CPP
#include <iostream> #include <stack> using namespace std; int main(){ int n; stack<int> st; while(cin>>n){ if(n){ st.push(n); } else { cout<<st.top()<<endl; st.pop(); } } return 0; }
0
CPP
#!/usr/bin/env python # -*- coding: utf-8 -*- n,k = map(int,input().split()) M = [list( map(int,input().split()))[1:] for i in range(k)] matryoshkas = 0 for i in range(k): if M[i][0] == 1: for j in range(len(M[i])-1): if M[i][j] + 1 != M[i][j+1]: matryoshkas += len(M[i]) - j - 1 break else: matryoshkas += len(M[i]) - 1 print(2 * matryoshkas + k - 1)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); long long n; cin >> n; long long a, b; cin >> a >> b; b = min(b, n); vector<pair<long long, long long> > creature(n); long long total_beg = 0; for (long long i = 0; i < n; i++) { cin >> creature[i].first >> creature[i].second; total_beg += creature[i].second; } vector<long long> poss(n); long long null = 0; for (long long i = 0; i < n; i++) { poss[i] = max(null, creature[i].first - creature[i].second); } sort(poss.begin(), poss.end()); reverse(poss.begin(), poss.end()); if (b == 0) { cout << total_beg << "\n"; return 0; } long long total_plus = 0; for (long long i = 0; i + 1 < b; i++) { total_plus += poss[i]; } long long max_total = total_plus + total_beg + poss[b - 1]; for (long long i = 0; i < n; i++) { long long new_total = total_plus + total_beg; long long new_health = (1 << a) * creature[i].first; if (creature[i].first - creature[i].second > poss[b - 1]) { new_total += min(-creature[i].first + creature[i].second, null) + poss[b - 1]; } new_total += new_health - creature[i].second; max_total = max(max_total, new_total); } cout << max_total << "\n"; }
11
CPP
a=input() b=input() c=sorted(input()) if sorted(a+b)==c: print('YES') else: print('NO')
7
PYTHON3
H, W = map(int, input().split()) N = int(input()) A = list(map(int, input().split())) colors = [] for i in range(1, N + 1): colors += [i] * A[i - 1] for i in range(H): if i % 2 == 0: print(*colors[i * W : (i + 1) * W]) else: print(*colors[i * W : (i + 1) * W][::-1])
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, i, j, k, a[1000005], b[1000005], c[2], cnt, p[1000005]; int t[1000005 * 4][2]; void maketree(int x, int l, int r) { if (l == r) { t[x][0] = t[x][1] = 0, t[x][p[l]] = l; return; } int mid = (l + r) >> 1; maketree(x << 1, l, mid), maketree(x << 1 ^ 1, mid + 1, r); t[x][0] = (t[x << 1][0]) ? t[x << 1][0] : t[x << 1 ^ 1][0]; t[x][1] = (t[x << 1][1]) ? t[x << 1][1] : t[x << 1 ^ 1][1]; } void change(int x, int l, int r, int v) { if (l == r) { t[x][0] = t[x][1] = 0; return; } int mid = (l + r) >> 1; if (v <= mid) change(x << 1, l, mid, v); else change(x << 1 ^ 1, mid + 1, r, v); t[x][0] = (t[x << 1][0]) ? t[x << 1][0] : t[x << 1 ^ 1][0]; t[x][1] = (t[x << 1][1]) ? t[x << 1][1] : t[x << 1 ^ 1][1]; } int find(int x, int l, int r, int L, int R, int v) { if (l > R || r < L) return 0; if (L <= l && r <= R) return t[x][v]; int mid = (l + r) >> 1, tmp = find(x << 1, l, mid, L, R, v); if (tmp) return tmp; else return find(x << 1 ^ 1, mid + 1, r, L, R, v); } int main() { scanf("%d", &n); char ch = getchar(); while (ch < '0' || ch > '1') ch = getchar(); for (i = 1; i <= n; i++) a[i] = ch - '0', ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); for (i = 1; i <= n; i++) b[i] = ch - '0', ch = getchar(); for (i = 1; i <= n; i++) if (a[i] != b[i]) p[++cnt] = a[i] > b[i]; int res = 0, ans = 0; for (i = 1; i <= cnt; i++) res += p[i] ? 1 : -1; if (res != 0) printf("-1"), exit(0); if (!cnt) printf("0"), exit(0); maketree(1, 1, cnt); res = cnt; while (res) { ans++; int x = find(1, 1, cnt, 1, cnt, 0), y = find(1, 1, cnt, 1, cnt, 1), v, now = max(x, y); change(1, 1, cnt, x), change(1, 1, cnt, y), res -= 2; if (x > y) v = 1; else v = 0; if (!res) break; x = find(1, 1, cnt, now + 1, cnt, v); while (x) { y = find(1, 1, cnt, x + 1, cnt, v ^ 1); if (y) { change(1, 1, cnt, x), change(1, 1, cnt, y), res -= 2; now = y; if (!res) break; x = find(1, 1, cnt, now + 1, cnt, v); } else break; } } printf("%d\n", ans); }
11
CPP
#include<iostream> #include<set> using namespace std; multiset<int, greater<int>> a; int64_t n,m,b,k; int main() { cin>>n>>m; while(n--)cin>>k,a.insert(k); while(m--)a.insert((*a.begin())/2),a.erase(a.begin()); for(auto c:a)b+=c; cout<<b; }
0
CPP
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools import random sys.setrecursionlimit(10**7) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def main(): n = I() s = [c for c in S()] t = [c for c in S()] r = 0 for i in range(n): if s[i] == t[i]: continue r += 1 if i < n-1 and s[i] == t[i+1] and s[i+1] == t[i]: s[i+1] = '0' t[i+1] = '0' return r print(main())
9
PYTHON3
def isDistinct(n): n = str(n) used = [] for i in n: if i in used: return False used.append(i) return True n = int(input()) n+=1 while True: if isDistinct(n): print(n) break n+=1
7
PYTHON3
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) a1 = list(map(abs, a)) for i in range(0, len(a), 2): a1[i] = -a1[i] print(*a1)
7
PYTHON3
#include <bits/stdc++.h> #define ref(i,n) for(int i=1;i<=n;i++) using namespace std; #define INF 111111111111 #define mod 998244353 typedef long long ll; typedef pair<int,int> P; int n,m; int v[1000000],w[1000000]; int dp[512][100001]; int dfs(int i,int k){ if(i<=511) return dp[i][k]; else if(w[i]<=k) return max(dfs(i/2,k),dfs(i/2,k-w[i])+v[i]); else return dfs(i/2,k); } int main() { cin>>n; ref(i,n){ scanf("%d %d",&v[i],&w[i]); } ref(i,511)ref(j,100000){ if(j<w[i]) dp[i][j]=dp[i/2][j]; else dp[i][j]=max(dp[i/2][j],dp[i/2][j-w[i]]+v[i]); } int q; cin>>q; ref(i,q) { int p,l; scanf("%d %d",&p,&l); printf("%d\n",dfs(p,l)); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long zz, zo, oz, oo; void init() { cin >> zz >> zo >> oz >> oo; } vector<long long> pt; void process() { if (zz == 0 && zo == 0 && oz == 0 && oo == 0) { puts("1"); return; } for (long long i = 0; i <= 1000000; i++) { pt.push_back(i * (i - 1) / 2); } if (find(pt.begin(), pt.end(), zz) == pt.end() || find(pt.begin(), pt.end(), oo) == pt.end()) { puts("Impossible"); return; } long long nz = find(pt.begin(), pt.end(), zz) - pt.begin(); long long no = find(pt.begin(), pt.end(), oo) - pt.begin(); if (nz <= 1) { nz = (oz + zo != 0) ? 1 : 0; } if (no <= 1) { no = (oz + zo != 0) ? 1 : 0; } if (zo + oz != no * nz) { puts("Impossible"); return; } vector<char> ans; while (no != 0 || nz != 0) { if (oz >= nz) ans.push_back('1'), oz -= nz, no--; else ans.push_back('0'), nz--; } for (char xx : ans) putchar(xx); puts(""); } int main() { init(); process(); return 0; }
8
CPP
import sys t = int(input()) for i in range(t): n, k = map(int, sys.stdin.readline().split()) s = "" for a in range(n): s+= chr(ord("a")+(a%k)) print(s)
7
PYTHON3
#include <bits/stdc++.h> int main() { int i, j, a = 0, b = 0, d, e, k, l, d1; char s[1000], c[1000], f[1000]; scanf("%s%s", s, c); j = strlen(s); e = strlen(c); for (i = 0; i < j; i++) { if (s[i] == '|') break; b++; } a = j - b - 1; d = a - b; if (d < 0) d = -d; if (((a == b || (a % 2 == 0 && b % 2 == 0)) && (e % 2 != 0)) || (a != b && d > e) || (d % 2 == 0 && e % 2 != 0) || (d % 2 != 0 && e % 2 == 0)) { printf("Impossible"); exit(0); } else { if (b > a) d1 = e - d - (e - d) / 2; else if (a > b) d1 = d + (e - d) / 2; else d1 = e / 2; for (i = 0, k = 0, l = 0; i < (j + e); i++) { if (i < d1) { f[i] = c[l]; l++; } else if (i >= d1 && i < d1 + j) { f[i] = s[k]; k++; } else { f[i] = c[l]; l++; } } f[i] = '\0'; } printf("%s", f); return 0; }
7
CPP
''' def main(): from sys import stdin,stdout if __name__=='__main__': main() ''' #349B ''' def main(): from sys import stdin,stdout N = int(stdin.readline()) arr = list(map(int,stdin.readline().split())) div = [] for i in arr: div.append(N//i) maxim = 0 maxindex = -1 for i in range(9): if div[i] >maxim: maxim = div[i] maxindex = i if maxindex > -1: ans = [ (maxindex+1) for i in range(maxim)] N= N%arr[maxindex] #print(N) i = 0 while i<maxim: #print('i=',i,'N=',N) for j in range(8,maxindex,-1): #print('j=',j,'diff=',arr[j]-arr[ans[i]-1]) if arr[j]-arr[ans[i]-1] <=N: N -= arr[j]-arr[ans[i]-1] ans[i] = j+1 break i+=1 for i in ans: stdout.write(str(i)) else: stdout.write('-1\n') if __name__=='__main__': main() ''' #234B Input and Output ''' def main(): from sys import stdin,stdout import collections with open('input.txt','r') as ip: N,K = map(int,ip.readline().split()) arr = list(map(int,ip.readline().split())) mydict = collections.defaultdict(set) for i in range(len(arr)): mydict[arr[i]].add(i+1) ans = [] i=0 while K>0: for it in mydict[sorted(mydict.keys(),reverse=True)[i]]: ans.append(it) K-=1 if K<1: break minim=i i+=1 with open('output.txt','w') as out: out.write(str(sorted(mydict.keys(),reverse=True)[minim])+'\n') ans=' '.join(str(x) for x in ans) out.write(ans+'\n') if __name__=='__main__': main() ''' #151B ''' def main(): from sys import stdin,stdout import collections names = collections.defaultdict(list) counter = 0 order = {} for i in range(int(stdin.readline())): n,ns = stdin.readline().split() names[ns]=[0,0,0] order[ns]=counter counter+=1 n=int(n) while n: ip=stdin.readline().strip() ip=ip.replace('-','') #test for taxi flag=True for i in range(1,6): if ip[i]!=ip[0]: flag=False break if flag: names[ns][0]+=1 n-=1 continue #test for pizza flag = True for i in range(1,6): if int(ip[i])>=int(ip[i-1]): flag =False break if flag: names[ns][1]+=1 else: names[ns][2]+=1 n-=1 #print(names) #for all girls t=-1 p=-1 g=-1 for i in names: t=max(t,names[i][0]) p = max(p, names[i][1]) g = max(g, names[i][2]) taxi=list(filter(lambda x: names[x][0]==t, names.keys())) pizza = list(filter(lambda x: names[x][1] == p, names.keys())) girls = list(filter(lambda x: names[x][2] == g, names.keys())) pizza.sort(key= lambda x: order[x]) taxi.sort(key= lambda x: order[x]) girls.sort(key= lambda x: order[x]) print('If you want to call a taxi, you should call:',', '.join(x for x in taxi),end='.\n') print('If you want to order a pizza, you should call:', ', '.join(x for x in pizza),end='.\n') print('If you want to go to a cafe with a wonderful girl, you should call:', ', '.join(x for x in girls),end='.\n') if __name__=='__main__': main() ''' #SQUADRUN Q2 ''' def LCMgen(a): import math lcm = a[0] for i in range(1,len(a)): g = math.gcd(lcm,a[i]) lcm = (lcm*a[i])//g return lcm def main(): from sys import stdin,stdout import collections import math N,W = map(int,stdin.readline().split()) counter = collections.Counter(map(int,stdin.readline().split())) lcm = LCMgen(list(counter.keys())) W*=lcm div = 0 for i in counter: div+=counter[i]*(lcm//i) ans = math.ceil(W/div) stdout.write(str(ans)) if __name__=='__main__': main() ''' #143B ''' def main(): from sys import stdin,stdout ip = stdin.readline().strip() inte = None flow = None for i,j in enumerate(ip): if j=='.': flow = ip[i:] inte = ip[:i] break if flow == None: flow = '.00' inte = ip else: if len(flow)==2: flow+='0' else: flow = flow[:3] ne = False if ip[0]=='-': ne = True if ne: inte = inte[1:] inte = inte[::-1] ans ='' for i,j in enumerate(inte): ans += j if i%3 == 2: ans+=',' ans = ans[::-1] if ans[0]==',': ans = ans[1:] ans = '$'+ans if ne: stdout.write('({})'.format(ans+flow)) else: stdout.write(ans+flow) if __name__=='__main__': main() ''' #A ''' def main(): from sys import stdin,stdout n = int(stdin.readline()) arr = list(map(int,stdin.readline().split())) minim = min(arr) my_l = [] for i,j in enumerate(arr): if j==minim: my_l.append(i) my_l_ = [] for i in range(1,len(my_l)): my_l_.append(my_l[i]-my_l[i-1]) stdout.write(str(min(my_l_))) if __name__=='__main__': main() ''' #B ''' def main(): from sys import stdin,stdout n,a,b = map(int,stdin.readline().split()) maxim = -1 for i in range(1,n): maxim = max(min(a//i,b//(n-i)),maxim) stdout.write(str(maxim)) if __name__=='__main__': main() ''' #233B ''' def main(): from sys import stdin,stdout def foo(x): tsum = 0 c = x while c: tsum+=(c%10) c//=10 return tsum N = int(stdin.readline()) up,down = 0 , int(1e18) flag = False while up<down: mid = (up+down)//2 val = foo(mid) val = (mid+val)*mid if val<N: up = mid elif val >N: down = mid else: flag=True break if flag: stdout.write(str(mid)+'\n') else: stdout.write('-1') if __name__=='__main__': main() def main(): def foo(x): n= x tsum = 0 while n: tsum += n%10 n//=10 return x*x + tsum*x - int(1e18) import matplotlib.pyplot as plt y = [foo(x) for x in range(1,int(1e18)+1)] x = range(1,int(1e18)+1) print(y[:100]) plt.plot(y,x) plt.show() if __name__=='__main__': main() ''' #RECTANGL ''' def main(): from sys import stdin,stdout import collections for _ in range(int(stdin.readline())): c = collections.Counter(list(map(int,stdin.readline().split()))) flag = True for i in c: if c[i]&1: flag=False if flag: stdout.write('YES\n') else: stdout.write('NO\n') if __name__=='__main__': main() ''' #MAXSC ''' def main(): from sys import stdin,stdout import bisect for _ in range(int(stdin.readline())): N = int(stdin.readline()) mat = [] for i in range(N): mat.append(sorted(map(int,stdin.readline().split()))) ## print(mat) temp = mat[-1][-1] tsum = mat[-1][-1] flag = True for i in range(N-2,-1,-1): ind = bisect.bisect_left(mat[i],temp)-1 if ind == -1: flag = False break else: tsum+=mat[i][ind] if flag: stdout.write(str(tsum)+'\n') else: stdout.write('-1\n') if __name__=='__main__': main() ''' #233B ******************** def main(): def rev(x): tsum = 0 while x: tsum += x%10 x//=10 return tsum from sys import stdin,stdout from math import sqrt,ceil n = int(stdin.readline()) for i in range(91): r = i*i+(n<<2) x = ceil(sqrt(r)) ## print(i,x) if x*x == r: num = (x-i)/2 if num == int(num): if rev(num)==i: stdout.write(str(int(num))) return stdout.write('-1') if __name__=='__main__': main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; double EPS = 1e-9; double PI = acos(-1); long long dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1}; long long diry[8] = {0, 1, -1, 0, -1, 1, -1, 1}; const long long INF = 1e18; const long long N = 100005; long long mod = 1e9 + 7; void solve() { long long n, m; cin >> n >> m; long long a[n][m]; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { cin >> a[i][j]; } } if (a[0][0] > 2 || a[0][m - 1] > 2 || a[n - 1][0] > 2 || a[n - 1][m - 1] > 2) { cout << "NO" << "\n"; return; } a[0][0] = 2; a[0][m - 1] = 2; a[n - 1][0] = 2; a[n - 1][m - 1] = 2; long long temp = 1; for (long long j = 1; j < (m - 1); j++) { if (a[0][j] > 3) { temp = 0; break; } a[0][j] = 3; if (a[n - 1][j] > 3) { temp = 0; break; } a[n - 1][j] = 3; } for (long long j = 1; j < (n - 1); j++) { if (a[j][0] > 3) { temp = 0; break; } a[j][0] = 3; if (a[j][m - 1] > 3) { temp = 0; break; } a[j][m - 1] = 3; } for (long long i = 1; i < (n - 1); i++) { for (long long j = 1; j < (m - 1); j++) { if (a[i][j] > 4) { temp = 0; break; } a[i][j] = 4; } } if (temp == 0) { cout << "NO" << "\n"; return; } cout << "YES" << "\n"; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { cout << a[i][j] << " "; } cout << "\n"; } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; for (long long tc = 1; tc <= t; tc++) { solve(); } return 0; }
8
CPP
#1,5,10,20,100 n=int(input()) t=0 while(n>0): if(n>=100): n-=100 t+=1 elif(n>=20): n-=20 t+=1 elif(n>=10): n-=10 t+=1 elif(n>=5): n-=5 t+=1 else: n-=1 t+=1 print(t)
7
PYTHON3
N,K,*x =map(int,open(0).read().split()) print(sum(2*[min(i,K-i) for i in x]))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxm = 200000; int n, dis, m; map<int, int> Map; map<int, int>::iterator it; pair<int, int> gas[maxm + 10]; void init() { cin >> dis >> n >> m; for (int i = 1; i <= m; ++i) scanf("%d%d", &gas[i].first, &gas[i].second); sort(gas + 1, gas + 1 + m); Map[0] = n; long long ans = 0; gas[0].first = 0; gas[m + 1].first = dis; for (int i = 1; i <= m + 1; ++i) { int del = gas[i].first - gas[i - 1].first; int tmp = del; if (del > n) { printf("-1\n"); return; } while (del) { it = Map.begin(); if (del >= it->second) { del -= it->second; ans += (long long)it->first * it->second; Map.erase(it); } else { ans += (long long)it->first * del; it->second -= del; del = 0; } } if (i == m + 1) break; if (!Map.count(gas[i].second)) Map[gas[i].second] = tmp; else Map[gas[i].second] += tmp; while (1) { it = Map.end(); --it; if (it->first <= gas[i].second) break; Map[gas[i].second] += it->second; Map.erase(it); } } cout << ans << "\n"; return; } int main() { init(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 50 + 1; const long long lim = (long long)(1e18) + 5, oo = 3e18; vector<long long> fact(3, 1), dp(N, -1); long long mul(long long a, long long b) { if (lim / a <= b) return oo; a *= b; if (a >= lim) return oo; return a; } long long add(long long a, long long b) { a += b; if (a >= lim) return oo; return a; } long long calc(int a) { if (a == 0) return 1; if (dp[a] != -1) return dp[a]; dp[a] = 0; for (int i = 1; i <= a; i++) dp[a] = add(dp[a], mul(calc(a - i), fact[i])); return dp[a]; } vector<int> ans; int chainBase[N], baseofChain[N]; void dodaj(int offset, int vel, long long k) { ans.push_back(vel + offset); for (int i = 0; i < vel; i++) chainBase[i] = baseofChain[i] = i; vector<bool> taken(vel); for (int i = 1; i < vel; i++) for (int j = 0; j < vel - 1; j++) if (!taken[j] && chainBase[i] != j) { if (k <= fact[vel - i]) { ans.push_back(offset + j + 1); baseofChain[chainBase[i]] = baseofChain[j]; chainBase[baseofChain[j]] = chainBase[i]; taken[j] = 1; break; } k -= fact[vel - i]; } } int n; void gen(int a, long long k) { if (a == 0) return; for (int i = 1; i <= a; i++) { long long drugo = calc(a - i); long long imam = mul(drugo, fact[i]); if (imam >= k) { long long koliko = (k - 1) / drugo; dodaj(n - a, i, koliko + 1); k -= koliko * drugo; gen(a - i, k); return; } k -= imam; } } int main() { for (int i = 1; i < N; i++) fact.push_back(mul(fact.back(), i)); int t; scanf("%i", &t); while (t--) { long long k; scanf("%i %lld", &n, &k); if (calc(n) < k) { printf("-1\n"); continue; } ans.clear(); gen(n, k); for (auto p : ans) printf("%i ", p); printf("\n"); } return 0; }
11
CPP
#include<bits/stdc++.h> using namespace std; inline int sqr(int x){ return x*x; } int main(){ int n,a[101]; cin>>n; int ans=0; for(int i=1;i<=n;i++){ cin>>a[i]; ans+=sqr(a[i]); } for(int x=-100;x<=100;x++){\ int tmp=0; for(int i=1;i<=n;i++){ tmp+=sqr(a[i]-x); } ans=min(ans,tmp); } cout<<ans; }
0
CPP
def solve(): num = input() past_touch = (int(num[0]) - 1) * 10 actual_touch = (1 + len(num)) * len(num) // 2 print(past_touch + actual_touch) tests = int(input()) while tests > 0: solve() tests -= 1
7
PYTHON3
a = [ int(x) for x in input().split() ] n = a[0] for i in range( a[1] ): if n % 10 != 0: n -= 1 elif n % 10 == 0: n //= 10 print(n)
7
PYTHON3
s1=input().lower() s2=input().lower() d={} for i in range(len(s1)): d[s1[i]]=s2[i] s1=s1.upper() s2=s2.upper() for i in range(len(s1)): d[s1[i]]=s2[i] inp=input() ans="" for x in inp: if(x in d): ans=ans+d[x] else: ans=ans+x print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const double Pi = acos((double)-1); const long long oo((long long)1e+18 + 100); template <typename T> inline ostream &operator<<(ostream &out, const vector<T> &v) { for (auto &it : v) out << it << ' '; return out; } template <typename T> inline istream &operator>>(istream &in, vector<T> &v) { for (auto &it : v) in >> it; return in; } template <typename T1, typename T2> inline ostream &operator<<(ostream &out, const pair<T1, T2> &p) { out << p.first << ' ' << p.second << ' '; return out; } template <typename T1, typename T2> inline istream &operator>>(istream &in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; } template <typename T> inline ostream &operator<<(ostream &out, const set<T> &p) { for (auto &i : p) out << i << ' '; return out; } template <typename T> inline istream &operator>(istream &in, T &p) { return in >> p; } template <typename T> inline ostream &operator<(ostream &out, const T &p) { return out << p; } template <typename T1, typename T2> inline pair<T1 &, T2 &> unzip_pair(T1 &p1, T2 &p2) { return pair<T1 &, T2 &>(p1, p2); } int snm[212345]; int getV(int v) { if (snm[v] != v) snm[v] = getV(snm[v]); return snm[v]; } void merge(int a, int b) { snm[getV(a)] = getV(b); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout < fixed < setprecision(7); for (int i = 0; i < (212345); i++) snm[i] = i; int n; cin > n; for (int i = 0; i < (n); i++) { int a; cin > a; a--; merge(i, a); } set<int> m; for (int i = 0; i < (n); i++) m.insert(getV(i)); int a = 0; for (int i = 0; i < (n); i++) { int b; cin > b; a += b; } int ans = m.size(); if (ans == 1) ans = 0; if (!(a & 1)) ans++; cout < ans; return 0; }
7
CPP
#include <iostream> #include <cstring> using namespace std; int main(){ char str[101]; char ans[101]; while(cin >> str){ int len=strlen(str); int n=0; for(int i=0;i<len;i++){ if(str[i]!='@') ans[n++]=str[i] ; else if(str[i]=='@') { char j=str[++i]; char ch=str[++i] ; for(int k=0;k<j-'0';k++) ans[n++]=ch; }; }; ans[n]='\0'; cout << ans << '\n' ; }; }
0
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> arr; void init() { vector<int> curr(10, 0); while (true) { int i = 0; while (curr[i] == 7 and i < 10) { i++; } if (i == 10) break; if (curr[i] == 4) curr[i] = 7; else curr[i] = 4; for (int a = 0; a < i; a++) { curr[a] = 4; } long long num = 0; for (int i = 0; i < 10; i++) { num += pow(10, i) * curr[i]; } arr.push_back(num); } } long long next(long long c) { if (binary_search(arr.begin(), arr.end(), c)) return c; long long ans = *upper_bound(arr.begin(), arr.end(), c); return ans; } int main() { init(); unsigned long long l, r; cin >> l >> r; unsigned long long ans = 0; while (l <= r) { unsigned long long n = next(l); ans += n * (min(r, n) - l + 1); l = n + 1; } cout << ans << "\n"; }
9
CPP
n,k=map(int,input().split()) participants=list(map(int, input().split())) count=0 k-=1 i=0 while (i<n and participants[i]>=participants[k]): if participants[i]>0: count+=1 else: break i+=1 print(count)
7
PYTHON3
n = int(input()) for i in range(2, 101): if n % i == 0: print(str(i)+str(n // i)) break
8
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int N = 5005; int n; int a[N], b[N], c[N]; int f[N][N]; int dp(int i, int j) { if (f[i][j] != -1) return f[i][j]; if (i == j) return f[i][j] = 0; if (c[i] == c[j]) { if (j == i + 1) return f[i][j] = 0; else return dp(i + 1, j - 1) + 1; } return f[i][j] = min(dp(i + 1, j) + 1, dp(i, j - 1) + 1); } int main() { cin >> n; memset(f, -1, sizeof(f)); for (int i = 1; i <= n; i++) cin >> a[i]; int cnt = 0, t = 0; for (int i = 1; i <= n; i++) if (a[i] != a[i - 1]) { if (!b[a[i]]) b[a[i]] = ++t; c[++cnt] = b[a[i]]; } cout << dp(1, cnt); return 0; }
10
CPP
N=int(input()) A=list(map(int,input().split())) r=1 for a in A: if a%2==0 and a%3!=0 and a%5!=0: r=0 print(['DENIED','APPROVED'][r])
0
PYTHON3
a = input() b = input() c = input() a = a.split() a = list(map(int, a)) b = b.split() b = list(map(int, b)) c = c.split() c = list(map(int, c)) a = [i % 2 for i in a] b = [i % 2 for i in b] c = [i % 2 for i in c] d = [1, 1, 1, 1, 1, 1, 1, 1, 1] if a[0] == 1: d[0] += 1 d[1] += 1 d[3] += 1 if a[1] == 1: d[0] += 1 d[1] += 1 d[2] += 1 d[4] += 1 if a[2] == 1: d[1] += 1 d[2] += 1 d[5] += 1 if b[0] == 1: d[0] += 1 d[3] += 1 d[4] += 1 d[6] += 1 if b[1] == 1: d[1] += 1 d[3] += 1 d[4] += 1 d[5] += 1 d[7] += 1 if b[2] == 1: d[2] += 1 d[4] += 1 d[5] += 1 d[8] += 1 if c[0] == 1: d[3] += 1 d[6] += 1 d[7] += 1 if c[1] == 1: d[4] += 1 d[6] += 1 d[7] += 1 d[8] += 1 if c[2] == 1: d[5] += 1 d[7] += 1 d[8] += 1 d = [i % 2 for i in d] d = list(map(str, d)) print(d[0] + d[1] + d[2]) print(d[3] + d[4] + d[5]) print(d[6] + d[7] + d[8])
7
PYTHON3
n=int(input()) li=input().split() for i in range(0,len(li)): li[i]=int(li[i]) li.sort() sum1=0 p1=1 sum2=0 p2=2 for i in li: sum1+=abs(p1-i) sum2+=abs(p2-i) p1+=2 p2+=2 print(min(sum1,sum2))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, C; vector<vector<int> > W; vector<pair<int, int> > seg; void dfs(int s, int e, int col) { if (s > e) return; if (W[s].size() == col) { dfs(s + 1, e, col); return; } for (int i = s; i <= e; i++) { if (W[i].size() == col) { seg.push_back(pair<int, int>(0, C - 1)); return; } } int minp = s; for (int i = s; i < e; i++) { if (W[i][col] > W[i + 1][col]) { minp = i + 1; break; } } int prev = W[minp][col]; for (int i = minp + 1; i <= e; i++) { if (prev > W[i][col]) { seg.push_back(pair<int, int>(0, C - 1)); return; } prev = W[i][col]; } for (int i = s; i < minp; i++) { if (prev > W[i][col]) { seg.push_back(pair<int, int>(0, C - 1)); return; } prev = W[i][col]; } if (minp == s) seg.push_back(pair<int, int>(C + 1 - W[e][col], C - W[s][col])); else { seg.push_back(pair<int, int>(0, C - W[s][col])); seg.push_back(pair<int, int>(C + 1 - W[e][col], C - 1)); } prev = s; for (int i = s; i <= e; i++) { if (i == e || W[i][col] != W[i + 1][col]) { dfs(prev, i, col + 1); prev = i + 1; } } } int main() { scanf("%d %d", &N, &C); W.resize(N); for (int i = 0; i < N; i++) { int len; scanf("%d", &len); W[i].resize(len); for (int j = 0; j < len; j++) { scanf("%d", &W[i][j]); } } dfs(0, N - 1, 0); sort(seg.begin(), seg.end()); int pos = 0; int mx = -1; for (int i = 0; i < C; i++) { while (pos < seg.size() && seg[pos].first <= i) { mx = max(mx, seg[pos].second); pos++; } if (mx < i) { printf("%d", i); return 0; } } printf("-1"); }
10
CPP
[a, b] = map(int, input().split()) aw = 0 bw = 0 d = 0 if abs(a - 1) > abs(b - 1): aw += 1 elif abs(a - 1) < abs(b - 1): bw += 1 else: d += 1 if abs(a - 2) > abs(b - 2): aw += 1 elif abs(a - 2) < abs(b - 2): bw += 1 else: d += 1 if abs(a - 3) > abs(b - 3): aw += 1 elif abs(a - 3) < abs(b - 3): bw += 1 else: d += 1 if abs(a - 4) > abs(b - 4): aw += 1 elif abs(a - 4) < abs(b - 4): bw += 1 else: d += 1 if abs(a - 5) > abs(b - 5): aw += 1 elif abs(a - 5) < abs(b - 5): bw += 1 else: d += 1 if abs(a - 6) > abs(b - 6): aw += 1 elif abs(a - 6) < abs(b - 6): bw += 1 else: d += 1 print(bw, d, aw)
7
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int maxx = 1e5 + 10; const int inf = 0x3f3f3f3f; int mx[] = {-1, 1, 0, 0}, my[] = {0, 0, -1, 1}; int ch[maxx]; int solve(int n) { for (int i = 1; i < n; i++) { if (ch[i] != ch[i - 1]) return 0; } return 1; } void input(int n) { for (int i = 0; i < n; i++) { scanf("%d", &ch[i]); if (!ch[i]) continue; while (ch[i] % 2 == 0) ch[i] /= 2; while (ch[i] % 3 == 0) ch[i] /= 3; } } int main() { int n; while (~scanf("%d", &n)) { input(n); if (solve(n)) printf("Yes\n"); else printf("No\n"); } }
9
CPP
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; long long ans = 1; int i = 0, j, z, n = (int)(s).size(); while (i < n) { z = 1; while (i + 1 < n && s[i] - '0' + s[i + 1] - '0' == 9) i++, z++; if (z > 1 && z % 2 == 1) ans *= ((z + 1) / 2); i++; } cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; string ans; cin >> s; for (int i = 1; i < s.size(); i++) { if (s[i] == s[i - 1] && i != s.size() - 1) { if (s[i - 1] != 'a' && s[i + 1] != 'a') { s[i] = 'a'; } else if (s[i - 1] != 'b' && s[i + 1] != 'b') { s[i] = 'b'; } else { s[i] = 'c'; } } else if (s[i] == s[i - 1] && i == s.size() - 1) { if (s[i] != 'a') { s[i] = 'a'; } else { s[i] = 'b'; } } } cout << s; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * f; } const int MAXN = 200010; const int INF = 2147483600; const long long Mod = 998244353LL; int N; vector<int> vec[MAXN + 1]; long long diedbef[MAXN + 1], diedaft[MAXN + 1], Alive[MAXN + 1], diedFa[MAXN + 1]; inline void dfs(int k, int Fa) { bool pas = 0; Alive[k] = 1; for (int i = 0; i < vec[k].size(); i++) { int v = vec[k][i]; if (v == Fa) { diedFa[k] = Alive[k]; pas = 1; continue; } dfs(v, k); if (!pas) { diedbef[k] = diedbef[k] * (Alive[v] + diedbef[v] + diedaft[v]) % Mod; diedbef[k] = diedbef[k] + Alive[k] * (Alive[v] + diedaft[v]) % Mod; Alive[k] = Alive[k] * (diedbef[v] + diedFa[v]) % Mod; } else { diedaft[k] = diedaft[k] * (Alive[v] + diedbef[v] + diedaft[v]) % Mod; diedaft[k] = diedaft[k] + Alive[k] * (Alive[v] + diedaft[v]) % Mod; diedbef[k] = diedbef[k] * (Alive[v] + diedbef[v] + diedaft[v]) % Mod; diedFa[k] = diedFa[k] * (Alive[v] + diedbef[v] + diedaft[v]) % Mod; Alive[k] = Alive[k] * (diedbef[v] + diedFa[v]) % Mod; } } } int main() { N = read(); for (int i = 1; i < N; i++) { int u = read(), v = read(); vec[u].push_back(v); vec[v].push_back(u); } dfs(1, 0); printf("%lld\n", (Alive[1] + diedbef[1]) % Mod); return 0; }
10
CPP
from sys import stdin i = [[int(y) for y in x.rstrip().split()] for x in stdin.readlines()] size = i[0][1] red = list(filter(lambda x: x<=5-size, i[1])) print (len(red)//3)
7
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:66777216") using namespace std; const int N = 200007; int n, m, x[N], curx[N], type[N], delta[N], pos[N], L[N], R[N]; map<int, int> id; struct segTree { long long T[4 * N], sum[4 * N]; int cnt[4 * N]; void fix(int v) { sum[v] = sum[2 * v] + sum[2 * v + 1]; cnt[v] = cnt[2 * v] + cnt[2 * v + 1]; T[v] = T[2 * v] + T[2 * v + 1]; if (cnt[2 * v] && cnt[2 * v + 1]) { long long d = cnt[2 * v] * sum[2 * v + 1] - cnt[2 * v + 1] * sum[2 * v]; assert(d >= 0); T[v] += d; } } void add(int v, int tl, int tr, int pos, int x) { if (tl == tr) { T[v] = 0; cnt[v] = 1; sum[v] = x; return; } int tm = (tl + tr) >> 1; if (pos <= tm) { add(2 * v, tl, tm, pos, x); } else { add(2 * v + 1, tm + 1, tr, pos, x); } fix(v); } void erase(int v, int tl, int tr, int pos) { if (tl == tr) { T[v] = 0; cnt[v] = 0; sum[v] = 0; return; } int tm = (tl + tr) >> 1; if (pos <= tm) { erase(2 * v, tl, tm, pos); } else { erase(2 * v + 1, tm + 1, tr, pos); } fix(v); } void query(int v, int tl, int tr, int l, int r, long long &curAns, long long &curSum, int &curCnt) { if (tl == l && tr == r) { curAns += T[v]; if (curCnt) { long long d = curCnt * sum[v] - cnt[v] * curSum; assert(d >= 0 && true); curAns += d; } curSum += sum[v]; curCnt += cnt[v]; return; } int tm = (tl + tr) >> 1; if (r <= tm) { query(2 * v, tl, tm, l, r, curAns, curSum, curCnt); } else if (l > tm) { query(2 * v + 1, tm + 1, tr, l, r, curAns, curSum, curCnt); } else { query(2 * v, tl, tm, l, tm, curAns, curSum, curCnt); query(2 * v + 1, tm + 1, tr, tm + 1, r, curAns, curSum, curCnt); } } } tree; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", x + i); curx[i] = x[i]; } scanf("%d", &m); vector<int> xx(x, x + n); for (int i = 0; i < m; ++i) { scanf("%d", type + i); if (type[i] == 1) { scanf("%d%d", pos + i, delta + i); --pos[i]; curx[pos[i]] += delta[i]; xx.push_back(curx[pos[i]]); } else { scanf("%d%d", L + i, R + i); } } sort((xx).begin(), (xx).end()); xx.resize(unique((xx).begin(), (xx).end()) - xx.begin()); int szt = (int)(xx).size(); for (int i = 0; i < szt; ++i) { id[xx[i]] = i; } for (int i = 0; i < n; ++i) { tree.add(1, 0, szt - 1, id[x[i]], x[i]); } for (int i = 0; i < m; ++i) { if (type[i] == 1) { tree.erase(1, 0, szt - 1, id[x[pos[i]]]); x[pos[i]] += delta[i]; tree.add(1, 0, szt - 1, id[x[pos[i]]], x[pos[i]]); } else { int l = L[i]; int r = R[i]; int lid = lower_bound((xx).begin(), (xx).end(), l) - xx.begin(); int rid; if (id.count(r)) { rid = id[r]; } else { rid = lower_bound((xx).begin(), (xx).end(), r) - xx.begin() - 1; } if (lid > rid) { puts("0"); } else { long long curAns = 0; long long curSum = 0; int curCnt = 0; tree.query(1, 0, szt - 1, lid, rid, curAns, curSum, curCnt); printf("%I64d\n", curAns); } } } return 0; }
11
CPP
n = int(input()) x = [int(x) for x in input().split()] odd = [i + 1 for i, j in enumerate(x) if j % 2 != 0] even = [i + 1 for i, j in enumerate(x) if j % 2 == 0] print(*(odd if len(odd) == 1 else even))
7
PYTHON3
a = input(); print(a+a[::-1]);
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 100; const int mod = 1e9 + 7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int n, a, d[maxn], c[maxn], diff[maxn], loga[maxn], par[maxn], L[maxn], R[maxn]; long long pref[maxn], mx[maxn][20], mn[maxn][20], ans; pair<int, int> diffs[maxn]; int root(int v) { return v == par[v] ? v : par[v] = root(par[v]); } void unite(int a, int b, int i, long long d) { a = root(a), b = root(b); int l = min(L[a], L[b]), r = max(R[a], R[b]); int len = (i - 1) - (l - 1) + 1; long long mini = min(mn[l - 1][loga[len]], mn[i - 1 - (1 << loga[len]) + 1][loga[len]]); len = r - (i + 1) + 1; long long maxi = max(mx[i + 1][loga[len]], mx[r - (1 << loga[len]) + 1][loga[len]]); ans = max(ans, maxi - mini - d * d); par[b] = a; L[a] = l, R[a] = r; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n >> a; for (int i = 1; i <= n; i++) { cin >> d[i] >> c[i]; c[i] = a - c[i]; ans = max(ans, 1ll * c[i]); par[i] = L[i] = R[i] = i; } for (int i = 1; i < n; i++) { diff[i] = d[i + 1] - d[i]; diffs[i] = {diff[i], i}; } for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + c[i]; for (int i = 1; i <= n; i++) mn[i][0] = mx[i][0] = pref[i]; for (int siz = 1; siz < 20; siz++) { int p = (1 << (siz - 1)); for (int i = 0; i <= n; i++) { if (i + 2 * p - 1 > n) break; mx[i][siz] = max(mx[i][siz - 1], mx[i + p][siz - 1]); mn[i][siz] = min(mn[i][siz - 1], mn[i + p][siz - 1]); } } loga[1] = 0; for (int i = 2; i < maxn; i++) loga[i] = loga[i / 2] + 1; sort(diffs + 1, diffs + n); for (int i = 1; i < n; i++) unite(diffs[i].second, diffs[i].second + 1, diffs[i].second, diffs[i].first); cout << ans << "\n"; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; #define all(cont) cont.begin(), cont.end() #define rall(cont) cont.end(), cont.begin() #define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++) #define IN(A, B, C) assert(B <= A && A <= C) #define SORT(v) sort(v.begin(), v.end()) #define MP make_pair #define PB push_back #define INF (int)1e9 #define EPS 1e-9 #define PI 3.1415926535897932384626433832795 #define MOD 1000000007 const double pi = acos(-1.0); typedef long int int32; typedef unsigned long int uint32; typedef long long int int64; typedef unsigned long long int uint64; const int N = 1e5 + 5; void solve() { int64 n, q, l, r; cin>>n>>q; string s; cin>>s; vector<int64> v, v1(n); while(n--){ v.PB(s[s.size()-n-1]-96); } partial_sum(v.begin(), v.end(), v1.begin()); while(q--){ cin>>l>>r; cout<<v1[r-1]-v1[l-1]+v[l-1]<<"\n"; } } int main() { solve(); return 0; }
8
CPP
for _ in range(int(input())): n, a, b, c, d = map(int, input().split()) if ((a-b)*n>(c+d) or (a+b)*n<(c-d)): print("No") else: print("Yes")
7
PYTHON3
from math import ceil n, a, b, k = map(int, input().split()) mon = list(map(int, input().split())) mon2 = [] p = 0 for x in range(0 ,len(mon), +1): if mon[x]%(a+b) > 0 and mon[x]%(a+b) <=a: p += 1 else: if mon[x]%(a+b) == 0: mon2.append(b) else: mon2.append(mon[x]%(a+b)-a) mon2 = sorted(mon2) for x in mon2: if k <= 0: break if x <= a: k -= 1 p += 1 else: if int(ceil(x/a)) <= k: k -= int(ceil(x/a)) p += 1 print(p)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; const int mod = 1e9 + 7; int main() { int T, n; cin >> T; while (T--) { scanf("%d", &n); for (int i = 1; i <= n; i++) printf("1 "); cout << endl; } return 0; }
7
CPP
# def gcd(lft, rght): # if lft < rght: # rght, lft = lft, rght # if rght == 0: # return lft # return gcd(rght, lft % rght) # # # def lcm(lft, rght): # return (lft * rght) / gcd(lft, rght) max_number, lookup_pos = map(int, input().split()) max_number += max_number % 2 if lookup_pos * 2 > max_number: print( max_number - (max_number - lookup_pos) * 2 ) else: print(lookup_pos * 2 - 1)
7
PYTHON3
# -*- coding:utf-8 -*- array = [] while True: try: a = int(input()) array.append(a) except EOFError: break data = [0]*101 for i in range(len(array)): data[array[i]] += 1 m = [i for i,j in enumerate(data) if j == max(data)] for i in range(len(m)): print(m[i])
0
PYTHON3
#Bhargey Mehta (Sophomore) #DA-IICT, Gandhinagar import sys, math, queue #sys.stdin = open("input.txt", "r") MOD = 10**9+7 k = int(input()) f = [] i = 1 while i*i < k: if k%i == 0: f.append((i, k//i)) i += 1 if i*i == k: f.append((i, i)) for i in range(len(f)): if f[i][0] >= 5 and f[i][1] >= 5: x = f[i][1] y = f[i][0] vow = ['a', 'e', 'i', 'o', 'u'] ans = [[None for i in range(y)] for j in range(x)] for ii in range(x): for jj in range(y): ans[ii][jj] = vow[(ii+jj)%5] string = [] for row in ans: string += row print(''.join(string)) exit() print(-1)
8
PYTHON3
#include<bits/stdc++.h> #define rgi register int #define ll long long namespace IO { #define _CCF_ 8388608 // #define getchar() (_Ip1==_Ip2&&(_Ip2=(_Ip1=_I)+fread(_I,1,1<<21,stdin),_Ip1==_Ip2)?EOF:*_Ip1++) // #define putchar(c) (_Op-_O<_CCF_)?(*_Op++=c):(fwrite(_O,_Op-_O,1,stdout),_Op=_O,*_Op++=c) char _I[_CCF_],*_Ip1=_I,*_Ip2=_I,_O[_CCF_],*_Op=_O; int _f,_ch,_On[32],_Oh; long long _k; template<typename _T> inline void read(_T&_x){ _x=_f=_ch=0; while(!isdigit(_ch)) _f|=(_ch=='-'),_ch=getchar(); while(isdigit(_ch)) _x=(_x<<1)+(_x<<3)+(_ch^'0'),_ch=getchar(); _f&&(_x=-_x);} template<typename _T> inline void write(_T _x){ if(_x==0) return putchar('0'),void(); if(_x<0) putchar('-'),_x=-_x; while(_x>0) _k=_x/10,_On[++_Oh]=(_x-(_k<<1)-(_k<<3))^'0',_x=_k; while(_Oh>0) putchar(_On[_Oh]),--_Oh;} inline void _Exit0() {fwrite(_O,_Op-_O,1,stdout),exit(0);} } using namespace IO; const int maxn=500004,mod=998244353; int inv[maxn]; inline int powmod(ll a,int b) { ll res=1; while(b>0) { if(b&1) (res*=a)%=mod; (a*=a)%=mod,b>>=1; } return res; } inline int calc(int n,int m) { ll res=0,fact=1,bn=1,bm=1,pn=powmod(n+1,m),pm=powmod(m+1,n); for(rgi i=0;i<=n;++i) { (res+=((i&1)?(mod-1ll):1ll)*fact%mod*bn%mod*bm%mod*pn%mod*pm)%=mod; (bn*=(ll)(n-i)*inv[i+1]%mod)%=mod,(bm*=(ll)(m-i)*inv[i+1]%mod)%=mod; (pn*=inv[n+1])%=mod,(pm*=inv[m+1])%=mod; (fact*=i+1)%=mod; } return res; } signed main() { // rgi T,n,m,a,b; read(T); // inv[1]=1; for(rgi i=2;i<maxn;++i) inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod; // while(T--) { // read(n),read(m),read(a),read(b); // if(a!=0&&(a==b||a==-b)) write(calc(n,m)); // else write((a?powmod(m+1,n):1)*(b?powmod(n+1,m):1)%mod); // putchar('\n'); // } _Exit0(); int n,m; read(n),read(m); inv[1]=1; for(rgi i=2;i<maxn;++i) inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod; write(calc(std::min(n,m),std::max(n,m))),_Exit0(); }
0
CPP
a, b = map(int, input().split()) i = 1 t = True while True: if t: a -= i if a < 0: print ("Vladik") break else: b -= i if b < 0: print ("Valera") break t = not t i += 1
7
PYTHON3