solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
n = int(input()) ans = 0 for l in range(1, n): if((n - l) % l == 0): ans += 1 print(ans)
800
PYTHON3
from math import ceil n = int(input()) A = list(map(int, input().split())) e = ceil(sum(A) / 2) s = 0 i = 0 while s < e: s += A[i] i += 1 print(i)
1,300
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/stack:30000000") using namespace std; const int Direction[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; const int Nmax = 2000100; long long a[Nmax]; long long Pref[Nmax], Suff[Nmax]; long long C1[Nmax], C2[Nmax]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i < (int)(n + 1); ++i) scanf("%I64d", &a[i]); int mn = a[1]; for (int i = 1; i < (int)(n + 1); ++i) a[i] -= mn; Pref[0] = 0; C1[0] = 0; for (int i = 1; i < (int)(n + 1); ++i) Pref[i] = Pref[i - 1] + ((i - 1) % m == 0 ? a[i] : 0), C1[i] = C1[i - 1] + ((i - 1) % m == 0 ? 1 : 0); Suff[n + 1] = 0; for (int i = n; i >= 1; i--) Suff[i] = Suff[i + 1] + ((i % m == n % m) ? a[i] : 0), C2[i] = C2[i + 1] + ((i % m == n % m) ? 1 : 0); long long Ans = 4000000000000000000LL; for (int i = 1; i <= n; i++) { int j = i; while (j <= n && a[j] == a[i]) j++; j--; long long Temp = 0; Temp = (C1[i - 1] * 1ll * a[i] - Pref[i - 1]); Temp = 2 * (Temp + (Suff[j + 1] - C2[j + 1] * a[j])); Ans = min(Ans, Temp); i = j; } cout << Ans << endl; return 0; }
2,000
CPP
for _ in range(int(input())): n, k = map(int,input().split()) n1 = n-(k-1) if n1>0 and n1%2!=0: print('YES') for i in range(k-1): print(1,end=' ') print(n1) continue n2 = n-2*(k-1) if n2>0 and n2%2==0: print('YES') for i in range(k-1): print(2,end=' ') print(n2) continue print('NO')
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int t, a, b; string s; int main() { cin >> t; while (t--) { cin >> a >> b >> s; int sl = 0, kq = a; bool ok = 0; for (int i = 0; i < s.size(); i++) if (s[i] == '0') sl++; else { if (ok) kq += min(sl * b, a); else ok = 1; sl = 0; } cout << (!ok ? 0 : kq) << '\n'; } return 0; }
1,300
CPP
n,m = map(int,input().split()) houses = [int(x) for x in input().split()] count = 0 init = 1 for i in range(m): if houses[i] >= init: count += houses[i] - init else: count += n - init + houses[i] init = houses[i] print(count)
1,000
PYTHON3
import sys from array import array # noqa: F401 from typing import List, Tuple, TypeVar, Generic, Sequence, Union # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') def main(): from functools import reduce from operator import or_ n = int(input()) if n < 3: print(reduce(or_, map(int, input().split()))) exit() mask = (1 << 30) - 1 upper, lower = array('i', [0]) * n, array('i', [0]) * n for i, x in enumerate(map(int, input().split())): upper[i] = x >> 30 lower[i] = x & mask ans_upper, ans_lower = 0, 0 for i in range(n): for j in range(i + 1, n): for k in range(j + 1, n): if ans_upper < upper[i] | upper[j] | upper[k]\ or ans_upper == upper[i] | upper[j] | upper[k] and ans_lower < lower[i] | lower[j] | lower[k]: ans_upper, ans_lower = upper[i] | upper[j] | upper[k], lower[i] | lower[j] | lower[k] print((ans_upper << 30) + ans_lower) if __name__ == '__main__': main()
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int w[8]; long long m[(1 << 7)][(1 << 7)]; long long tmp[(1 << 7)][(1 << 7)]; long long tmp2[(1 << 7)][(1 << 7)]; long long dp[8][(1 << 7)]; void mult(long long a[(1 << 7)][(1 << 7)], long long b[(1 << 7)][(1 << 7)], int sz) { for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { tmp[i][j] = 0; for (int k = 0; k < sz; k++) { tmp[i][j] += a[i][k] * b[k][j]; tmp[i][j] %= 1000000007; } } } for (int i = 0; i < (1 << 7); i++) for (int j = 0; j < (1 << 7); j++) a[i][j] = tmp[i][j]; } void pow(int b, int sz) { for (int i = 0; i < sz; i++) for (int j = 0; j < sz; j++) tmp2[i][j] = i == j; while (b) { if (b & 1) mult(tmp2, m, sz); mult(m, m, sz); b >>= 1; } swap(tmp2, m); } int main() { dp[0][0] = 1; for (int i = 1; i <= 7; i++) { cin >> w[i]; for (int x = 0; x < (1 << i); x++) { for (int y = 0; y < (1 << i); y++) { int a = 0, b = 1; for (int j = 0; j < i; j++) { int aa, bb; aa = a + b; bb = a + (x & y & (1 << j) ? 0 : b); a = aa; b = bb; } m[x][y] = b; } } pow(w[i], (1 << i)); for (int j = 0; j < (1 << (i - 1)); j++) { for (int k = 0; k < (1 << i); k++) { dp[i][k] += dp[i - 1][j] * m[j | (1 << (i - 1))][k]; dp[i][k] %= 1000000007; } } } cout << dp[7][(1 << 7) - 1] << '\n'; }
2,700
CPP
import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int, input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int, input().split())) def foo(): n, x = invr() arr = inlt() l = -1 r = 0 i = sum = 0 for a in arr: if a % x: if l == -1: l = i r = i sum += a i += 1 if sum % x: print(n) elif l == -1: print(-1) else: print(n-min(l+1, n-r)) if __name__ == '__main__': t = inp() for i in range(t): foo()
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> T GCD(T a, T b) { if (b == 0) return a; return GCD(b, a % b); } template <class T> T LCM(T a, T b) { return (a * b) / GCD(a, b); } const long long inf = 1 << 28; long long n, a, b; map<long long, long long> M; void dfs(long long now, long long digit) { if (now > n || digit > 10) return; if (now != 0) M[now] = 1; dfs(now * 10 + a, digit + 1); dfs(now * 10 + b, digit + 1); } int main() { long long i, j, k; while (cin >> n) { M.clear(); for (a = 0; a <= 9; a++) for (b = a + 1; b <= 9; b++) dfs(0, 0); cout << M.size() << endl; } return 0; }
1,600
CPP
input() s=input() if s.count('0')!=s.count('1'): print(1) print(s) else: print(2) print(s[0],s[1:])
800
PYTHON3
s = int(input()) a = [4, 7] for i in a: if max(a) < 1000: z = (i * 10) + 4 x = (i * 10) + 7 if x not in a: a.append(x) if z not in a: a.append(z) for i in a: if s % i == 0: print("YES") quit() print("NO")
1,000
PYTHON3
import sys import math from collections import defaultdict # sys.stdin = open('input.txt', 'r') # sys.stdout = open('output.txt', 'w') def solve(test): n, m = map(int, input().split()) res = [] if m > 2 * n + 2 or m < n - 1: print(-1) elif n - m == 1: ans = ['0', '1'] * m + ['0'] print(''.join(ans)) else: while n and m: res.append(1) res.append(0) n -= 1 m -= 1 if m: res.append(1) m -= 1 for i in range(0, len(res), 2): if m == 0: break res[i] = 2 m -= 1 ans = [] for i in range(len(res)): if res[i] == 2: ans.append(str(1)) ans.append(str(1)) elif res[i] == 1: ans.append(str(1)) else: ans.append(str(0)) print(''.join(ans)) if __name__ == "__main__": test_cases = 1 for t in range(1, test_cases + 1): solve(t)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long tmp, b, n, m, k, ans = 0; map<long long, long long> matrix; cin >> n >> m >> k; long long app[n + 1]; for (long long i = 1; i < n + 1; i++) { long long var; cin >> var; app[i] = var; matrix[var] = i; } for (long long i = 0; i < m; i++) { long long a; cin >> a; if (matrix[a] % k == 0) { if (matrix[a] == k) { ans++; } else { ans += (matrix[a] / k); } } else { ans += (1 + matrix[a] / k); } if (matrix[a] == 1) continue; else { b = matrix[a]; tmp = app[b - 1]; matrix[a] = b - 1; matrix[tmp] = b; swap(app[b - 1], app[b]); } } cout << ans; return 0; }
1,600
CPP
# -*- coding: utf-8 -*- """ #k,m=map(int,input().split()) t=int(input()) for _ in range(t): n=int(input()) x=list(map(int,input().split())) """ t=int(input()) for _ in range(t): n,x=map(int,input().split()) a=list(map(int,input().split())) p=0 for i in range(n): if a[i]%2==0: p=p+1 q=n-p if x%2==1: if q>=1 and (p//2)+((q-1)//2)>=(x-1)//2: print('YES') else: print('NO') else: if q>=1 and p>=1 and (q-1)//2 +(p-1)//2 >= (x-2)//2: print('YES') else: print('NO')
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int arr[200005]; int ans = 0; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; stack<int> pila; for (int i = 0; i < n; i++) { int cur = arr[i]; while (pila.size() && pila.top() < cur) { pila.pop(); } if (pila.size()) ans = max(ans, cur ^ pila.top()); pila.push(cur); } while (pila.size()) pila.pop(); for (int i = n - 1; i >= 0; i--) { int cur = arr[i]; while (pila.size() && pila.top() < cur) { pila.pop(); } if (pila.size()) ans = max(ans, cur ^ pila.top()); pila.push(cur); } cout << ans << "\n"; return 0; }
1,800
CPP
#include <bits/stdc++.h> const double eps = 1e-5; int dcmp(double x) { return x < -eps ? -1 : x > eps; } struct Line { int a, b, c, id; bool operator<(const Line &rhs) const { return a * 1ll * rhs.b < rhs.a * b; } void show() { printf("l : %d %d %d %d\n", a, b, c, id); } }; const int N = 100000 + 5; int n, K; std::vector<Line> lines; struct Point { double x, y; int a, b; void show() { printf("p : %f %f %d %d\n", x, y, a, b); } }; bool intersection(const Line &lhs, const Line &rhs, Point &o) { if (lhs.a * 1ll * rhs.b == rhs.a * 1ll * lhs.b) { return false; } if (lhs.a != 0) { o.y = (rhs.a * 1. / lhs.a * lhs.c - rhs.c) / (rhs.b - rhs.a * 1. / lhs.a * lhs.b); o.x = -(lhs.b * 1. * o.y + lhs.c) / lhs.a; } else { o.y = (lhs.a * 1. / rhs.a * rhs.c - lhs.c) / (lhs.b - lhs.a * 1. / rhs.a * rhs.b); o.x = -(rhs.b * 1. * o.y + rhs.c) / rhs.a; } o.a = lhs.id; o.b = rhs.id; return true; } bool point_on_line(const Point &o, const Line &l) { return dcmp(l.a * 1. * o.x + l.b * 1. * o.y + l.c) == 0; } std::vector<Point> answer; bool search(std::vector<Line> vl, int K) { if (vl.size() <= K) { puts("YES"); printf("%d\n", answer.size() + vl.size()); for (auto &o : answer) { printf("%d %d\n", o.a + 1, o.b + 1); } for (auto &l : vl) { printf("%d -1\n", l.id + 1); } return true; } if (K == 0) { return false; } for (int i = 0; i < K + 1; ++i) { for (int j = 0; j < i; ++j) { Point o; if (!intersection(vl[i], vl[j], o)) { continue; } std::vector<Line> nvl; for (auto &l : vl) { if (!point_on_line(o, l)) { nvl.push_back(l); } } answer.push_back(o); if (search(nvl, K - 1)) { return true; } answer.pop_back(); } } return false; } bool work() { while (K && lines.size() > K * K) { bool flag = false; for (int i = 0; i < K * K + 1; ++i) { for (int j = 0; j < i; ++j) { Point o; if (!intersection(lines[i], lines[j], o)) { continue; } int cnt = 0; for (int k = 0; k < K * K + 1; ++k) { if (point_on_line(o, lines[k])) { cnt++; } } if (cnt > K) { flag = true; answer.push_back(o); int p = 0; for (auto &l : lines) { if (!point_on_line(o, l)) { lines[p++] = l; } } lines.resize(p); K--; break; } } if (flag) { break; } } if (!flag) { return false; } } return search(lines, K); } int main() { scanf("%d%d", &n, &K); for (int i = 0; i < n; ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); lines.push_back({a, b, c, i}); } if (!work()) { puts("NO"); } }
2,800
CPP
a=int(input()) cnt=0 while int(a/2)!=0: # print(a) if (a%2)==1: cnt=cnt+1 a=int(a/2) print(cnt+1)
1,000
PYTHON3
n = int(input()) arr = [int(w) for w in input().split(' ')] c = [int(w) for w in input().split(' ')] inf = 10**18 dp = [[inf for x in range(n)] for x in range(3)] dp[0] = c.copy() for i in range(1,3): for j in range(i,n): for k in range(j): if arr[j] > arr[k]: dp[i][j] = min( c[j] + dp[i-1][k] , dp[i][j] ) ans = min( dp[-1] ) if ans == inf: ans =-1 print(ans)
1,400
PYTHON3
for _ in range(int(input())): a, b = [int(x) for x in input().split()] a,b = sorted([a, b]) turns = 0 while a < b: turns += 1 a *= 8 if a == b or a // 2 == b or a // 4 == b: print(turns) else: print(-1)
1,000
PYTHON3
#include <bits/stdc++.h> using ll = long long; using ld = long double; using ull = unsigned long long; using namespace std; const int N = 2e5 + 2, mod = 998244353; int n, dp[N][3]; vector<int> g[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = (2); i <= (n); ++i) { int p; cin >> p; g[p].emplace_back(i); } for (int u = (n); u >= (1); --u) { if (((int)(g[u]).size())) { dp[u][0] = 1; for (int v : g[u]) { dp[u][2] = ((dp[u][2] + dp[u][1]) * 1LL * (dp[v][1] + dp[v][2]) + dp[u][2] * 1LL * (dp[v][0] + dp[v][2])) % mod; dp[u][1] = ((dp[u][0] * 1LL * (dp[v][1] + dp[v][2])) + (dp[u][1] * 1LL * (dp[v][0] + dp[v][2]))) % mod; dp[u][0] = dp[u][0] * 1LL * (dp[v][0] + dp[v][2]) % mod; } } else dp[u][2] = 1; } cout << (dp[1][0] + dp[1][2]) % mod; return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; string txt, pat; string con(int x) { stringstream ss; ss << x; return ss.str(); } int sv_sub[1000005][10]; int F[1000005]; void fail() { int i = 0, j = -1; F[0] = -1; while (i < pat.size()) { while (j >= 0 && pat[i] != pat[j]) j = F[j]; i++, j++; F[i] = j; } } vector<int> sv2; bool chk(int sz, vector<int> sv) { string sz_s = con(sz); for (char c : sz_s) sv[c - '0']--; int cnt = 0; for (int i = 0; i < 10; i++) cnt += sv[i]; if (cnt != sz) return 0; for (int i = 0; i <= 9; i++) if (sv[i] < 0) return 0; for (int i = 0; i <= 9; i++) if (sv[i] < sv2[i]) return 0; for (int i = 1; i <= 9; i++) if (sv[i]) return 1; return 0; } vector<int> sv; void get_txt(int sz) { string sz_s = con(sz); for (char c : sz_s) sv[c - '0']--; } bool chk(int idx, int add, int match) { auto tmp_sv = sv; if (!idx && !add) return 0; if (!tmp_sv[add]) return 0; tmp_sv[add]--; while (match >= 0 && add != (pat[match] - '0')) match = F[match]; match++; for (int i = 0; i < 10; i++) if (tmp_sv[i] < sv_sub[match][i]) return 0; return 1; } int main() { cin >> txt >> pat; if (txt.size() == 2) { if (txt[0] == '1') txt.erase(txt.begin()); else txt.erase(txt.begin() + 1); cout << txt << '\n'; return 0; } sv.assign(10, 0); sv2.assign(10, 0); for (char c : txt) sv[c - '0']++; for (char c : pat) sv2[c - '0']++; int i = 1; for (int i = pat.size() - 1; i >= 0; i--) for (int j = 0; j < 10; j++) sv_sub[i][j] = sv_sub[i + 1][j] + (j == (pat[i] - '0')); for (;; i++) { if (chk(i, sv)) break; } int sz = i; get_txt(sz); string ans = ""; int f = 0; i = 0; fail(); while (i < sz) { for (int j = (i == 0); j < 10; j++) { if (chk(i, j, f)) { ans.push_back(j + '0'); sv[j]--; break; } } while (f >= 0 && ans[i] != pat[f]) f = F[f]; i++, f++; if (f == pat.size()) break; } for (int i = 0; i < 10; i++) while (sv[i]--) ans.push_back('0' + i); cout << ans << '\n'; return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; const int oo = 1e+9; const int N = 1e+5 + 5; int ans = 0; int main() { int n, k; cin >> n >> k; priority_queue<int> x; int y; for (int(i) = (0); (i) < (n); (i)++) { cin >> y; x.push(-y); } for (int(i) = (0); (i) < (k); (i)++) { y = x.top(); x.pop(); x.push(-y); } for (int(i) = (0); (i) < (n); (i)++) { ans += (-x.top()); x.pop(); } cout << ans << endl; return 0; }
1,200
CPP
s = list(input()) def cnt(str): rtn = 0 for i in range(1, len(str)): if str[i - 1] == 'V' and str[i] == 'K': rtn += 1 return rtn def change(str, idx): if str[idx] == 'V': return str[:idx] + ['K'] + str[idx + 1:] else: return str[:idx] + ['V'] + str[idx + 1:] ans = cnt(s) for i in range(len(s)): ans = max(cnt(change(s, i)), ans) print(ans)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; long long a[200039], sum[200039]; long long getsum(int l, int r) { return sum[r] - sum[l - 1]; } map<long long, long long> prest, sufst; int main() { scanf("%d", &n); for (int i = (1); i < n + 1; i++) { scanf("%lld", &a[i]); sufst[a[i]]++; } for (int i = (1); i < n + 1; i++) sum[i] = sum[i - 1] + a[i]; int fg = 0; for (int i = (1); i < n + 1; i++) { prest[a[i]]++; auto iter = sufst.find(a[i]); iter->second--; if (!iter->second) sufst.erase(iter); long long presum = getsum(1, i), sufsum = getsum(i + 1, n); if (presum == sufsum) fg = 1; else if (presum > sufsum) { long long diff = presum - sufsum; if (diff & 0x1) continue; diff >>= 1; if (prest.count(diff)) fg = 1; } else { long long diff = sufsum - presum; if (diff & 0x1) continue; diff >>= 1; if (sufst.count(diff)) fg = 1; } } if (fg) printf("YES\n"); else printf("NO\n"); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int n, t1 = -1, t2; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> t2; if (t2 - t1 == 1) { t1 = t2; } else if (t2 > t1) { printf("%d\n", i); return 0; } } printf("-1\n"); return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; const int maxh = 200 * 1000 + 5; struct item { int pos, hash; item(int pos, int hash) : pos(pos), hash(hash) {} }; bool operator<(item a, item b) { return a.pos < b.pos; } int n, m, h; set<item> S[maxh]; map<int, int> M; int pos[maxh]; int Len[maxh]; int Base[maxh]; bool used[maxh]; long long add(int x, int hash) { int base = Base[hash]; set<item>::iterator it = S[base].lower_bound(item(pos[hash], hash)); if (it == S[base].end()) { it = S[base].begin(); } int nhash = it->hash; M[x] = nhash; S[base].erase(it); long long res = pos[nhash] - pos[hash]; if (res < 0) { res += Len[base]; } return res; } void del(int x) { int base = Base[M[x]]; S[base].insert(item(pos[M[x]], M[x])); } int main() { ios::sync_with_stdio(0); scanf("%d%d%d", &h, &m, &n); for (int i = 0; i < h; ++i) { if (used[i]) { continue; } for (int j = i, p = 0; !used[j]; j += m, j %= h, ++p) { S[i].insert(item(p, j)); pos[j] = p; Base[j] = i; used[j] = 1; } Len[i] = (int)S[i].size(); } long long res = 0; for (int i = 0; i < n; ++i) { char op; int id; scanf("\n%c %d", &op, &id); if (op == '+') { int hash; scanf("%d", &hash); res += add(id, hash); } else { del(id); } } printf("%I64d\n", res); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; void fileio() {} long long gcd(long long a, long long b) { return (b) ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long max(long long a, long long b) { return (a >= b) ? a : b; } long long min(long long a, long long b) { return (a <= b) ? a : b; } long long pw(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) { res = res * a; } a = a * a; b = b >> 1; } return res; } long long pw(long long a, long long b, long long MOD) { long long res = 1; a %= MOD; while (b > 0) { if (b & 1) { res = (res * a) % MOD; } a = (a * a) % MOD; b = b >> 1; } return res; } long long egcd(long long a, long long b, long long& x, long long& y) { if (b == 0) { x = 1; y = 0; return a; } long long x1, y1; long long _gcd = egcd(b, a % b, x1, y1); x = y1; y = x1 - (y1 * (a / b)); return _gcd; } long long modInv(long long a, long long MOD) { long long x, y; long long _gcd = egcd(a, MOD, x, y); if (_gcd != 1) { cout << "a and MOD are NOT coprime" << "\n"; return -1; } else { return (x % MOD + MOD) % MOD; } } long long modInvFormat(long long a, long long MOD) { return pw(a, MOD - 2, MOD); } const long long inf = 1e18; const long long ninf = -1e17; const long long mod = 1e9 + 7; const long long mod2 = 998244353; void testcase(long long tc = 1) { vector<long long> v(7); for (long long i = 0; i < (long long)7; i++) cin >> v[i]; long long a = v[0]; long long b = v[1]; long long c = v[6] - v[0] - v[1]; cout << a << ' ' << b << ' ' << c << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long tcs = 1; fileio(); cin >> tcs; for (long long tc = 1; tc <= tcs; tc++) { testcase(tc); } return 0; }
800
CPP
# =============================================================================================== # importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from itertools import * import bisect from heapq import * from math import ceil, floor from copy import * from collections import deque, defaultdict from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl from operator import * # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is returned # ============================================================================================== # fast I/O region BUFSIZE = 8192 from sys import stderr 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") # =============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### # =============================================================================================== # some shortcuts mod = 1000000007 def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def out(var): sys.stdout.write(str(var)) # for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def nextline(): out("\n") # as stdout.write always print sring. def testcase(t): for p in range(t): solve() def pow(x, y, p): res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res from functools import reduce def factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) def gcd(a, b): if a == b: return a while b > 0: a, b = b, a % b return a # N=100000 # mod = 10**9 +7 # fac = [1, 1] # finv = [1, 1] # inv = [0, 1] # # for i in range(2, N + 1): # fac.append((fac[-1] * i) % mod) # inv.append(mod - (inv[mod % i] * (mod // i) % mod)) # finv.append(finv[-1] * inv[-1] % mod) # # # def comb(n, r): # if n < r: # return 0 # else: # return fac[n] * (finv[r] * finv[n - r] % mod) % mod ##############Find sum of product of subsets of size k in a array # ar=[0,1,2,3] # k=3 # n=len(ar)-1 # dp=[0]*(n+1) # dp[0]=1 # for pos in range(1,n+1): # dp[pos]=0 # l=max(1,k+pos-n-1) # for j in range(min(pos,k),l-1,-1): # dp[j]=dp[j]+ar[pos]*dp[j-1] # print(dp[k]) def prefix_sum(ar): # [1,2,3,4]->[1,3,6,10] return list(accumulate(ar)) def suffix_sum(ar): # [1,2,3,4]->[10,9,7,4] return list(accumulate(ar[::-1]))[::-1] # ========================================================================================= from collections import defaultdict def numberOfSetBits(i): i = i - ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 def N(): return int(inp()) def solve(): n,k=sep() ar=lis() ar.sort(reverse=True) print(sum(ar[:k+1])) #solve() testcase(int(inp()))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, p = 1000000; bool checkDias(int a, const vector<unsigned long long int> &v) { for (int i = p; i < n + 1; i += 1) { unsigned long long int d = i / a; unsigned long long int r = i % a; if (v[i] >= (unsigned long long int)m + r * (d + 1) * d / 2 + (a - r) * d * (d - 1) / 2) return true; } return false; } int main() { cin >> n >> m; vector<int> vect(n); vector<unsigned long long int> sumasparciales(n + 1, 0); for (int i = 0; i < n; i += 1) { cin >> vect[i]; } sort(vect.begin(), vect.end(), greater<int>()); for (int i = 0; i < n; i += 1) { sumasparciales[i + 1] = sumasparciales[i] + vect[i]; if (sumasparciales[i + 1] >= m && i + 1 < p) p = i + 1; } if (sumasparciales[n] < m) { cout << -1 << endl; return 0; } int i = 1; int j = m; int minimo = m + 10; while (i <= j) { int nepe = (i + j) / 2; if (checkDias(nepe, sumasparciales)) { j = nepe - 1; minimo = min(minimo, nepe); } else i = nepe + 1; } cout << minimo << endl; }
1,700
CPP
t=int(input()) for _ in range(t): n=int(input()) l=list(map(int,input().split())) arr=[] key=0 for i in range(1,n): if(l[i]-l[i-1]>0): arr.append("+") else: arr.append("-") #print(arr) for i in range(1,n-1): if(arr[i]=="-" and arr[i-1]=="+"): print("YES") print(i,i+1,i+2) key=1 break if(key==0): print("NO")
900
PYTHON3
myString=input() newString = "%s%s" % (myString[0].upper(), myString[1:]) print(newString)
800
PYTHON3
t=int(input()) for i in range(t): n=int(input()) a=[] ans=[-1 for i in range(n)] for i in range(n): h,w=map(int, input().split()) if w>h: h,w=w,h a.append((h,w,i)) a.sort(key=lambda x:x[1],reverse=True) a.sort(key=lambda x:x[0]) prei=-1 prew=10**10 for h,w,i in a: if prew<w: ans[i]=prei+1 else: prei=i prew=w print(*ans)
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; using int32 = int_fast32_t; using uint32 = uint_fast32_t; using int64 = int_fast64_t; using uint64 = uint_fast64_t; using PII = pair<int32, int32>; using PLL = pair<int64, int64>; const double eps = 1e-10; template <typename A, typename B> inline void chmin(A &a, B b) { if (a > b) a = b; } template <typename A, typename B> inline void chmax(A &a, B b) { if (a < b) a = b; } int32 n, m; int32 x[112345]; int32 fl[112345] = {}, fr[112345] = {}; int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < (n); i++) { int32 l, r; cin >> l >> r; l--; x[l]++; x[r]--; } for (int i = 0; i < (m); i++) { x[i + 1] += x[i]; } vector<int32> lis(m + 1, (int)1e9); for (int i = 0; i < (m); i++) { int32 in = upper_bound(lis.begin(), lis.end(), x[i]) - lis.begin(); fl[i] = in; lis[in] = x[i]; } lis = vector<int32>(m + 1, (int)1e9); for (int32 i = m - 1; i >= 0; i--) { int32 in = upper_bound(lis.begin(), lis.end(), x[i]) - lis.begin(); fr[i] = in; lis[in] = x[i]; } int32 res = 0; for (int i = 0; i < (m); i++) { res = max(res, fl[i] + fr[i] + 1); } cout << res << endl; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << "[" << H << "]"; debug_out(T...); } clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const double eps = 0.00001; const int MOD = 1e9 + 7; const int INF = 1000000101; const long long LLINF = 1223372000000000555; const int N = 1e6 + 3e2; const int M = 555; int n; pair<int, int> a[N]; long long dp[N]; pair<long long, int> t[N]; pair<long long, int> combine(pair<long long, int> lf, pair<long long, int> rg) { return lf.first <= rg.first ? lf : rg; } void update(int pos, int v = 1, int tl = 1, int tr = n) { if (tl == tr) { t[v].first = dp[tl] - a[tl + 1].first; t[v].second = tl; return; } int tm = (tl + tr) >> 1; if (pos <= tm) update(pos, v + v, tl, tm); else update(pos, v + v + 1, tm + 1, tr); t[v] = combine(t[v + v], t[v + v + 1]); } pair<long long, int> get(int l, int r, int v = 1, int tl = 1, int tr = n) { if (l <= tl && tr <= r) return t[v]; if (tl > r || tr < l) return {LLINF, 0}; int tm = (tl + tr) >> 1; return combine(get(l, r, v + v, tl, tm), get(l, r, v + v + 1, tm + 1, tr)); } int prv[N]; int ans[N]; void solve(int TC) { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + n + 1); fill(t, t + N, make_pair(LLINF, 0)); for (int i = 1; i <= n; i++) { if (i <= 3) { dp[i] = LLINF; if (i == 3) dp[i] = a[i].first - a[1].first; prv[i] = 0; } else { dp[i] = a[i].first - a[1].first; auto p = get(3, i - 3); dp[i] = min(dp[i], p.first + a[i].first); prv[i] = p.second; } update(i); } int cnt = 0; int x = n; while (x) { cnt++; int y = prv[x]; while (x > y) { ans[a[x].second] = cnt; x--; } } cout << dp[n] << ' ' << cnt << endl; for (int i = 1; i <= n; i++) cout << ans[i] << ' '; } int main() { startTime = clock(); cin.tie(nullptr); cout.tie(nullptr); ios_base::sync_with_stdio(false); bool llololcal = false; int TC = 1; for (int test = 1; test <= TC; test++) { 42; solve(test); } if (llololcal) cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl; return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100; const int M = 2e6 + 100; const int _ = 1e5 + 7; int hd[N], pre[M], to[M], num; int dis[N], tot, n; void adde(int x, int y) { num++; pre[num] = hd[x]; hd[x] = num; to[num] = y; } queue<int> q; vector<int> g[N]; bitset<_> f[300]; int fcnt[300]; int len = -1; long long ans; void update(int l, long long x) { if (l > len) len = l, ans = x; else if (l == len) ans += x; } void dfs(int v, int s) { if (v > n && v <= n + 8) { f[(1 << (v - n - 1))][s] = 1; return; } if (dis[v] == 1) { if (!g[v].size() || *--g[v].end() != s) g[v].push_back(s); return; } for (int i = hd[v]; i; i = pre[i]) { int u = to[i]; if (dis[u] == dis[v] - 1) dfs(u, s); } } int cov[N], d[N]; void bfs(int c) { for (int i = 1; i <= tot; i++) dis[i] = -1; dis[n + c] = 0, q.push(n + c); int maxd = 0; while (!q.empty()) { int v = q.front(); q.pop(); if (dis[v] & 1) maxd = max(maxd, dis[v]); for (int i = hd[v]; i; i = pre[i]) { int u = to[i]; if (dis[u] == -1) dis[u] = dis[v] + 1, q.push(u); } } for (int i = 1; i <= 8; i++) if (i != c) f[1 << (i - 1)].reset(); int m = 0, k = 0; for (int i = 1; i <= tot; i++) if (dis[i] == maxd) dfs(i, i), m++; for (int i = 1; i < (1 << 8); i++) if (!(i >> (c - 1) & 1)) { if (i != (i & -i)) { f[i] = f[i - (i & -i)] | f[i & -i]; } fcnt[i] = f[i].count(); } for (int i = 1; i <= tot; i++) cov[i] = 0; for (int i = 1; i <= 8; i++) if (i != c) cov[n + i] = (1 << (i - 1)); for (int v = 1; v <= tot; v++) if (dis[v] > 1) for (int i = hd[v]; i; i = pre[i]) { int u = to[i]; if (dis[u] == dis[v] - 1) d[u]++; } for (int i = 1; i <= tot; i++) if (!d[i]) q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); if (dis[v] == 1) continue; for (int i = hd[v]; i; i = pre[i]) { int u = to[i]; if (dis[u] == dis[v] - 1) { cov[u] |= cov[v]; d[u]--; if (!d[u]) q.push(u); } } } int len = maxd - 1; long long ans = 0; for (int v = 1; v <= tot; v++) if (dis[v] == 1) { k++; int cnt = fcnt[cov[v]]; for (int i = 0, s = g[v].size(); i < s; i++) if (!f[cov[v]][g[v][i]]) cnt++; if (cnt < m) len = maxd + 1, ans += m - cnt; } if (len == maxd - 1) { ans = 1ll * k * m; for (int i = 1; i <= 8; i++) if (i != c) f[1 << (i - 1)].reset(); m = 0, k = 0; for (int i = 1; i <= tot; i++) if (dis[i] == 1) g[i].clear(); for (int i = 1; i <= tot; i++) if (dis[i] == maxd - 2) dfs(i, i), m++; for (int i = 1; i < (1 << 8); i++) if (!(i >> (c - 1) & 1)) { if (i != (i & -i)) { f[i] = f[i - (i & -i)] | f[i & -i]; } fcnt[i] = f[i].count(); } for (int i = 1; i <= tot; i++) cov[i] = 0; for (int i = 1; i <= 8; i++) if (i != c) cov[n + i] = (1 << (i - 1)); for (int v = 1; v <= tot; v++) if (dis[v] > 1) for (int i = hd[v]; i; i = pre[i]) { int u = to[i]; if (dis[u] == dis[v] - 1) d[u]++; } for (int i = 1; i <= tot; i++) if (!d[i]) q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); if (dis[v] == 1) continue; for (int i = hd[v]; i; i = pre[i]) { int u = to[i]; if (dis[u] == dis[v] - 1) { cov[u] |= cov[v]; d[u]--; if (!d[u]) q.push(u); } } } for (int v = 1; v <= tot; v++) if (dis[v] == 1) { k++; int cnt = fcnt[cov[v]]; for (int i = 0, s = g[v].size(); i < s; i++) if (!f[cov[v]][g[v][i]]) cnt++; ans += m - cnt; } update(len, ans); } else update(maxd + 1, ans); } int cl[N]; bool use[10]; char fafs[100010]; int main() { scanf("%d", &n); scanf("%s", fafs + 1); if (n == 1) printf("%d %d\n", 0, 1), exit(0); tot = n + 8; for (int i = 1; i <= n; i++) { cl[i] = fafs[i] - 'a'; cl[i]++; use[cl[i]] = 1; adde(i, n + cl[i]), adde(n + cl[i], i); } for (int i = 1; i < n; i++) { ++tot; adde(i, tot), adde(tot, i); adde(i + 1, tot), adde(tot, i + 1); } for (int i = 1; i <= 8; i++) if (use[i]) bfs(i); printf("%d %lld\n", len / 2, ans / 2); return 0; }
3,300
CPP
#include <bits/stdc++.h> using namespace std; const int INF = (-1u) / 2; vector<int> hero; map<int, int> rev; void mrev() { for (int i = 0; i < 24; ++i) rev[1 << i] = i; } const int MAXT = 20; const int MAXN = 1 << MAXT; int res[2][MAXN + 10]; vector<short> mv; vector<char> mvt; int main() { mrev(); int n; cin >> n; hero.resize(n); for (int i = 0; i < n; ++i) cin >> hero[i]; sort(hero.begin(), hero.end(), greater<int>()); int m; cin >> m; hero.resize(m); mv.resize(m); mvt.resize(m); for (int i = 0; i < m; ++i) cin >> mvt[i] >> mv[i]; for (int move = m - 1; move >= 0; --move) { int zarib = (move == m - 1 or mv[move + 1] == mv[move]) ? 1 : -1; if (mvt[move] == 'p') for (int I = 0; I < 1 << m; ++I) { res[move % 2][I] = -INF; int get = I & (-I); if (get == 0) res[move % 2][I] = 0; else res[move % 2][I] = max(res[move % 2][I], zarib * (res[1 - move % 2][I ^ get]) + hero[rev[get]]); } else for (int I = 0; I < 1 << m; ++I) { if (I == 0) { res[move % 2][I] = 0; continue; } int J = I; res[move % 2][I] = -INF; while (J != 0) { int get = J & (-J); J ^= get; res[move % 2][I] = max(res[move % 2][I], zarib * res[1 - move % 2][I ^ get]); } } } if (mv[0] == 1) cout << fixed << res[0][(1 << m) - 1] << endl; else cout << fixed << -res[0][(1 << m) - 1] << endl; return 0; }
2,200
CPP
def main(): import sys input = sys.stdin.readline def gcd(a, b): while b: a, b = b, a % b return a n, m, q = map(int, input().split()) g = gcd(n, m) n //= g m //= g for i in range(q): s, x, t, y = map(int, input().split()) x -= 1 y -= 1 if s == 1: x //= n else: x //= m if t == 1: y //= n else: y //= m if x == y: print("YES") else: print("NO") return 0 main()
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int p = 0, q = 0, l = s.length(); if (l < 3) { printf("No solution\n"); return 0; }; vector<int> v; for (int i = 0; i < (l); ++i) if (s[i] == '@') v.push_back(i); int vl = v.size(); if (!vl) { printf("No solution\n"); return 0; }; if (v[0] == 0) { printf("No solution\n"); return 0; }; if (v[vl - 1] == l - 1) { printf("No solution\n"); return 0; }; for (int i = 0; i < (vl - 1); ++i) if (v[i + 1] - v[i] < 3) { printf("No solution\n"); return 0; }; while (p != l) { printf("%c", s[p]); if (q < vl - 1 && p == v[q] + 1) { printf(","); q++; } p++; } printf("\n"); return 0; }
1,500
CPP
t = int(input()) for p in range(0,t): n =int(input()) a = list(map(int,input().split())) k = 1 chck = a[0] for i in range(1,n): if(a[i]*chck <0): k+=1 chck = a[i] # print(k) if(k == 1): print(max(a)) else: sum = 0 i = 0 while(i<n and k>0): # print(a[i]) if(i == n-1): sum += a[n-1] k -=1 else: j = i+1 temp = [a[i]] while(j<n and a[j]*a[i]>0 ): # print('ff') temp.append(a[j]) j +=1 sum += max(temp) # print(sum,end=' ==') # print() i = j k -=1 print(sum)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; bool gcd(long long a, long long b) { while (b) { long long mod = a % b; a = b; b = mod; } if (a == 1) { return true; } else { return false; } } int main() { long long lp, rp; while (cin >> lp >> rp) { bool flag = false; for (long long i = lp; i <= rp - 2; ++i) { for (long long j = i + 1; j <= rp - 1; ++j) { for (long long k = j + 1; k <= rp; ++k) { if (gcd(j, i) && gcd(k, j) && (!gcd(k, i))) { cout << i << " " << j << " " << k << endl; flag = true; } if (flag) break; } if (flag) break; } if (flag) break; } if (flag == false) { cout << -1 << endl; } } return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; struct edge { long long flow; long long cap; long long next; long long inv; }; const long long N = 1100000; const long long inf = (long long)1e9; vector<edge> v[N]; bool vis[N]; long long from[N]; long long RES_FLOW = 0; vector<pair<long long, pair<long long, long long> > > edges; vector<pair<long long, long long> > ness_edges; void add_edge(long long x1, long long x2, long long x3) { edge x; x.flow = 0; x.cap = x3; x.next = x2; x.inv = v[x2].size(); v[x1].push_back(x); x.flow = 0; x.cap = 0; x.next = x1; x.inv = v[x1].size() - 1; v[x2].push_back(x); } void init() { for (long long i = 0; i < N; i++) v[i].clear(); RES_FLOW = 0; for (long long i = 0; i < ness_edges.size(); i++) add_edge(ness_edges[i].first, ness_edges[i].second, 1); } string a[50]; long long females[500][3]; long long males[500][3]; vector<pair<long long, pair<long long, long long> > > v2[800]; long long maxflow(long long n) { long long res_flow = 0; while (true) { queue<long long> q; for (long long i = 0; i < n; i++) { vis[i] = false; } q.push(0); while (!q.empty()) { long long x = q.front(); q.pop(); for (long long i = 0; i < v[x].size(); i++) if (!vis[v[x][i].next] && v[x][i].cap > v[x][i].flow) { q.push(v[x][i].next); vis[v[x][i].next] = true; from[v[x][i].next] = v[x][i].inv; } } if (!vis[n - 1]) break; long long dflow = inf; long long x = n - 1; while (x != 0) { long long a = v[x][from[x]].next; long long b = v[x][from[x]].inv; dflow = min(dflow, v[a][b].cap - v[a][b].flow); x = a; } x = n - 1; while (x != 0) { long long a = v[x][from[x]].next; long long b = v[x][from[x]].inv; v[a][b].flow += dflow; v[x][from[x]].flow -= dflow; x = a; } res_flow += dflow; } RES_FLOW += res_flow; return RES_FLOW; } int main() { cin.sync_with_stdio(false); cin.tie(0); long long x1, x2, cm, cf; cin >> x1 >> x2 >> cm >> cf; for (long long i = 0; i < x1; i++) cin >> a[i]; if (abs(cm - cf) != 1) { cout << "-1"; return 0; } long long a1, a2, a3; cin >> a1 >> a2 >> a3; long long maxt = a3; for (long long i = 0; i < cm; i++) { cin >> males[i][0] >> males[i][1] >> males[i][2]; maxt = max(maxt, males[i][2]); } for (long long i = 0; i < cf; i++) { cin >> females[i][0] >> females[i][1] >> females[i][2]; maxt = max(maxt, females[i][2]); } if (cm == cf + 1) { females[cf][0] = a1; females[cf][1] = a2; females[cf][2] = a3; cf = cm; } else { males[cm][0] = a1; males[cm][1] = a2; males[cm][2] = a3; cm = cf; } long long left = -1; long long right = maxt * 2 * (x1 + x2) + 1; for (long long i = 0; i < cm; i++) { ness_edges.push_back(make_pair(0, i + 1)); } for (long long j = 0; j < x1; j++) for (long long k = 0; k < x2; k++) { ness_edges.push_back( make_pair(j * x2 + k + cm + 1, j * x2 + k + cm + 1 + x1 * x2)); } for (long long i = 0; i < cm + cf; i++) { long long x3; long long x4; long long t; if (i < cm) { x3 = males[i][0]; x4 = males[i][1]; t = males[i][2]; } else { x3 = females[i - cm][0]; x4 = females[i - cm][1]; t = females[i - cm][2]; } x3--; x4--; queue<pair<pair<long long, long long>, long long> > q; q.push(make_pair(make_pair(x3, x4), 0)); vector<pair<long long, long long> > dir; dir.push_back(make_pair(0, -1)); dir.push_back(make_pair(0, 1)); dir.push_back(make_pair(1, 0)); dir.push_back(make_pair(-1, 0)); bool visited[50][50]; for (long long j = 0; j < x1; j++) for (long long k = 0; k < x2; k++) visited[j][k] = false; visited[x3][x4] = true; while (!q.empty()) { long long x3 = q.front().first.first; long long x4 = q.front().first.second; long long dd = q.front().second; if (i < cm) { edges.push_back( make_pair(dd * t, make_pair(i + 1, x3 * x2 + x4 + cm + 1))); } else { edges.push_back( make_pair(dd * t, make_pair(x3 * x2 + x4 + cm + 1 + x1 * x2, x1 * x2 * 2 + cm + 1 + i - cm))); } q.pop(); for (long long j = 0; j < 4; j++) { long long nx3 = x3 + dir[j].first; long long nx4 = x4 + dir[j].second; if (nx3 >= 0 && nx3 < x1 && nx4 >= 0 && nx4 < x2) if (!visited[nx3][nx4] && a[nx3][nx4] == '.') { visited[nx3][nx4] = true; q.push(make_pair(make_pair(nx3, nx4), dd + 1)); } } } } for (long long i = 0; i < cf; i++) { ness_edges.push_back(make_pair(x1 * x2 * 2 + cf + cm + 1 + i - cm, x1 * x2 * 2 + cm + 1 + cf * 2 - cm)); } long long n = x1 * x2 * 2 + cm + 1 + cf * 2 - cm + 1; sort(edges.begin(), edges.end()); long long now = 0; long long now2 = 0; while (now < edges.size()) { long long buf = edges[now].first; while (now < edges.size() && edges[now].first == buf) { v2[now2].push_back(edges[now]); now++; } if (v2[now2].size() >= 700) now2++; } now2++; long long sum = 0; for (long long i = 0; i < now2; i++) { sum += v2[i].size(); } init(); long long gr = 0; for (long long i = 0; i < now2; i++) { for (long long j = 0; j < v2[i].size(); j++) add_edge(v2[i][j].second.first, v2[i][j].second.second, 1); long long fl = maxflow(n); gr = i; if (fl == cf) break; } init(); for (long long i = 0; i < gr; i++) { for (long long j = 0; j < v2[i].size(); j++) add_edge(v2[i][j].second.first, v2[i][j].second.second, 1); } now = 0; while (now < v2[gr].size()) { long long buf = v2[gr][now].first; while (now < v2[gr].size() && v2[gr][now].first == buf) { add_edge(v2[gr][now].second.first, v2[gr][now].second.second, 1); now++; } long long fl = maxflow(n); if (fl == cf) { cout << buf; return 0; } } cout << "-1"; return 0; }
2,700
CPP
t = int(input()) for _ in range(t): n, p, k = map(int, input().split()) a = [int(i) for i in input().split()] a.sort() dp = [0 for _ in range(n)] cost = [0 for _ in range(n)] for i in range(n): if i - k >= 0 and cost[i - k] + a[i] <= p: dp[i] = k + dp[i - k] cost[i] = a[i] + cost[i - k] if dp[i] < 1 + dp[i - 1] and cost[i - 1] + a[i] <= p: dp[i] = 1 + dp[i - 1] cost[i] = a[i] + cost[i - 1] elif a[i] <= p and i - k + 1 >= 0: dp[i] = k cost[i] = a[i] if dp[i] < 1 + dp[i - 1] and cost[i - 1] + a[i] <= p: dp[i] = 1 + dp[i - 1] cost[i] = a[i] + cost[i - 1] elif a[i] <= p: dp[i] = 1 cost[i] = a[i] if i - 1 >= 0 and dp[i] < 1 + dp[i - 1] and cost[i - 1] + a[i] <= p: dp[i] = 1 + dp[i - 1] cost[i] = a[i] + cost[i - 1] else: break print(max(dp))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int check(string s, string t) { if (s.length() > t.length()) { return 0; } long long int i = 0, n1 = s.length(), n2 = t.length(); long long int j = 0; while (i < n1 && j < n2) { if (s[i] == t[j]) { i++; j++; } else { j++; } } if (i == n1) { return 1; } return 0; } int main() { string s1, s2; cin >> s1 >> s2; long long int n1 = s1.length(), i, j; long long int ans = 0; string str = ""; long long int k; for (i = 0; i < n1; i++) { for (j = i; j < n1; j++) { str = ""; for (k = 0; k < i; k++) { str += s1[k]; } for (k = j + 1; k < n1; k++) { str += s1[k]; } if (check(s2, str)) { long long int p = str.length(); ans = max(ans, n1 - p); } } } cout << ans << '\n'; }
1,600
CPP
#include <bits/stdc++.h> inline constexpr int ctoi(const char c) { return c - '0'; } inline constexpr char itoc(const int n) { return n + '0'; } template <typename T> inline T clamp(const T& n, const T& lo, const T& hi) { return std::max(lo, std::min(n, hi)); } template <class T> inline void sort(T& a) { std::sort(a.begin(), a.end()); } template <class T1, class T2> inline void sort(T1& a, T2 comp) { sort(a.begin(), a.end(), comp); } template <class T> inline int read(T& n) { return std::cin >> n ? 1 : -1; } template <typename T, typename... types> inline int read(T& n, types&... args) { return read(n) == -1 ? -1 : read(args...) + 1; } template <class T> inline void write(const T& n) { std::cout << n; } template <typename T, typename... types> inline void write(const char sep, T& n, types&... args) { write(n); write(sep); write(sep, args...); } template <typename T> inline constexpr bool odd(const T a) { return bool(a & 1); } template <typename T> inline constexpr bool even(const T a) { return !odd(a); } template <class T> inline unsigned int mod(const T m, const T n) { return m >= 0 ? m % n : (n - abs(m % n)) % n; } template <class T> class reader { public: void operator()(T& t) const { std::cin >> t; } }; template <class CONTAINER, class READ_Fn> void read(CONTAINER& v, const unsigned int size, const READ_Fn& rfn = reader<typename CONTAINER::value_type>()) { for (int i = 0; i < size; ++i) { rfn(v[i]); } } auto pair_cmp = [](const std::pair<signed long, signed long>& p1, const std::pair<signed long, signed long>& p2) { return (p1.first < p2.first) || (p1.first == p2.first && p1.second < p2.second); }; using namespace std; int main() { ios_base::sync_with_stdio(false); int s = 300000; vector<int> P(s, 0); int n; read(n); while (n--) { char c; signed long long x; read(c, x); int N = 0; int p = 1; while (x > 0) { N += p * ((x % 10) % 2 == 1); p *= 2; x /= 10; } if (c != '?') { int inc = (c == '+') ? 1 : -1; P[N] += inc; } else { write(P[N]); write('\n'); } } return 0; }
1,400
CPP
n=int(input()) res=[0]*100001 line=[int(i) for i in input().split()] for i in line: res[i]+=1 k=0 for i in range(100001): res[i]+=k k=res[i] num=int(input()) for i in range(num): m=int(input()) if m>100000: m=100000 print(res[m])
1,100
PYTHON3
s = ( 13, 19, 31, 37, 43, 53, 61, 79, 101, 113, 139, 163, 173, 199, 211, 223, 241, 269, 277, 331, 353, 373, 397, 457, 463, 509, 521, 541, 577, 601, 619, 631, 727, 773, 787, 811, 829, 853, 883, 907, 919, 947, 967, 991) n, k = [int(i) for i in input().split()] print(("YES", "NO")[sum(p <= n for p in s) < k])
1,000
PYTHON3
a,b=[int(i) for i in input().split()] if b>a: print(-1) else: if b==1: if a>1: print(-1) else:print('a') else: b-=2 for i in range(0,a-b): if i%2==0:print('a',end='') else:print('b',end='') for i in range(b): print(chr(ord('c')+i),end='')
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> T inf() { return numeric_limits<T>::has_infinity ? numeric_limits<T>::infinity() : (numeric_limits<T>::max() / 2); } FILE *file_in = stdin, *file_out = stdout; struct NORMAL_IN { bool cnt; NORMAL_IN() : cnt(true) {} operator int() const { return cnt; } NORMAL_IN &operator>>(int &n) { cnt = fscanf(file_in, "%d", &n) != EOF; return *this; } NORMAL_IN &operator>>(unsigned int &n) { cnt = fscanf(file_in, "%u", &n) != EOF; return *this; } NORMAL_IN &operator>>(long long &n) { cnt = fscanf(file_in, "%lld", &n) != EOF; return *this; } NORMAL_IN &operator>>(unsigned long long &n) { cnt = fscanf(file_in, "%llu", &n) != EOF; return *this; } NORMAL_IN &operator>>(double &n) { cnt = fscanf(file_in, "%lf", &n) != EOF; return *this; } NORMAL_IN &operator>>(long double &n) { cnt = fscanf(file_in, "%Lf", &n) != EOF; return *this; } NORMAL_IN &operator>>(char *c) { cnt = fscanf(file_in, "%s", c) != EOF; return *this; } NORMAL_IN &operator>>(string &s) { s.clear(); for (bool r = false;;) { const char c = getchar(); if (c == EOF) { cnt = false; break; } const int t = isspace(c); if (!r and !t) r = true; if (r) { if (!t) s.push_back(c); else break; } } return *this; } template <class T> NORMAL_IN &operator>>(vector<T> &v) { int n; fscanf(file_in, "%d", &n); for (int i = 0; i < (int)(n); ++i) { T t; *this >> t; v.push_back(t); } return *this; } } normal_in; struct NORMAL_OUT { NORMAL_OUT &operator<<(const int &n) { fprintf(file_out, "%d", n); return *this; } NORMAL_OUT &operator<<(const unsigned int &n) { fprintf(file_out, "%u", n); return *this; } NORMAL_OUT &operator<<(const long long &n) { fprintf(file_out, "%lld", n); return *this; } NORMAL_OUT &operator<<(const unsigned long long &n) { fprintf(file_out, "%llu", n); return *this; } NORMAL_OUT &operator<<(const double &n) { fprintf(file_out, "%lf", n); return *this; } NORMAL_OUT &operator<<(const long double &n) { fprintf(file_out, "%Lf", n); return *this; } NORMAL_OUT &operator<<(const char c[]) { fprintf(file_out, "%s", c); return *this; } NORMAL_OUT &operator<<(const string &s) { fprintf(file_out, "%s", s.c_str()); return *this; } } normal_out; struct ERR_OUT { template <class T> ERR_OUT &operator<<(const T &a) { cerr << "\x1b[7m" << a << "\x1b[m"; return *this; } } ferr; template <class Weight> struct edge { int src, dst; Weight weight; int rev; edge(int src, int dst, Weight weight = 1, int rev = -1) : src(src), dst(dst), weight(weight), rev(rev) {} }; template <class Weight> bool operator<(const edge<Weight> &e, const edge<Weight> &f) { return e.weight != f.weight ? e.weight > f.weight : e.src != f.src ? e.src < f.src : e.dst < f.dst; } template <class Weight> struct matrix : vector<vector<Weight> > { matrix(const array<int, 2> &a, Weight w = 0) : vector<vector<Weight> >(a[0], vector<Weight>(a[1], w)) {} matrix(const array<int, 1> &a, Weight w = 0) : vector<vector<Weight> >(a[0], vector<Weight>(a[0], 0)) {} matrix() {} }; template <class Weight> struct graph : vector<vector<edge<Weight> > > { Weight inf{::inf<Weight>()}; graph() {} graph(const int &n) : vector<vector<edge<Weight> > >(n) {} void _add_edge(int from, int to, Weight w, int rev = -1) { if ((int)this->size() < from + 1) this->resize(from + 1); this->at(from).push_back(edge<Weight>(from, to, w, rev)); } }; template <class Weight> void addBiEdge(graph<Weight> &g, const pair<int, int> &e, Weight w = 1) { const int &from = e.first, &to = e.second; g._add_edge(from, to, w, g[to].size()); g._add_edge(to, from, w, g[from].size() - 1); } template <class Weight> void addEdge(graph<Weight> &g, const pair<int, int> &e, Weight w = 1) { const int &from = e.first, &to = e.second; g._add_edge(from, to, w); } template <class T> void matrix_out(const vector<vector<T> > &x) { for (int i = 0; i < (int)(x.size()); ++i) { for (int j = 0; j < (int)(x[0].size()); ++j) { cout << x[i][j] << "\t"; } cout << "\n"; } } template <class U, class S, class T> U &operator<<(U &os, const pair<S, T> &p) { os << "(" << p.first << ", " << p.second << ")"; return os; } void printBit(int b, int k = -1) { if (k == -1) { int bb = 1, i = 0; while (bb) { if (b & bb) k = i; bb <<= 1; i++; } } for (int ct = 0; ct < (int)(k); ++ct) { cout << b % 2; b >>= 1; } cout << "\n"; } template <typename U, template <typename... Args> class Container, typename... Types> U &operator<<(U &os, const Container<Types...> &x) { os << "{"; for (const auto &e : x) os << " " << e << " "; os << "}"; return os; } int main() { int n, m; normal_in >> n >> m; graph<int> g(n); unordered_map<int, unordered_map<int, int> > mp; vector<pair<int, int> > query(m); for (int i = 0; i < (int)(m); ++i) { int a, b, c; normal_in >> a >> b >> c; a--; b--; mp[a][b] = mp[b][a] = i; query[i] = {a, b}; addBiEdge(g, {a, b}, c); } vector<int> ans(m, -1), flow(n, 0); for (int u = 1; u < n - 1; u++) { for (int i = 0; i < (int)(g[u].size()); ++i) { flow[u] += g[u][i].weight; } flow[u] /= 2; } queue<int> q; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); for (auto &&e : g[u]) { int v = e.dst; int i = mp[u][v]; if (ans[i] != -1) continue; if (query[i].first == u) ans[i] = 0; else ans[i] = 1; flow[v] -= e.weight; if (v != n - 1 and flow[v] == 0) { q.push(v); } } } for (int i = 0; i < (int)(ans.size()); ++i) { normal_out << ans[i] << "\n"; } }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, a[1 << 18], ans = 0, buf = 1, j = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == -1) memset(a, 0, sizeof(a)); } reverse(a, a + n); multiset<int> S; for (int i = 0, cur = n; i < log2(n); i++) { while (j < buf) S.insert(a[j++]); ans += *S.begin(); S.erase(S.begin()); buf += (cur /= 2); } cout << ans << '\n'; }
2,400
CPP
import math t=int(input()) for j in range(t): n=int(input()) temp=int((n/2)-1) count=0 ang=360/(2*n) for i in range(temp): ang1=(i+1)*ang count=count+math.cos(math.radians(ang1)) ans=float((2*count)+1.0) print(format(ans,'.9f'))
1,400
PYTHON3
#!/usr/bin/env python3 def main(): try: while True: n = int(input()) a = list(map(int, input().split())) b = [0] * n last = 400000 for i in range(n - 1, -1, -1): if a[i] == 0: last = i b[i] = last - i last = -400000 for i in range(n): if a[i] == 0: last = i print(min(i - last, b[i]), end=' ') print() except EOFError: pass main()
1,200
PYTHON3
import sys import math input = sys.stdin.readline n = int(input()) a = map(int, input().split()) l = [[], [], []] for i, ai in enumerate(a, 1): l[ai-1].append(i) print(min(len(l[0]),len(l[1]),len(l[2]))) for j in zip(*l): print(' '.join(map(str, j)))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[105]; vector<int> v; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; for (int i = 0; i < k; i++) cin >> a[i]; for (int i = 1; i <= n; i++) v.push_back(i); int ptr = 0; for (int i = 0; i < k; i++) { int x = a[i] % v.size(); ptr = ptr + x; ptr %= v.size(); cout << v[ptr] << " "; v.erase(v.begin() + ptr); } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k, res = 0; cin >> n >> k; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.begin(), v.end(), greater<int>()); for (int i = 0; i < n; i += k) { res += (v[i] - 1) * 2; } cout << res; return 0; }
1,300
CPP
n = int(input()) for i in range(0,n): str1 = input() len1 = str(len(str1)-2) if len(str1) > 10: print(str1[0]+len1+str1[-1]) else: print(str1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int x1, y1, x2, y2; while (cin >> x1 >> y1 >> x2 >> y2) { int ans = 0; if (x1 + y1 <= max(x2, y2)) ans = 1; if (x1 <= x2 && y1 <= y2) ans = 1; if (ans) cout << "Polycarp\n"; else cout << "Vasiliy\n"; } return 0; }
1,700
CPP
a = list(map(int, input().split())) a.sort() if a[0]+a[1] > a[2]: print('0') else: print((a[2]-(a[0]+a[1]))+1)
800
PYTHON3
""" nombre: Twims id: 160A fuente: codeforces coder: cgesu """ n = int(input()) coins = sorted(list(map(int, input().split())), reverse=1) sum_coins = sum(coins) num_coins = sum_temp = 0 for coin in coins: num_coins += 1 sum_temp += coin sum_coins -= coin if sum_temp > sum_coins: break; print(num_coins)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; int arr[10000]; int max = 0, min = 0; int cnt = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } max = arr[0]; min = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] < min) { cnt++; min = arr[i]; } if (arr[i] > max) { cnt++; max = arr[i]; } } if (n == 1) { cout << 0 << endl; } else { cout << cnt << endl; } }
800
CPP
#include <bits/stdc++.h> using namespace std; int n, k, x, y; void go() { cin >> n; vector<int> a(n + 1); for (int(i) = (1); (i) < (n + 1); (i)++) cin >> a[i]; int sum_even = 0; int sum_odd = 0; vector<int> pe(n + 1, 0), se(n + 1, 0), po(n + 1, 0), so(n + 1, 0); for (int(i) = (1); (i) < (n + 1); (i)++) { if (i % 2 == 0) { pe[i] = sum_even; sum_even += a[i]; if (i + 1 <= n) pe[i + 1] = sum_even; } else { po[i] = sum_odd; sum_odd += a[i]; if (i + 1 <= n) po[i + 1] = sum_odd; } } sum_odd = sum_even = 0; for (int i = n; i >= 1; i--) { if (i % 2 == 0) { se[i] = sum_even; sum_even += a[i]; if (i - 1 >= 0) se[i - 1] = sum_even; } else { so[i] = sum_odd; sum_odd += a[i]; if (i - 1 >= 0) so[i - 1] = sum_odd; } } int ans = 0; for (int i = 1; i <= n; i++) { if ((pe[i] + so[i]) == (po[i] + se[i])) ans++; } cout << ans; } int main() { int tc = 1; while (tc--) { go(); } return 0; }
1,200
CPP
number = input() count = 0 for i in number: if i=='4' or i=='7': count += 1 if count == 4 or count == 7: print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long i = 0; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } long long start = a[0]; i = 1; long long l = 1; long long dir = 0; vector<long long> ans; ans.push_back(a[0]); while (i < n) { while ((a[i] == start) && (i < n)) { i++; } if (i == n) break; long long el = a[i]; if (el > start) dir = 1; else dir = -1; if (dir == 1) { while ((i < (n - 1)) && (a[i + 1] >= a[i])) { i++; } } else { while ((i < (n - 1)) && (a[i + 1] <= a[i])) { i++; } } long long val = a[i]; ans.push_back(val); l++; if (i < n) start = a[i]; } if (l == 1) { ans.push_back(a[0]); l++; } cout << l << endl; for (auto it : ans) cout << it << " "; cout << endl; } }
1,300
CPP
list1 = list(map(int,input().split())) n,k = list1[0],list1[1] list2 = list(map(int,input().split())) bigger = 0 for i in range(n): if list2[i] >= list2[k-1] and list2[i] != 0: bigger += 1 else: break print(bigger)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e6 + 5, inf = 1e18, mod = 1e9 + 7; long long n, q, a[maxn]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> q; long long p1 = 0, p2 = 1; while (q--) { long long x; cin >> x; if (x == 2) { p1 ^= 1; p2 ^= 1; continue; } long long y; cin >> y; p1 += y; p2 += y; if (p1 < 0) p1 += n; if (p1 >= n) p1 -= n; if (p2 < 0) p2 += n; if (p2 >= n) p2 -= n; } long long x = 1, y = p1; while (x < n) { a[y] = x; x += 2; y += 2; if (y >= n) y -= n; } x = 2; y = p2; while (x <= n) { a[y] = x; x += 2; y += 2; if (y >= n) y -= n; } for (long long i = 0; i < n; i++) cout << a[i] << ' '; }
1,800
CPP
t=int(input()) while t: n,m = map(int,input().split()) m-=1 while m: ls = list(str(n)) n+=(int(max(ls))*int(min(ls))) if((int(max(ls))*int(min(ls)))) == 0: break m-=1 print(n) t-=1
1,200
PYTHON3
import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) res = [] len = 0 m, n , k = inlt() for i in range(n-1): res.append("L") len+=1 for i in range(m-1): res.append("U") len+=1 for i in range(m): for j in range(n-1): if i % 2 == 0: res.append("R") else: res.append("L") len+=1 if(i < m-1): res.append("D") len+=1 print(len) listToStr = ''.join(map(str, res)) print(listToStr)
1,600
PYTHON3
a = list(map(int,input().split())) print(a[0]-max(a[1]+1,a[0]-a[2])+1)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = int(1e5) + 9; int n; set<int> sDouble; long long sum[2]; set<int> s[2]; int cntDouble[2]; void upd(int id) { assert(s[id].size() > 0); int x = *s[id].rbegin(); if (id == 1) x = *s[id].begin(); bool d = sDouble.count(x); sum[id] -= x, sum[!id] += x; s[id].erase(x), s[!id].insert(x); cntDouble[id] -= d, cntDouble[!id] += d; } int main() { cin >> n; for (int i = 0; i < n; ++i) { int tp, x; cin >> tp >> x; if (x > 0) { sum[0] += x; s[0].insert(x); cntDouble[0] += tp; if (tp) sDouble.insert(x); } else { x = -x; int id = 0; if (s[1].count(x)) id = 1; else assert(s[0].count(x)); sum[id] -= x; s[id].erase(x); cntDouble[id] -= tp; if (tp) { assert(sDouble.count(x)); sDouble.erase(x); } } int sumDouble = cntDouble[0] + cntDouble[1]; while (s[1].size() < sumDouble) upd(0); while (s[1].size() > sumDouble) upd(1); while (s[1].size() > 0 && s[0].size() > 0 && *s[0].rbegin() > *s[1].begin()) { upd(0); upd(1); } assert(s[1].size() == sumDouble); long long res = sum[0] + sum[1] * 2; if (cntDouble[1] == sumDouble && sumDouble > 0) { res -= *s[1].begin(); if (s[0].size() > 0) res += *s[0].rbegin(); } cout << res << endl; } return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int tests; cin >> tests; vector<vector<int>> permutations(tests); for (int i = 0; i < tests; ++i) { int sz; cin >> sz; for (int j = 1; j <= sz; ++j) { permutations[i].push_back(j); } rotate(permutations[i].begin(), permutations[i].begin() + 1, permutations[i].end()); } for (auto&& perm : permutations) { for (int i : perm) { cout << i << ' '; } cout << '\n'; } return 0; }
800
CPP
n=int(input()) n1=n ans=1 while(n1>0): ans=ans*(n1%10) n1=n1//10 p=1 d=1 z1=n//10 while(z1>0): p=1 z1-=1 for i in range(d): p*=9 z2=z1 while(z2>0): p*=z2%10 z2=z2//10 ans=max(ans,p) d+=1 z1=z1//10 print(ans)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[36000], lst[36000]; int dp[36000][55]; const int mx = int(1 << 16); int tree[2 * mx], aval[2 * mx]; void split(int r) { tree[r << 1] += aval[r]; tree[r << 1 | 1] += aval[r]; aval[r << 1] += aval[r]; aval[r << 1 | 1] += aval[r]; aval[r] = 0; } void update(int rt, int l, int r, int u, int v, int val) { if (u <= l && r <= v) { tree[rt] += val; aval[rt] += val; return; } split(rt); int m = (l + r) / 2; if (u < m) update(rt << 1, l, m, u, v, val); if (m < v) update(rt << 1 | 1, m, r, u, v, val); tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]); } int query(int rt, int l, int r, int u, int v) { if (u <= l && r <= v) return tree[rt]; int ans = 0; int m = (l + r) / 2; if (u < m) ans = max(ans, query(rt << 1, l, m, u, v)); if (m < v) ans = max(ans, query(rt << 1 | 1, m, r, u, v)); return ans; } int main() { int n, k; scanf("%d%d", &(n), &(k)); for (int i = 1; i <= n; i++) scanf("%d", &(a[i])); for (int i = 1; i <= k; i++) { memset(lst, 0, sizeof(lst)); memset(tree, 0, sizeof(tree)); memset(aval, 0, sizeof(aval)); for (int j = i; j <= n; j++) { int l = max(i, lst[a[j]] + 1); update(1, mx, mx * 2, mx + l, mx + j + 1, 1); update(1, mx, mx * 2, mx + j, mx + j + 1, dp[j - 1][i - 1]); dp[j][i] = query(1, mx, mx * 2, mx + i, mx + j + 1); lst[a[j]] = j; } } printf("%d\n", dp[n][k]); return 0; }
2,200
CPP
#include <bits/stdc++.h> const char* const days[] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}; int main() { char f[64], s[64]; scanf("%s%s", f, s); int i, j; for (i = 0; i < 7; ++i) { if (strcmp(days[i], f) == 0) break; } if (strcmp(days[(i + 31) % 7], s) == 0 || strcmp(days[(i + 30) % 7], s) == 0 || strcmp(days[(i + 28) % 7], s) == 0) { puts("YES"); } else { puts("NO"); } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { char a[1005], b[1005]; int i, len1, len2, x1 = 0, x2 = 0, ans = 0, max, j; cin >> a >> b; len1 = strlen(a); len2 = strlen(b); max = (len1 > len2) ? len1 : len2; for (i = 0; i < len2; i++) { if (b[i] != '*') { for (j = 0; j < max; j++) { if (j < len1) { if (a[j] == b[i]) { x1++; a[j] = '*'; } } if (j < len2) { if (b[j] == b[i]) { x2++; if (j != i) b[j] = '*'; } } } b[i] = '*'; if (x1 == 0 && x2 != 0) { cout << -1; return 0; } if (x1 == x2 || x1 < x2) ans += x1; if (x1 > x2) ans += x2; x1 = 0; x2 = 0; } } cout << ans; return 0; }
1,200
CPP
n=int(input()) s=0 for i in range(n): a=input() if a.find('++')==-1: s-=1 else: s+=1 print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(long long &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const long long &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.first); putchar(' '); _W(x.second); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } int MOD = 1e9 + 7; void ADD(long long &x, long long v) { x = (x + v) % MOD; if (x < 0) x += MOD; } const int SIZE = 1e6 + 10; int a[SIZE], b[SIZE]; int main() { int n, m, p; R(n, m, p); for (int i = 0; i < (n); ++i) { R(a[i]); a[i] %= p; } for (int i = 0; i < (m); ++i) { R(b[i]); b[i] %= p; } int it = 0; while (a[it] == 0 && b[it] == 0) it++; if (a[it] && b[it]) W(2 * it); else { int it2 = it; if (!a[it]) { while (!a[it2]) it2++; } else { while (!b[it2]) it2++; } W(it + it2); } return 0; }
1,800
CPP
for _ in range (int(input())): s = input() ans = [] for i in range(len(s)): if 2 <= i <= len(s) - 2 and s[i - 2: i + 3] == 'twone': ans.append(i + 1) elif i >= 2 and s[i - 2: i + 1] == 'two': ans.append(i) elif i <= len(s) - 2 and s[i: i + 3] == 'one': ans.append(i + 2) print(len(ans)) print(*ans)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 6e5 + 100; map<pair<string, string>, int> mp; vector<int> vec[N]; string s, a, b; int q; inline int toInt(string s) { int res = 0; for (auto c : s) res = res * 27 + (c - 'a') + 1; return res; } inline int solve(string a, string b) { auto vec1 = vec[toInt(a)]; auto vec2 = vec[toInt(b)]; int res = 1e9; if (vec1.size() > vec2.size()) { swap(vec1, vec2); swap(a, b); } for (auto x : vec1) { int mn = lower_bound(vec2.begin(), vec2.end(), x) - vec2.begin(); if (mn < vec2.size()) res = min(res, max(x + int(a.size()), vec2[mn] + int(b.size())) - min(x, vec2[mn])); if (!mn) continue; mn--; res = min(res, max(x + int(a.size()), vec2[mn] + int(b.size())) - min(x, vec2[mn])); } return (res == 1e9 ? -1 : res); } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> s; for (int i = 0; i < s.size(); i++) { string tmp = ""; for (int j = 0; j < 4 && i + j < s.size(); j++) { tmp += s[i + j]; vec[toInt(tmp)].push_back(i); } } cin >> q; while (q--) { string a, b; cin >> a >> b; if (mp[{a, b}] == 0) mp[{a, b}] = solve(a, b); cout << mp[{a, b}] << '\n'; } }
2,500
CPP
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-9; const int MOD = 1e9 + 9; const int INF = 1e9 + 10000; const long long INFLL = 4e18; const int dr[] = {-1, 0, 1, 0, -1, 1, 1, -1}; const int dc[] = {0, 1, 0, -1, 1, 1, -1, -1}; long long power(long long x, int y) { if (y == 0) return 1LL; long long ret = power(x, y / 2); ret = (ret * ret) % MOD; if (y & 1) ret = (ret * x) % MOD; return ret; } set<int> elev; set<int> stair; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, st, el, v; scanf("%d %d %d %d %d", &n, &m, &st, &el, &v); for (int i = 0; i < st; i++) { int x; scanf("%d", &x); stair.insert(x); } for (int i = 0; i < el; i++) { int x; scanf("%d", &x); elev.insert(x); } int q; scanf("%d", &q); while (q--) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); if (x1 == x2) { printf("%d\n", abs(y1 - y2)); continue; } int clost; set<int>::iterator it; int op1 = 2e9; if (stair.size() > 0) { it = stair.lower_bound(y1); if (it == stair.end()) { --it; op1 = abs(y1 - *it) + abs(x1 - x2) + abs(*it - y2); } else if (it == stair.begin()) { op1 = abs(y1 - *it) + abs(x1 - x2) + abs(*it - y2); } else { int tmp1 = abs(y1 - *it) + abs(x1 - x2) + abs(*it - y2); --it; int tmp2 = abs(y1 - *it) + abs(x1 - x2) + abs(*it - y2); op1 = min(tmp1, tmp2); } } int op2 = 2e9; if (elev.size() > 0) { int naik = (int)(ceil((double)(abs(x1 - x2)) / (double)v)); it = elev.lower_bound(y1); if (it == elev.end()) { --it; op2 = abs(y1 - *it) + naik + abs(*it - y2); } else if (it == elev.begin()) { op2 = abs(y1 - *it) + naik + abs(*it - y2); } else { int tmp1 = abs(y1 - *it) + naik + abs(*it - y2); --it; int tmp2 = abs(y1 - *it) + naik + abs(*it - y2); op2 = min(tmp1, tmp2); } } int ans = min(op1, op2); printf("%d\n", ans); } return 0; }
1,600
CPP
q = int(input()) for i in range(q): matches = int(input()) if matches == 0: print(4) elif matches == 2: print(2) elif matches % 2 != 0: print(1) else: print(0)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int dif(int a) { vector<int> p; int i; int tag = 0; while (a != 0) { p.push_back(a % 10); a = a / 10; } sort(p.begin(), p.end()); for (i = 1; i < p.size(); i++) { if (p[i] == p[i - 1]) { return 0; tag = 1; break; } } if (tag == 0) return 1; } int main() { int l, r, i; cin >> l >> r; for (i = l; i <= r; i++) { if (dif(i)) { cout << i; break; } } if (i == r + 1) cout << -1; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int inf = INT_MAX; const int mod = 1e9 + 7; const int N = 2e5 + 5; vector<pair<long long, long long>> adj[1005]; bool vis[1005]; long long cnt, l; void dfs(long long node) { l = node; vis[node] = true; for (auto x : adj[node]) { if (!vis[x.first]) { dfs(x.first); cnt = min(cnt, x.second); } } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, p; cin >> n >> p; vector<long long> in(n + 1), out(n + 1); for (long long i = 0; i < p; i++) { long long a, b, c; cin >> a >> b >> c; adj[a].push_back({b, c}); in[b]++; out[a]++; } vector<vector<long long>> ans; for (long long i = 1; i <= n; i++) { if (in[i] == 0 and out[i] == 1) { cnt = inf; dfs(i); ans.push_back({i, l, cnt}); } } cout << ans.size() << "\n"; for (auto x : ans) { cout << x[0] << " " << x[1] << " " << x[2] << "\n"; } return 0; }
1,400
CPP
n = int(input()) acc = 0 for i in range(n): row = list(map(int, input().split())) for j in range(n): if ( i == j or i + j == n - 1 or j == n // 2 or i == n // 2 ): acc += row[j] print(acc)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int isLess(long long x, long long y, long long a, long long b, long long c) { if (b == 0) { long long num = -c; long long den = a; if (den < 0) { den *= -1; num *= -1; } return den * x < num; } long long num = -a * x - c; long long den = b; if (den < 0) { den *= -1; num *= -1; } return den * y < num; } int main() { long long a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); int n; scanf("%d", &n); int ans = 0; for (int i = 0; i < n; i++) { long long x, y, z; scanf("%lld %lld %lld", &x, &y, &z); ans += isLess(a, b, x, y, z) ^ isLess(c, d, x, y, z); } printf("%d\n", ans); }
1,700
CPP
#include <bits/stdc++.h> using namespace std; queue<pair<long long, long long> > v; int main() { long long x, val, c; cin >> x; v.push(make_pair(0, 0)); while (!v.empty()) { val = v.front().first; c = v.front().second; v.pop(); if (val >= x && c == 0) return cout << val, 0; v.push(make_pair(val * 10 + 4, c + 1)); v.push(make_pair(val * 10 + 7, c - 1)); } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 123; const int mod = 998244353; const int pr = 31; int n; string s; long long p[N], h[N]; int cnt[N]; const int prime = 131; bool good(int pos, int len) { long long hash1 = h[len]; long long hash2 = h[pos + len - 1]; hash2 -= h[pos - 1]; hash2 %= mod; hash2 += mod; hash2 %= mod; return ((hash1 * p[pos - 1] % mod) == hash2); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; p[0] = 1; for (int i = 1; i <= s.size(); i++) p[i] = p[i - 1] * prime % mod; int n = s.size(); s = '$' + s; for (int i = 1; i <= n; i++) { h[i] = (s[i] - 'A' + 1) * p[i] + h[i - 1]; h[i] %= mod; } for (int i = 1; i <= n; i++) { int l = 1, r = n - i + 1; int ans = 0; while (l <= r) { int mid = (l + r) / 2; if (good(i, mid)) ans = mid, l = mid + 1; else r = mid - 1; } cnt[ans]++; } vector<pair<int, int> > v; for (int i = n; i >= 1; i--) cnt[i] += cnt[i + 1]; for (int i = 1; i <= n; i++) { if (good(n - i + 1, i) && cnt[i]) v.push_back({i, cnt[i]}); } cout << v.size() << endl; for (auto [x, y] : v) cout << x << ' ' << y << endl; return 0; }
2,000
CPP
# -*- coding: utf-8 -*- """ Created on Tue Dec 17 20:26:50 2019 @author: zheng """ g=list(input()) h=list(input()) bad=list(input()) a=g+h a.sort() n=len(a) bad.sort() if len(bad)!=len(a): print('NO') else: if all(a[i]==bad[i] for i in range(n)): print('YES') else: print('NO')
800
PYTHON3
from collections import defaultdict n = int(input()) A = [int(i) for i in input().split()] C = defaultdict(int) for i in range(n): for j in range(i+1, n): C[A[i] + A[j]] += 1 print(max(C.values()))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j, k; cin >> n; for (i = 0; i < n; i++) { printf("%c", 'a' + (i % 4)); } printf("\n"); }
1,100
CPP
n = int(input()) print(n, 0, 0)
900
PYTHON3
n,m=map(int,input().split()) r=[] c=[] for i in range(n): x=input() for j in range(m): if x[j]=='S': r.append(i) c.append(j) r=len(list(set(r))) c=len(list(set(c))) print(n*m-r*c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = int(1e9) + 7; const int INF = INT_MAX; const int MAX = 2e5 + 5; void Print() { cerr << endl; } template <typename Head, typename... Tail> void Print(Head h, Tail... t) { cerr << h << " "; Print(t...); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<int> v(n); for (auto &x : v) cin >> x; if (k == 1) return cout << (int)v.size(), 0; sort(v.begin(), v.end()); for (int i = 0; i < (int)v.size(); ++i) { if (i + k - 1 < (int)v.size() && (lower_bound(v.begin(), v.end(), v[i] + m) - (v.begin() + i) >= k)) { ; v.erase(v.begin() + i + k - 1, lower_bound(v.begin(), v.end(), v[i] + m)); } } cout << n - (int)v.size(); }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100; set<int> where[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; int v = 0; for (int i = 1; i <= n; ++i) { int a; cin >> a; where[--a].insert(i); if (where[a].size() > where[v].size()) { v = a; } } int ans = 0; for (int i = 0; i < min(n, 100); ++i) { if (i == v) { continue; } map<int, int> events; for (auto j : where[i]) { events[j] = 1; for (int k = 0; k < 2; ++k) { auto nxt = where[v].lower_bound(j); auto prv = nxt; if (prv != where[v].begin()) { --prv; events[*prv] = -1; where[v].erase(prv); } if (nxt != where[v].end()) { events[*nxt] = -1; where[v].erase(nxt); } } } for (auto it = events.begin(); it != events.end(); ++it) { if (events.count(it->first - 1) == 0) { events[it->first - 1] = 0; } } if (events.count(n) == 0) { events[n] = 0; } map<int, int> first; first[0] = 0; int sum = 0; int prv = 0; for (auto [a, b] : events) { sum += b; if (first.count(sum) > 0) { if (first[sum] != prv) { ans = max(ans, a - first[sum]); } } else { first[sum] = a; } prv = a; if (b == -1) { where[v].insert(a); } } } cout << ans << "\n"; }
2,600
CPP
for line in range(int(input())): b = input() a="" for j, i in enumerate(b): if j%2==1: a+=i print(b[0]+a)
800
PYTHON3
from fractions import gcd def main(): T = int(input()) for _ in range(T): r, b, k = map(int, input().split()) if r > b: r, b = b, r g = gcd(r, b) r //= g b //= g m = (b - 1 + (r-1))//r # m = ceil((b - 1)/r) if m < k: print('OBEY') else: print('REBEL') main()
1,700
PYTHON3
import math n = int(input()) insects = input() resr1 = 0 resb1 = 0 for i in range(n): if i % 2 == 0 and insects[i] == "r": resr1 += 1 elif i % 2 == 1 and insects[i] == "b": resb1 += 1 res1 = min(resr1, resb1) + abs((resr1 - resb1)) resr2 = 0 resb2 = 0 for i in range(n): if i % 2 == 0 and insects[i] == "b": resb2 += 1 elif i % 2 == 1 and insects[i] == "r": resr2 += 1 res2 = min(resr2, resb2) + abs((resr2 - resb2)) print(min(res1, res2))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int l = s.length(); long long int dp[l][10]; memset(dp, 0, sizeof(dp[0][0]) * l * 10); for (int i = 0; i < 10; i++) dp[0][i] = 1; for (int i = 1; i < l; i++) { for (int j = 0; j < 10; j++) { int z = (s[i] - 48) + j; if (z % 2) { dp[i][z / 2] += dp[i - 1][j]; dp[i][z / 2 + 1] += dp[i - 1][j]; } else dp[i][z / 2] += dp[i - 1][j]; } } long long int ans = 0; for (int i = 0; i < 10; i++) ans += dp[l - 1][i]; int ch = 1; for (int i = 0; i < l - 1; i++) { double z = (s[i] + s[i + 1]) / 2.0; int x = floor(z); int y = ceil(z); if (x != s[i + 1] && y != s[i + 1]) { ch = 0; break; } } cout << ans - ch << endl; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 501; const long long mod = 1000000007; const long long ool = 1e18 + 7; const long long o = 1; long long a[maxn]; long long b[maxn]; long long k[maxn]; int n; long long minz; long long cx[maxn]; long long cy[maxn]; int px[maxn]; int py[maxn]; long long we[maxn][maxn]; int visx[maxn]; int visy[maxn]; long long slack[maxn]; int dfs(int u) { visx[u] = 1; for (int v = (0); v < (n); ++v) { if (visy[v]) continue; long long t = cx[u] + cy[v] - we[u][v]; if (t == 0) { visy[v] = 1; if (py[v] == -1 || dfs(py[v])) { py[v] = u; px[u] = v; return 1; } } else if (t < slack[v]) slack[v] = t; } return 0; } int main(void) { scanf("%lld", &n); for (int i = (0); i < (n); ++i) { scanf("%lld%lld%lld", &a[i], &b[i], &k[i]); } for (int i = (0); i < (n); ++i) px[i] = py[i] = -1; for (int i = (0); i < (n); ++i) for (int j = (0); j < (n); ++j) we[i][j] = max(0LL, a[j] - b[j] * min(k[j], (long long)i)), cx[i] = max(cx[i], we[i][j]); for (int i = (0); i < (n); ++i) { for (int i = 0; i < n; ++i) slack[i] = ool; for (;;) { memset(visx, false, sizeof(visx)); memset(visy, false, sizeof(visy)); if (dfs(i)) break; long long minz = ool; for (int i = (0); i < (n); ++i) if (!visy[i]) minz = min(minz, slack[i]); for (int i = (0); i < (n); ++i) { if (visx[i]) cx[i] -= minz; if (visy[i]) cy[i] += minz; else slack[i] -= minz; } } } long long ans = 0; for (int i = (0); i < (n); ++i) if (py[i] != -1) ans += we[py[i]][i]; printf("%lld\n", ans); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; vector<long long> v(n); for (long long &i : v) cin >> i; long long neg = 0; for (long long i = 0; i < n; ++i) { if (v[i] < 0) neg++; } if (neg > k) { puts("-1"); return 0; } long long ans = 0; vector<pair<long long, long long>> gaps; long long extra = k - neg; long long cnt = 0; bool summer = 1; for (long long i = 0; i < n; ++i) { if (v[i] < 0) { if (summer) { ans++; summer = 0; } } else { if (!summer) { ans++; summer = 1; } } } bool seen = 0; for (long long i = 0; i < n; ++i) { if (v[i] < 0) { if (cnt > 0 && seen) gaps.push_back({cnt, -2}); seen = 1; cnt = 0; } else { cnt++; } } sort(gaps.begin(), gaps.end()); for (pair<long long, long long> i : gaps) { if (extra >= i.first) { extra -= i.first; ans += i.second; } } if (cnt > 0 && seen && extra >= cnt) { ans--; } cout << ans; return 0; }
1,800
CPP
import sys I = sys.stdin.buffer.readline for _ in range(int(I())): I(); print(len(set(I().split())))
1,200
PYTHON3