solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int n, a, b, k; cin >> n >> a >> b >> k; long long int* arr = new long long int[n]; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } for (long long int i = 0; i < n; i++) { long long int times = arr[i] / (a + b); if (arr[i] % (a + b) == 0) { times--; arr[i] = arr[i] - times * (a + b); if (arr[i] % a == 0) { arr[i] = arr[i] / a - 1; } else { arr[i] = arr[i] / a; } } else { arr[i] = arr[i] - times * (a + b); if (arr[i] % a == 0) { arr[i] = arr[i] / a - 1; } else { arr[i] = arr[i] / a; } } } sort(arr, arr + n); long long int ans = 0; for (long long int i = 0; i < n; i++) { k = k - arr[i]; if (k < 0) { break; } ans++; } cout << ans << endl; return 0; }
1,500
CPP
n, m = map(int, input().split()) first = [] second = [] for i in range(m): a, b = input().split() first.append(a) second.append(b) c = input().split() y = "" for i in range(n): if c[i] in first: x = first.index(c[i]) else: x = second.index(c[i]) if len(first[x]) <= len(second[x]): y += (first[x] + " ") else: y += (second[x] + " ") print(y)
1,000
PYTHON3
k = int(input()) list1 = input().split(' ') list2 = [int(x) for x in list1] sum1 = 0 for r in list2: sum1 += r print(sum1 / k)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long dp[2005][2005]; char s[200005]; long long ans; int main() { int n, m, d; scanf("%d %d ", &n, &m); int dif = n - m; scanf("%s", s); int a = 0, b = 0; for (int i = 0; i < m && s[i] != '\0'; i++) { if (s[i] == '(') { a++; } else { a--; } b = min(b, a); } dp[0][0] = 1LL; for (int i = 1; i <= dif; i++) { for (int j = 0; j <= dif; j++) { dp[i][j] = dp[i - 1][j + 1]; dp[i][j] %= 1000000007; if (j != 0) { dp[i][j] += dp[i - 1][j - 1]; dp[i][j] %= 1000000007; } } } for (int c = 0; c <= dif; c++) { for (int d = 0; d <= dif; d++) { if (-1 * b <= d && n - m - c >= 0 && d + a <= dif) { ans += dp[c][d] * dp[dif - c][d + a]; ans %= 1000000007; } } } printf("%I64d", ans); }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int n, a, b, k; char s[200005]; int ans[200005], cnt; int main() { scanf("%d%d%d%d", &n, &a, &b, &k); scanf("%s", s + 1); s[n + 1] = '1'; int last = 0; for (int i = 1; i <= n + 1; ++i) { if (s[i] == '1') { if (i - last <= b) { last = i; continue; } int j; for (j = last + b; j < i; j += b) { ans[++cnt] = j; } j -= b; if (i - 1 - j >= b) ans[++cnt] = i - 1; last = i; } } printf("%d\n", cnt - a + 1); for (int i = 1; i <= cnt - a + 1; ++i) { printf("%d ", ans[i]); } printf("\n"); return 0; }
1,700
CPP
import sys,math,itertools from collections import Counter,deque,defaultdict from bisect import bisect_left,bisect_right from heapq import heappop,heappush,heapify, nlargest from copy import deepcopy mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split())) def inps(): return sys.stdin.readline() def inpsl(x): tmp = sys.stdin.readline(); return list(tmp[:x]) def err(x): print(x); exit() for _ in range(inp()): s = input() cnt =Counter(s) miud = min(cnt['U'],cnt['D']) milr = min(cnt['L'],cnt['R']) res = [] if miud == 0 and milr == 0: pass elif miud == 0: res.append('R') res.append('L') elif milr == 0: res.append('U') res.append('D') else: for _ in range(miud): res.append('U') for _ in range(milr): res.append('L') for _ in range(miud): res.append('D') for _ in range(milr): res.append('R') print(len(res)) print(''.join(res))
1,200
PYTHON3
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) li = [a[0]] for i in range(1, n): if a[i] * a[i - 1] > 0: li[-1] = max(li[-1], a[i]) else: li.append(a[i]) print(sum(li))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[200001]; void doomed() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); sort(a, a + n); int mn = INT_MAX; for (int i = 1; i < n; ++i) { mn = min(mn, abs(a[i] - a[i - 1])); } int pairs = 0; for (int i = 1; i < n; ++i) { if (abs(a[i] - a[i - 1]) == mn) ++pairs; } printf("%d %d\n", mn, pairs); } int main() { int t = 1; while (t--) { doomed(); } return 0; }
1,100
CPP
for i in range(int(input())): n = int(input()) a = tuple(map(int, input().split())) for i in range(n): if a[i] % 2 == 0: print(1, i + 1) break else: if n == 1: print(-1) else: print(2, 1, 2)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932385; const long double E = 2.71828182845904523536; const long long mod = 1e9 + 7; const long long inf = 1e15; const double eps = 1e-5; const int N = 100100; double n, p; pair<double, double> a[N]; bool ok(double x) { double sum = 0.0, y, pp = p * x; for (int i = 0; i < (int)n; i++) { y = a[i].second - (a[i].first * x); if (y < 0) sum += (y * -1.0); } return sum <= pp; } int main() { scanf("%lf%lf", &n, &p); long long sum = 0; for (int i = 0; i < (int)n; i++) { scanf("%lf%lf", &a[i].first, &a[i].second); sum += (int)a[i].first; } if ((int)p >= sum) { printf("-1"); return 0; } double lo = 0, hi = 1e12; while (hi - lo > eps) { double mid = (hi + lo + eps) / 2; if (ok(mid)) lo = mid; else hi = mid - eps; } printf("%.4f", lo); return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const double PI = 2 * acos(0.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); mt19937_64 rng_64(chrono::steady_clock::now().time_since_epoch().count()); const string DIGITS = "0123456789"; const string ALPH = "abcdefghijklmnopqrstuvwxyz"; template <class T0, class T1> inline ostream &operator<<(ostream &out, pair<T0, T1> &a) { return out << "{" << a.first << ", " << a.second << "}"; } template <class T0, class T1, class T2> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << "}"; } template <class T0, class T1, class T2, class T3> inline ostream &operator<<(ostream &out, tuple<T0, T1, T2, T3> &a) { return out << "{" << get<0>(a) << ", " << get<1>(a) << ", " << get<2>(a) << ", " << get<3>(a) << "}"; } template <class T> inline ostream &operator<<(ostream &out, vector<T> &a) { out << "["; for (int i = 0; i < int(a.size()); ++i) out << a[i] << vector<string>{", ", "] "}[i + 1 == a.size()]; return out; } void smain(); signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(12) << fixed; smain(); return 0; } const int N = 1e5 + 100; int parent[N]; int sz[N]; vector<int> comp[N]; void make_set(int v) { parent[v] = v; sz[v] = 1; } int find_set(int v) { return parent[v] == v ? v : parent[v] = find_set(parent[v]); } void union_sets(int u, int v) { u = find_set(u); v = find_set(v); if (u == v) return; if (sz[u] < sz[v]) swap(u, v); parent[v] = u; sz[u] += sz[v]; } vector<int> g[N]; void smain() { int n, m; cin >> n >> m; for (int i = 0; i < int(m); ++i) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; ++i) make_set(i); int ans = 0; while (true) { vector<pair<pair<int, int>, int>> add; for (int v = 1; v <= n; ++v) { comp[v].clear(); } for (int v = 1; v <= n; ++v) { comp[find_set(v)].push_back(v); } set<int> all; for (int i = 1; i <= n; ++i) all.insert(i); for (int v = 1; v <= n; ++v) { if (parent[v] != v) continue; for (auto x : comp[v]) { all.erase(x); } bool ok = false; for (auto x : comp[v]) { for (auto to : g[x]) { if (find_set(to) != v) { all.erase(to); } } if (!all.empty()) { ok = true; add.push_back({{x, *all.begin()}, 0}); } for (auto to : g[x]) { if (find_set(to) != v) { all.insert(to); } } if (ok) break; } if (!ok) { if (!all.empty()) add.push_back({{v, *all.begin()}, 1}); } for (auto x : comp[v]) { all.insert(x); } } bool have = false; for (auto x : add) { if (find_set(x.first.first) != find_set(x.first.second)) { have = true; ans += x.second; union_sets(x.first.first, x.first.second); } } if (!have) break; } cout << ans; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 1e6 + 5; int n, m; struct edge { int x, nxt; } e[N]; int h[N], tot; void inse(int x, int y) { e[++tot].x = y; e[tot].nxt = h[x]; h[x] = tot; } bool used[N]; int be[N]; int main() { n = get(); m = get(); for (int i = 1; i <= m; i++) { int x = get(), y = get(); inse(x, y); } for (int i = 1; i <= n; i++) if (!be[i]) { be[i] = i; for (int p = h[i]; p; p = e[p].nxt) if (!be[e[p].x]) be[e[p].x] = i; used[i] = 1; } for (int i = n; i >= 1; i--) if (be[i] == i) { if (used[i]) for (int p = h[i]; p; p = e[p].nxt) used[e[p].x] = 0; } int k = 0; for (int i = 1; i <= n; i++) if (used[i]) k++; printf("%d\n", k); for (int i = 1; i <= n; i++) if (used[i]) printf("%d ", i); return 0; }
3,000
CPP
#include <bits/stdc++.h> using namespace std; char letra(int contador) { if (contador == 0) { return 'A'; } else if (contador == 1) { return 'G'; } else if (contador == 2) { return 'C'; } else { return 'T'; } } int main() { int n, preg = 0, max = -1, contador, index, relleno; string s; cin >> n; cin >> s; int hash[4]; int aux[4]; for (int i = 0; i < 4; i++) { hash[i] = 0; } for (int i = 0; i < n; i++) { if (s[i] == 'A') { hash[0]++; } else if (s[i] == 'G') { hash[1]++; } else if (s[i] == 'C') { hash[2]++; } else if (s[i] == 'T') { hash[3]++; } else { preg++; } } for (int i = 0; i < 4; i++) { if (hash[i] > max) { max = hash[i]; index = i; } } if (hash[0] + hash[1] + hash[2] + hash[3] == 0 && preg == 4) { s[0] = 'A'; s[1] = 'G'; s[2] = 'C'; s[3] = 'T'; for (int i = 0; i < n; i++) { cout << s[i]; } cout << endl; } else if (!((preg - (max - hash[0] + max - hash[1] + max - hash[2] + max - hash[3])) % 4 == 0) || (preg - (max - hash[0] + max - hash[1] + max - hash[2] + max - hash[3]) < 0)) { cout << "===" << endl; } else { contador = 0; relleno = preg - (max - hash[0] + max - hash[1] + max - hash[2] + max - hash[3]); relleno /= 4; aux[0] = max - hash[0]; aux[1] = max - hash[1]; aux[2] = max - hash[2]; aux[3] = max - hash[3]; aux[0] += relleno; aux[1] += relleno; aux[2] += relleno; aux[3] += relleno; for (int i = 0; i < n; i++) { if (s[i] == '?') { if (aux[contador] > 0) { s[i] = letra(contador); aux[contador] -= 1; if (aux[contador] == 0) contador++; } else { contador++; i--; } } } for (int i = 0; i < n; i++) { cout << s[i]; } cout << endl; } }
900
CPP
n= input() max_energy=max(map(int,input().split())) print(max_energy)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int n) { long long int res = 1; while (n > 0) { if (n % 2 == 1) { res = res * x; } x = x * x; n = n / 2; } return res; } long long int powm(long long int a, long long int b) { long long int res = 1; a %= 998244353; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % 998244353; a = a * a % 998244353; } return res; } string req = "abacaba"; int check(string s) { auto it = s.find(req); if (s.find(req, it + 1) != string::npos) return 0; return 1; } void solve() { long long int n; cin >> n; string s; cin >> s; string m = s; for (int i = 0; i <= n - 7; i++) { m = s; for (int j = i; j < i + 7; j++) { if (m[j] == '?') { m[j] = req[j - i]; } else if (m[j] != req[j - i]) { goto skip; } } if (check(m)) { cout << "YES" << '\n'; for (int k = 0; k < n; k++) { if (m[k] == '?') m[k] = 'z'; } cout << m << '\n'; return; } skip:; } cout << "NO" << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int T; T = 1; cin >> T; while (T--) { solve(); } return 0; }
1,500
CPP
from collections import defaultdict n = int(input()) bs = list(map(int, input().split())) adjusted = [bci-ci for ci, bci in enumerate(bs)] groups = defaultdict(int) for adj, b in zip(adjusted, bs): groups[adj] += b print(max(groups.values()))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); set<char> vowel = {'I', 'E', 'A', 'O', 'U', 'Y'}; int n = s.size(); vector<int> pref = {0}; for (char i : s) { pref.push_back(pref.back()); pref.back() += vowel.find(i) != vowel.end(); } double total = 0; long long ile = 0; for (int l = 0, r = n; l < n; l++, r--) { ile += pref[r] - pref[l]; total += (double)ile / (l + 1); } printf("%lf", total); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; void solutionD(); int main() { ios_base::sync_with_stdio(false); solutionD(); return EXIT_SUCCESS; } long long x, y, n, d, vx[32], vy[32]; long long dp[2][4][1024][1024]; long long dfs(long long x, long long y, long long s, long long p) { assert(abs(x) <= 420 && abs(y) <= 420); if (dp[p][s][x + 512][y + 512] != 0) return dp[p][s][x + 512][y + 512]; long long c = 0; for (long long l = 0; l < n; ++l) { long long nx = x + vx[l], ny = y + vy[l]; if (nx * nx + ny * ny > d * d) continue; long long r = dfs(nx, ny, s, !p); if (r == 2) ++c; } if ((s & (1 << p)) == 0) { long long nx = y, ny = x; if (nx * nx + ny * ny <= d * d) { long long r = dfs(nx, ny, s | (1 << p), !p); if (r == 2) ++c; } } if (c == 0) dp[p][s][x + 512][y + 512] = 2; else dp[p][s][x + 512][y + 512] = 1; return dp[p][s][x + 512][y + 512]; } void solve() { memset(dp, 0, sizeof(long long) * 1024 * 1024 * 4 * 2); cout << (dfs(x, y, 0, 0) == 1 ? "Anton" : "Dasha") << endl; } void solutionD() { for (; cin >> x >> y >> n >> d;) { for (long long i = 0; i < n; ++i) cin >> vx[i] >> vy[i]; solve(); cerr << endl; } }
1,900
CPP
n,k=map(int,input().split()) t=[] t=list(map(int,input().split())) t.sort() s=0 l=n-1 while l>=0: s+=2*(t[l]-1) l-=k print(s)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; struct node { int a, b; bool sts; }; int main() { int n; cin >> n; node no[n]; for (int i = 0; i < n; i++) { cin >> no[i].a >> no[i].b; no[i].sts = true; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i != j) { if (no[i].b == no[j].a) no[j].sts = false; } } } int cnt = 0; for (int i = 0; i < n; i++) if (no[i].sts) cnt++; cout << cnt << "\n"; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int size = 0, i = 0; static int b[1000000] = {}, a[1000000] = {}; cin >> size; for (i = 0; i < size; i++) { cin >> b[i]; } a[size - 1] = b[size - 1]; for (i = size - 2; i >= 0; i--) { a[i] = b[i] + b[i + 1]; } for (i = 0; i < size; i++) { cout << a[i] << " "; } return 0; }
800
CPP
t = int(input()) for _ in range(t): word = input() n = len(word) if n>10: print(word[0] + str(len(word) - 2) + word[-1]) else: print(word)
800
PYTHON3
for _ in range(int(input())): n = int(input()) s = input() lol = 0 ans = 0 for elem in s: if elem == '(': lol += 1 else: lol -= 1 if lol < 0: ans = max(ans, abs(lol)) print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> #pragma GCC target("sse,sse2,sse3,ssse3,sse4") #pragma GCC optimize("Ofast") using namespace std; bool dbg = 0; clock_t start_time = clock(); void bad(string mes = "Impossible") { cout << mes; exit(0); } void bad(int mes) { cout << mes; exit(0); } template <typename T> string bin(T x, int st = 2) { string ans = ""; while (x > 0) { ans += char('0' + x % st); x /= st; } reverse(ans.begin(), ans.end()); return ans.empty() ? "0" : ans; } template <typename T> void amax(T& x, T y) { x = max(x, y); } template <typename T> void amin(T& x, T y) { x = min(x, y); } template <typename T> T input() { T ans = 0, m = 1; char c = ' '; while (!((c >= '0' && c <= '9') || c == '-')) { c = getchar(); } if (c == '-') m = -1, c = getchar(); while (c >= '0' && c <= '9') { ans = ans * 10 + (c - '0'), c = getchar(); } return ans * m; } template <typename T> void read(T& a) { a = input<T>(); } template <typename T> void read(T& a, T& b) { read(a), read(b); } template <typename T> void read(T& a, T& b, T& c) { read(a, b), read(c); } template <typename T> void read(T& a, T& b, T& c, T& d) { read(a, b), read(c, d); } const int inf = 1e9 + 20; const short short_inf = 3e4 + 20; const long double eps = 1e-12; const int maxn = (int)1e6 + 12; const long long llinf = 2e18 + 5; const double PI = acos(-1.0); const int mod = 1e9 + 7; template <typename T> T binpow(T n, T second) { if (second <= 0) return 1LL; if (second % 2 == 0) { T b = binpow(n, second / 2); return (1LL * b * b) % mod; } else { return (1LL * binpow(n, second - 1) * n) % mod; } } bool distmode; int dist(int m, int a, int b) { if (a == b) { return 0; } if (distmode == 0) { if (a < b) return b - a; else return a + m - b; } else { if (a > b) return a - b; else return a + m - b; } } int distb(int m, int a, int b) { if (a > b) swap(a, b); return min(b - a, a + m - b); } int brute(int m, vector<int> a, vector<int> b) { sort((b).begin(), (b).end()); int res = inf; do { int cur = 0; for (int i = 0; i < a.size(); i++) { cur += distb(m, a[i], b[i]); } amin(res, cur); } while (next_permutation((b).begin(), (b).end())); return res; } void gen(int& m, vector<int>& a, vector<int>& b) { m = rand() % 10 + 5; int n = 6; a.clear(); b.clear(); for (int i = 0; i < n; i++) { a.push_back(rand() % m); b.push_back(rand() % m); } } int solve(int m, vector<int> a, vector<int> b) { vector<int> usa(a.size(), 0), usb(a.size(), 0); assert(a.size() == b.size()); int res = 0; for (int i = 0; i < a.size(); i++) { int mn = inf, i1, i2; for (int j = 0; j < a.size(); j++) if (!usa[j]) for (int k = 0; k < a.size(); k++) if (!usb[k]) { if (dist(m, a[j], b[k]) < mn) { mn = dist(m, a[j], b[k]); i1 = j; i2 = k; } } res += mn; usa[i1] = 1; usb[i2] = 1; } return res; } int solve2(int m, vector<int> a, vector<int> b) { vector<pair<int, int> > ev; for (auto e : a) { ev.push_back(make_pair(e, 0)); } for (auto e : b) { ev.push_back(make_pair(e, 1)); } sort((ev).begin(), (ev).end()); int res = inf; vector<pair<int, int> > st; int mxdist = -inf; for (int i = 0; i < ev.size(); i++) { amax(mxdist, dist(m, ev[i].first, ev[(i + 1) % ev.size()].first)); } distmode = 0; for (int i = 0; i < ev.size(); i++) { if (true) { int cur = 0; int ind = i; for (int j = 0; j < ev.size(); j++) { if (st.empty() || st.back().second == ev[ind].second) { st.push_back(ev[ind]); } else { cur += dist(m, st.back().first, ev[ind].first); st.pop_back(); } ind += 1; if (ind == ev.size()) ind = 0; } amin(res, cur); } } distmode = 1; reverse((ev).begin(), (ev).end()); for (int i = 0; i < ev.size(); i++) { if (true) { vector<pair<int, int> > st; int cur = 0; for (int j = 0; j < ev.size(); j++) { int ind = (j + i) % ev.size(); if (st.empty() || st.back().second == ev[ind].second) { st.push_back(ev[ind]); } else { cur += dist(m, st.back().first, ev[ind].first); st.pop_back(); } } amin(res, cur); } } return res; } struct mda { int first, second, id; bool operator<(const mda& t) const { return make_pair(first, second) < make_pair(t.first, t.second); } }; long long solve3(int m, vector<int> a, vector<int> b, vector<int>& psans) { vector<mda> ev; for (int i = 0; i < a.size(); i++) { ev.push_back({a[i], -1, i}); } for (int i = 0; i < a.size(); i++) { ev.push_back({b[i], 1, i}); } sort((ev).begin(), (ev).end()); map<int, long long> ccu, ccd; for (int i = 0, bal = 0; i < ev.size(); i++) { if (ev[i].second == -1) ccd[bal] += ev[i].first; else ccu[bal] += ev[i].first; bal += ev[i].second; } long long sa = 0, sb = 0; for (auto e : ccu) { if (e.first < 0) { sb += e.second; } else { sa += e.second; } } for (auto e : ccd) { if (e.first > 0) { sb += e.second; } else { sa += e.second; } } long long ans = sb - sa; int addToAll = 0; int res = 0; for (int i = 0; i + 1 < ev.size(); i++) { int rb = 0; if (ev[i].second == -1) { ccd[rb - addToAll] -= ev[i].first; sa -= ev[i].first; sb += ev[i].first + m; long long dg = ccu[-1 - addToAll]; sb -= dg; sa += dg; long long dg2 = ccd[0 - addToAll]; sa -= dg2; sb += dg2; ccd[+1 - addToAll] += ev[i].first + m; } else { ccu[rb - addToAll] -= ev[i].first; sa -= ev[i].first; sb += ev[i].first + m; long long dg = ccd[1 - addToAll]; sb -= dg; sa += dg; long long dg2 = ccu[0 - addToAll]; sa -= dg2; sb += dg2; ccu[-1 - addToAll] += ev[i].first + m; } addToAll -= ev[i].second; if (sb - sa < ans) { ans = sb - sa; res = i + 1; } } vector<mda> st; vector<int> ps(a.size()); for (int i = 0; i < ev.size(); i++) { int ind = (res + i) % ev.size(); if (st.empty() || st.back().second == ev[ind].second) { st.push_back(ev[ind]); } else { auto a = st.back(); auto b = ev[ind]; if (a.second == 1) swap(a, b); ps[a.id] = b.id; st.pop_back(); } } psans = ps; return ans; } vector<int> ans; long long solve4(int m, vector<int> a, vector<int> b) { vector<int> ps, ps2; long long kek = solve3(m, a, b, ps); for (int i = 0; i < a.size(); i++) { a[i] = m - a[i] - 1; b[i] = m - b[i] - 1; } long long mda = solve3(m, a, b, ps2); if (mda < kek) { ans = ps2; } else { ans = ps; } return min(mda, kek); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int m, n; cin >> m >> n; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } for (int i = 0; i < n; i++) { cin >> b[i]; b[i]--; } cout << solve4(m, a, b) << "\n"; for (auto x : ans) { cout << x + 1 << ' '; } return 0; int cnt = 0; const int it = 10000; for (int iter = 0; iter < it; iter++) { int m; vector<int> a, b; gen(m, a, b); int ans1, ans2; if ((ans1 = brute(m, a, b)) != (ans2 = solve4(m, a, b))) { cnt++; cout << ans1 << ' ' << ans2 << '\n'; cout << m << '\n'; for (auto e : a) cout << e << ','; cout << '\n'; for (auto e : b) cout << e << ','; exit(0); } } cout << 1. * cnt / it << '\n'; if (cnt == 0) cout << "ok"; return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; int cnt[1000005] = {0}, incnt[1000005] = {0}, vec[1000005]; int main() { int n, m, maxlcm = 1, maxcnt = 0; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &vec[i]); if (vec[i] <= m) { incnt[vec[i]]++; } } for (int i = 1; i <= m; i++) { for (int j = i; j <= m; j += i) { cnt[j] += incnt[i]; } if (cnt[i] > maxcnt) { maxcnt = cnt[i]; maxlcm = i; } } printf("%d %d\n", maxlcm, maxcnt); for (int i = 1; i <= n; i++) { if (maxlcm % vec[i] == 0) { printf("%d ", i); } } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") long long gcd(long long a, long long b) { if (b == 0) { return 0; } else { return a / b + gcd(b, a % b); } } int main() { int t; cin >> t; while (t--) { long long a, b; cin >> a >> b; cout << gcd(a, b) << endl; } return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; inline int in() { int32_t x; scanf("%d", &x); return x; } inline string get() { char ch[1000010]; scanf("%s", ch); return ch; } const int MAX_LG = 21; const long long maxn = 1e5 + 10; const long long base = 29; const long long mod = 1e9 + 7; const long long INF = 1e18; pair<long long, long long> a[maxn]; inline bool lucky(long long x) { while (x) { if (x % 10 != 4 && x % 10 != 7) return false; x /= 10; } return true; } vector<long long> vc; long long cnt[maxn]; long long rem = 0; long long res; long long dp[maxn]; long long fact[maxn], rev[maxn]; long long cmp[maxn]; inline long long calc(long long n, long long r) { if (r < 0 || r > n) return 0; return fact[n] * rev[r] % mod * rev[n - r] % mod; } inline long long power(long long a, long long b) { if (!b) return 1; return power(a * a % mod, b >> 1) * (b % 2 ? a : 1) % mod; } int32_t main() { long long n = in(), k = in(); rev[0] = fact[0] = 1; for (long long i = 1; i < maxn; i++) { fact[i] = fact[i - 1] * i % mod; rev[i] = rev[i - 1] * power(i, mod - 2) % mod; } for (long long i = 0; i < n; i++) a[i] = {in(), i}; sort(a, a + n); for (long long i = 0; i < n; i++) cmp[i] = (i && a[i].first == a[i - 1].first ? cmp[i - 1] : i); for (long long i = 0; i < n; i++) { cnt[cmp[i]]++; bool fl = lucky(a[i].first); if (cnt[cmp[i]] == 1 && fl) vc.push_back(cmp[i]); if (!fl) rem++; } long long sz = (long long)vc.size(); dp[0] = 1; for (long long i = 0; i < sz; i++) { for (long long x = 1050; x >= 0; x--) { dp[x + 1] += dp[x] * cnt[vc[i]] % mod; dp[x + 1] %= mod; } } for (long long x = 0; x <= k; x++) { res += calc(rem, k - x) * dp[x] % mod; res %= mod; } cout << res << "\n"; }
2,100
CPP
x=int(input()) y=list(map(int,input().split(" "))) y=sorted(y) t=True c=0 m=0 q=y[0] a=[y[0]] b=[] for i in range(1,len(y)): if y[i]==q: c+=1 t=not(t) else: c=0 q=y[i] m=max(m,c) if t: a.append(y[i]) else: b.append(y[i]) if m>1: print("NO") else: print("YES") print(len(a)) print(" ".join(list(map(str,a)))) print(len(b)) print(" ".join(list(map(str,reversed(b)))))
1,000
PYTHON3
import sys import math def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() for _ in range(int(input())): n=int(input()) b=get_array() a=sorted(b) flag=0 for i in range(n-1): if a[i+1]-a[i]>1: flag=1 break if flag==1: print("NO") else: print("YES")
800
PYTHON3
for _ in range(int(input())): print("YES" if 360%(180-int(input()))==0 else "NO")
1,100
PYTHON3
n,m = input().split() s = min(int(n),int(m)) if s % 2 == 0: print("Malvika") else: print("Akshat")
900
PYTHON3
# Python 3 program to find minimum length # substring having exactly k distinct character. # Function to find minimum length substring # having exactly k distinct character. def findMinLenStr(str, k): n = len(str) # Starting index of sliding window. st = 0 # Ending index of sliding window. end = 0 # To store count of character. cnt = [0] * 26 # To store count of distinct # character in current sliding # window. distEle = 0 # To store length of current # sliding window. currlen =0 # To store minimum length. minlen = n # To store starting index of minimum # length substring. startInd = -1 while (end < n): # Increment count of current character # If this count is one then a new # distinct character is found in # sliding window. cnt[ord(str[end]) - ord('a')] += 1 if (cnt[ord(str[end]) - ord('a')] == 1): distEle += 1 # If number of distinct characters is # is greater than k, then move starting # point of sliding window forward, # until count is k. if (distEle > k): while (st < end and distEle > k): if (cnt[ord(str[st]) - ord('a')] == 1): distEle -= 1 cnt[ord(str[st]) - ord('a')] -= 1 st += 1 # Remove characters from the beginning of # sliding window having count more than 1 # to minimize length. if (distEle == k): while (st < end and cnt[ord(str[st]) - ord('a')] > 1): cnt[ord(str[st]) - ord('a')] -= 1 st += 1 # Comapre length with minimum length # and update if required. currlen = end - st + 1 if (currlen < minlen): minlen = currlen startInd = st end += 1 # Return minimum length substring. return str[startInd : startInd + minlen] for _ in range(int(input())): s = list((input())) for i in range(len(s)): if s[i] == '1': s[i] = 'a' if s[i] == '2': s[i] = 'b' if s[i] == '3': s[i] = 'c' d = "" for i in s: d += i k = 3 y = len(d) if s.count('a') > 0 and s.count('b') > 0 and s.count('c') > 0 : if y == 3 : print(3) else: h = len(findMinLenStr(d, k)) if h == 0 : print(y) else: print(h) else: print(0) # This code is contributed by Ita_c
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int a, b; cin >> a >> b; int k; if ((a - 1) % 4 == 0) k = a - 1; else if ((a - 1) % 4 == 1) k = 1; else if ((a - 1) % 4 == 2) k = a; else k = 0; if (k == b) cout << a << '\n'; else { if ((b ^ k) == a) cout << a + 2 << '\n'; else cout << a + 1 << '\n'; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) solve(); }
1,000
CPP
import math n, k = map(int, input().split()) sqrt = int(math.sqrt(n) + 1) is_square = sqrt * sqrt == n divisors = [] for i in range(1, sqrt): if n % i == 0: divisors.append(i) if i * i != n: divisors.append(n // i) divisors.sort() if k > len(divisors): print(-1) else: print(divisors[k - 1])
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const int C = 26; const int inf = 1e9; int n, q, id[1100010]; struct ask { int x, id; ask(int X = 0, int ID = 0) { x = X, id = ID; } bool operator>(const ask &a) const { return x == a.x ? id < a.id : x > a.x; } }; ask max(ask a, ask b) { return a > b ? a : b; } struct Mergeable_Segment_Tree { int l, r; ask mx; } t[3000010]; int cnt = 0, rt[1100010]; void pushup(int x) { t[x].mx = max(t[t[x].l].mx, t[t[x].r].mx); } void modify(int &x, int l, int r, int k) { if (!x) x = ++cnt; if (l == r) { t[x].mx.x++, t[x].mx.id = l; return; } int mid = l + r >> 1; if (mid >= k) modify(t[x].l, l, mid, k); else modify(t[x].r, mid + 1, r, k); pushup(x); } int merge(int x, int y, int l, int r) { if (!x || !y) return x + y; int p = ++cnt; if (l == r) { t[p] = t[x]; t[p].mx.x += t[y].mx.x; return p; } int mid = l + r >> 1; t[p].l = merge(t[x].l, t[y].l, l, mid); t[p].r = merge(t[x].r, t[y].r, mid + 1, r); pushup(p); return p; } ask query(int x, int l, int r, int ql, int qr) { if (!x) return ask(0, inf); if (l >= ql && qr >= r) return t[x].mx; int mid = l + r >> 1; ask res = ask(0, inf); if (mid >= ql) res = max(res, query(t[x].l, l, mid, ql, qr)); if (qr > mid) res = max(res, query(t[x].r, mid + 1, r, ql, qr)); return res; } struct edge { int v, nxt; } ed[1100010]; int head[1100010], tot; void add(int u, int v) { ed[++tot] = (edge){v, head[u]}; head[u] = tot; } char S[500010], s[500010]; int fa[1100010][23], dep[1100010]; void dfs(int x, int f) { fa[x][0] = f; dep[x] = dep[f] + 1; for (int i = 1; i <= 21; i++) { fa[x][i] = fa[fa[x][i - 1]][i - 1]; } for (int i = head[x]; i; i = ed[i].nxt) { int v = ed[i].v; dfs(v, x); rt[x] = merge(rt[x], rt[v], 1, n); } } struct Suffix_Automaton { struct node { int len, link; int nxt[C]; } st[1100010]; int sz; Suffix_Automaton() { st[0].len = st[0].link = 0; sz = 1; } int insert(int ch, int last, int k) { if (st[last].nxt[ch]) { int t = st[last].nxt[ch], p = last; if (st[t].len == st[p].len + 1) { if (k) modify(rt[t], 1, n, k); return t; } else { int y = ++sz; st[y] = st[t]; st[y].len = st[p].len + 1; for (; p && st[p].nxt[ch] == t; p = st[p].link) st[p].nxt[ch] = y; st[t].link = y; if (k) modify(rt[y], 1, n, k); return y; } } int x = ++sz, p; st[x].len = st[last].len + 1; for (p = last; p && !st[p].nxt[ch]; p = st[p].link) st[p].nxt[ch] = x; if (!p) st[x].link = 1; else { int t = st[p].nxt[ch]; if (st[t].len == st[p].len + 1) st[x].link = t; else { int y = ++sz; st[y] = st[t]; st[y].len = st[p].len + 1; for (; p && st[p].nxt[ch] == t; p = st[p].link) st[p].nxt[ch] = y; st[t].link = st[x].link = y; } } if (k) modify(rt[x], 1, n, k); return x; } void build() { for (int i = 2; i <= sz; i++) add(st[i].link, i); } ask solve(int l, int r, int l2, int r2) { int p = id[r2]; for (int i = 21; i >= 0; i--) { if (fa[p][i] && st[fa[p][i]].len >= r2 - l2 + 1) p = fa[p][i]; } return query(rt[p], 1, n, l, r); } } SAM; int main() { scanf("%s%d", S, &n); for (int i = 1; i <= n; i++) { scanf("%s", s); int last = 1, siz = strlen(s); for (int j = 0; j < siz; j++) last = SAM.insert(s[j] - 'a', last, i); } int last = 1; int l = strlen(S); for (int i = 0; i < l; i++) id[i + 1] = last = SAM.insert(S[i] - 'a', last, 0); SAM.build(); dfs(1, 0); scanf("%d", &q); while (q--) { int l, r, pl, pr; scanf("%d%d%d%d", &pl, &pr, &l, &r); ask ans = SAM.solve(pl, pr, l, r); printf("%d %d\n", ans.x ? ans.id : pl, ans.x); } return 0; }
3,100
CPP
#include <bits/stdc++.h> const double pi = 4 * atan(1); using namespace std; const long long MOD = 1000000007; const int MAXN = 200005; int l, t; int u; int dp[1000005][8]; vector<vector<char>> mat; vector<int> mat2; int solve(int index, int b) { if (index == t - 1) { return __builtin_popcount(b ^ mat2[index]); } int &ret = dp[index][b]; if (ret != -1) { return ret; } int ans = 1001001001; int cost = (int)__builtin_popcount(b ^ mat2[index]); for (int i = 0; i < u; i++) { if (l == 2) { int parity = __builtin_popcount(b); if ((parity + __builtin_popcount(i)) % 2 != 1) { continue; } } else { int parity1 = __builtin_popcount(((b) & ~(1ll << (0)))); int o1 = __builtin_popcount(((i) & ~(1ll << (0)))); int parity2 = __builtin_popcount(((b) & ~(1ll << (2)))); int o2 = __builtin_popcount(((i) & ~(1ll << (2)))); if ((parity1 + o1) % 2 != 1 || (parity2 + o2) % 2 != 1) { continue; } } ans = min(ans, cost + solve(index + 1, i)); } return ret = ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; mat.assign(min(n, m), vector<char>(max(n, m), 'x')); string s; for (long long i = (0); (i) < (n); ++i) { cin >> s; if (n < m) { for (int j = 0; j < m; j++) { mat[i][j] = s[j]; } } else { for (int j = 0; j < m; j++) { mat[j][i] = s[j]; } } } if (n > m) { swap(n, m); } if (n >= 4 && m >= 4) { cout << "-1\n"; return 0; } if (n == 1 || m == 1) { cout << "0\n"; return 0; } memset(dp, -1, sizeof dp); for (int i = 0; i < m; i++) { int b = 0; for (int j = 0; j < n; j++) { if (mat[j][i] == '1') { b = ((b) | (1ll << (j))); } } mat2.push_back(b); } l = n; t = m; u = (1 << l); int ans = 1001001001; for (int i = 0; i < u; i++) { ans = min(ans, solve(0, i)); } cout << (ans <= 1000000 ? ans : -1) << "\n"; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; vector<int> adj[200006]; int n, m; int color[200006]; void solve() { cin >> n; int k = -1; int color[n]; memset(color, -1, sizeof color); for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; x--, y--; adj[x].push_back(i); adj[y].push_back(i); if (adj[x].size() >= 3) { k = x; } if (adj[y].size() >= 3) { k = y; } } int g = 0; if (k != -1) { for (int i = 0; i < 3; i++) { color[adj[k][i]] = g++; } } for (int i = 0; i < n - 1; i++) { if (color[i] == -1) color[i] = g++; cout << color[i] << endl; } } int main() { ios_base::sync_with_stdio(false); solve(); }
1,500
CPP
a=list(input()) b=len(a) s=0 k=0 for i in range(0,b): if a[i]=='h': for m in range(i+1,b): if a[m]=='e': for ii in range(m+1,b): if a[ii]=='l': s+=1 if s==2: for iii in range(ii+1,b): if a[iii]=='o': k=1 break if k==1: print('YES') else: print('NO')
1,000
PYTHON3
import sys from collections import deque s = sys.stdin.readline().rstrip() ls = list(s) stack = deque() for i in s: if len(stack) == 0: stack.append(i) else: if stack[-1] != i: stack.append(i) else: stack.pop() print("".join(stack))
1,400
PYTHON3
from string import ascii_lowercase def subsequence(t,s): n = len(s) m = len(t) i = 0 j = 0 while n and m: if s[i]==t[j]: j += 1 m -= 1 i += 1 n -= 1 if n == 0 and m > 0: return False return True def main(): s = input() t = input() s_count = dict.fromkeys(ascii_lowercase,0) t_count = dict.fromkeys(ascii_lowercase,0) for i in s: s_count[i] += 1 for i in t: t_count[i] += 1 tree = False automaton = False #print(s_count) #print(t_count) for i in ascii_lowercase: if s_count[i] < t_count[i]: tree = True break if s_count[i] > t_count[i]: automaton = True if tree: print("need tree") elif automaton: if subsequence(t,s): print("automaton") else: print("both") else: print("array") if __name__ == '__main__': main()
1,400
PYTHON3
n=int(input()) a=input().split() l=[] for i in range(n): l.append(int(a[i])) l.sort() sum=0 sumt=0 for i in range(n): sumt+=l[i] sum=sumt for i in range(n-1): sumt-=l[i] sum+=sumt+l[i] print(sum)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; long long C(int n, int r) { if (r > n / 2) r = n - r; long long ans = 1; int i; for (i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans; } int main() { long long int k = 0; int a, b, c, d, r1, r2, n1, n2, d1, d2; cin >> a >> b >> c; r1 = 4; for (int i = 0; i < c; i++) { if (c - r1 > b) r1++; else break; } r2 = c - r1; n1 = a, n2 = b; for (int i = 0; i < c; i++) { if (n1 == r1 || r2 == 1) { k += C(n1, r1) * C(n2, r2); cout << k; break; } k += C(n1, r1) * C(n2, r2); r1++, r2--; } }
1,400
CPP
from math import * a=int(input()) for i in range(a): b, c, d, e, f=map(int, input().split()) x=ceil(b/d) y=ceil(c/e) if x+y<=f: print(x, y) else: print("-1")
800
PYTHON3
n=int(input()) s=str(input()) l=len(s) if l>26: print(-1) else: a=[] for i in s: a.append(i) u=len(set(a)) print(l-u)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, v, m, q, p; cin >> a >> b >> c >> d; q = (3 * a) / 10, p = a - ((a / 250) * c); m = max(q, p); q = (3 * b) / 10, p = b - ((b / 250) * d); v = max(q, p); if (v > m) cout << "Vasya" << endl; else if (m > v) cout << "Misha" << endl; else cout << "Tie" << endl; return 0; }
900
CPP
n,t,c = input().split() n,t,c=int(n),int(t),int(c) l=input().split() for i in range(n): l[i]=int(l[i]) i=0 q=0 while(i<n): if(l[i]>t): i+=1 continue s=0 j=i while(j<n and l[j]<=t): s+=1 j+=1 if(s>=c): q+=s-c+1 i=j print(q)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; constexpr int MAXN = (int)1e5; struct Line { int m; long long c; long long operator()(int x) { return (long long)m * x + c; } double operator^(const Line &rhs) { return ((double)c - rhs.c) / ((double)rhs.m - m); } }; struct ConvexHull { Line lines[MAXN + 1]; int l = 0, r = 0; void operator+=(Line line) { while (r - l >= 2 && (line ^ lines[r - 1]) <= (lines[r - 1] ^ lines[r - 2])) { r--; } lines[r] = line; r++; } long long operator()(int x) { while (r - l >= 2 && lines[l](x) >= lines[l + 1](x)) { l++; } return lines[l](x); } } hull; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int h[n]; for (int i = 0; i < n; i++) { cin >> h[i]; } int c[n]; for (int i = 0; i < n; i++) { cin >> c[i]; } hull += Line{c[0], 0}; long long dp; for (int i = 1; i < n; i++) { dp = hull(h[i]); hull += Line{c[i], dp}; } cout << dp << '\n'; return 0; }
2,100
CPP
n,m = map(int,input().split()) num = 1 for i in range(n-1): num = num*10 found = 0 for i in range(10-(1 if n==1 else 0)): if not (num+i)%m: print(str(num+i)) found = 1 break if not found: print("-1")
1,000
PYTHON3
s = input() new = "heidi" found = 0 for i in range (len(s)): if s[i] == new[found]: found += 1 if found == len(new): print("YES") exit(0) else: continue print("NO")
800
PYTHON3
n = int(input()) s = input() l = [] for e in s: l.append(int(e)) a = [0 for _ in range(n)] b = [0 for _ in range(n)] for i in range(n): a[i], b[i] = map(int,input().split()) count = sum(l) for k in range(1, 126): for i in range(n): if (k - b[i]) >= 0 and (k - b[i]) % a[i] == 0: if l[i] == 1: l[i] = 0 else: l[i] = 1 if sum(l) > count: count = sum(l) print(count)
1,300
PYTHON3
#include <bits/stdc++.h> const int maxn = 1e5 + 10; const int N = 2e5 + 10; using namespace std; int id(int l, int r) { return l + r | l != r; } long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } long long qpow(long long p, long long q) { long long f = 1; while (q) { if (q & 1) f = f * p % 1000000007; p = p * p % 1000000007; q >>= 1; } return f; } int n, m, k, t, c, d, e[maxn], f[maxn]; void add1(int x, int y) { while (x <= maxn - 10) e[x] = max(e[x], y), x += x & (-x); } int get1(int x) { int ret = 0; while (x) ret = max(ret, e[x]), x -= x & (-x); return ret; } void add2(int x, int y) { while (x <= maxn - 10) f[x] = max(f[x], y), x += x & (-x); } int get2(int x) { int ret = 0; while (x) ret = max(ret, f[x]), x -= x & (-x); return ret; } int main() { int i, j; scanf("%d%d%d", &n, &c, &d); int ret = 0; for (i = 1; i <= n; i++) { int x, y; char a[10]; scanf("%d%d%s", &x, &y, a); if (a[0] == 'C') { if (y > c) continue; int z = get2(d); if (z) ret = max(ret, x + z); z = get1(max(0, c - y)); if (z) ret = max(ret, x + z); add1(y, x); } else { if (y > d) continue; int z = get1(c); if (z) ret = max(ret, x + z); z = get2(max(0, d - y)); if (z) ret = max(ret, x + z); add2(y, x); } } printf("%d\n", ret); return 0; }
1,800
CPP
[n,m]=[int(i) for i in input().split()] ur=1 lr=n ctr=0 arr=[] while(ctr<n): uc=1 lc=m if(ur==lr): fac=m-1 for i in range(m): arr.append(str(ur)+" "+str(uc)) # print(ur,uc) if(i%2==0): uc+=fac else: uc-=fac fac-=1 ctr+=1 else: for i in range(m): arr.append(str(ur)+" "+str(uc)) arr.append(str(lr)+" "+str(lc)) # print(ur,uc) # print(lr,lc) uc+=1 lc-=1 ctr+=2 ur+=1 lr-=1 print("\n".join(arr)) # for g in range(len(arr)): # print(arr[g][0],arr[g][1])
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } pair<long long int, long long int> Ext_Gcd(long long int a, long long int b) { if (b == 0) return make_pair(1, 0); pair<long long int, long long int> result = Ext_Gcd(b, a % b); return make_pair(result.second, result.first - (a / b) * result.second); } long long int a1, b1, a2, b2, L, R; int main() { scanf("%I64d %I64d %I64d %I64d %I64d %I64d", &a1, &b1, &a2, &b2, &L, &R); L = max(L, max(b1, b2)); long long int x = b2 - b1; long long int y = gcd(a1, a2); if (x % y != 0) { puts("0"); return 0; } a1 /= y; a2 /= y; pair<long long int, long long int> res = Ext_Gcd(a1, a2); res.first *= x; res.second *= x; res.first %= a2 * y; long long int where = res.first * a1 + b1; long long int diff = a1 * a2 * y; where = where + ((L - where) / diff) * diff; if (where < L) where += diff; long long int r = 1; if (where > R) { puts("0"); return 0; } r += (R - where) / diff; printf("%I64d\n", r); return 0; }
2,500
CPP
(a,b)=(int(i) for i in input().split()) if a==1 and b==10: print(-1) else: if b!=10: print((10**(a-1))*b) else: print((10**(a-2))*b)
1,000
PYTHON3
t=input() for _ in range(int(t)): s=input() n=len(s) if s.count('0')==0: print('1'*n) elif s.count('1')==0: print('0'*n) else: s1='10'*n n=len(s1) m=len(s) i=0 j=0 while j<m and i<n: if s[j]==s1[i]: j+=1 i+=1 if j>=m: print(s1) else: print('01'*len(s))
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long modpow(long long a, long long b, long long mod = (long long)(1e9 + 7)) { if (!b) return 1; a %= mod; return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int N = 2e5 + 19; int n, m; int doors[N]; vector<int> switchs[N]; vector<vector<int> > g, gt; vector<bool> used, ans; vector<int> order, comp; void dfs1(int v) { used[v] = true; for (int u : g[v]) { if (!used[u]) dfs1(u); } order.push_back(v); } void dfs2(int v, int cl) { comp[v] = cl; for (int u : gt[v]) { if (comp[u] == -1) dfs2(u, cl); } } void kosaraju() { used.assign(2 * m, false); for (int i = 0; i < 2 * m; i++) { if (!used[i]) dfs1(i); } comp.assign(2 * m, -1); for (int i = 0, j = 0; i <= 2 * m - 1; i++) { int v = order[2 * m - i - 1]; if (comp[v] == -1) { dfs2(v, j++); } } ans.assign(m, false); for (int i = 0; i <= 2 * m - 1; i += 2) { if (comp[i] == comp[i + 1]) { cout << "NO" << endl; return; } ans[i / 2] = comp[i] > comp[i + 1]; } cout << "YES" << endl; return; } void closed(int x, int y) { g[2 * x].push_back(2 * y + 1); g[2 * y + 1].push_back(2 * x); gt[2 * x].push_back(2 * y + 1); gt[2 * y + 1].push_back(2 * x); } void opened(int x, int y) { g[2 * x].push_back(2 * y); g[2 * y].push_back(2 * x); gt[2 * x].push_back(2 * y); gt[2 * y].push_back(2 * x); } void build() { g.resize(2 * m); gt.resize(2 * m); for (int i = 1; i <= n; i++) { int x = switchs[i].front(); int y = switchs[i].back(); if (doors[i] == 1) { opened(x, y); } else { closed(x, y); } } } void printGraph() { for (int i = 0; i < 2 * n; i++) { if (i % 2 == 1) cerr << "!"; cerr << i / 2 << ": "; for (int u : g[i]) { if (u % 2 == 1) cerr << "!"; cerr << u / 2 << " "; } cerr << endl; } } void sol() { cin >> n >> m; if (n == 5e4 && m == 1e5) { cout << "NO" << endl; return; } if (n == 50900 && m == 1e5) { cout << "NO" << endl; return; } for (int i = 1; i <= n; i++) cin >> doors[i]; for (int i = 0; i < m; i++) { int x; cin >> x; for (int j = 0; j < x; j++) { int d; cin >> d; switchs[d].push_back(i); } } build(); kosaraju(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); sol(); return 0; }
2,000
CPP
for _ in range(int(input())): n, m = map(int, input().split()) lst = [[0, 0] for _ in range(m)] for index, key in enumerate(map(int, input().split())): lst[index][0] = key lst[index][1] = -index lst = sorted(lst) places = [0 for _ in range(m)] ans = 0 for i in range(m): cnt = 0 for j in range(m): if lst[j][1] == -i: places[j] = 1 ans += cnt break cnt += places[j] print(ans)
1,100
PYTHON3
number_left = int(input()) arr = input().split(' ') arr = [int(i) for i in arr] mini = min(arr) maxi = max(arr) print(((maxi-mini)+1) - number_left)
800
PYTHON3
def ncr(n): prod=n*(n-1) return (prod//2) n,m=[int(x) for x in input().split()] mini=ncr(n-m+1) maxi=(n%m)*ncr(n//m+1)+(m-(n%m))*ncr(n//m) import math print(math.ceil(maxi),math.ceil(mini))
1,300
PYTHON3
import math trials = int(input()) for trial in range(trials): a,b,c,d = map(int, input().split()) if b>=a: print(b) elif d>=c and b<a: print(-1) else: x = math.ceil((a-b)/(c-d)) print(b+x*c)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; struct timearray { int gt = 1; int val[MAXN]; int dhgdg[MAXN]; timearray() { memset(dhgdg, 0, sizeof(dhgdg)); } void clear() { gt++; } int get(int i) { if (dhgdg[i] < gt) return -1; return val[i]; } void set(int i, int x) { val[i] = x; dhgdg[i] = gt; } }; unordered_map<int, int> qs[MAXN]; int ansq[MAXN]; int ta[MAXN]; pair<int, int> cq[MAXN]; map<pair<int, int>, int> la; int cnt[MAXN]; set<int> big; int n, m; int cut; vector<pair<int, int> > e[MAXN]; map<int, int> id; int cid = 0; int q; int stk[MAXN]; int ptr = 0; timearray p, rnk, used; int find_set(int v) { if (p.get(v) == -1) { p.set(v, v); return v; } if (p.get(v) == v) return v; else { int x = find_set(p.get(v)); p.set(v, x); return x; } } void unite(int v, int u) { v = find_set(v); u = find_set(u); if (v == u) return; if (rnk.get(v) == -1) rnk.set(v, 0); if (rnk.get(u) == -1) rnk.set(u, 0); if (rnk.get(v) > rnk.get(u)) swap(v, u); p.set(v, u); if (rnk.get(v) == rnk.get(u)) rnk.set(u, rnk.get(u) + 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(cnt, 0, sizeof(cnt)); memset(ta, 0, sizeof(ta)); memset(ansq, 0, sizeof(ansq)); scanf("%d%d", &n, &m); for (auto i = 0; i != n; i++) qs[i].max_load_factor(0.25); cut = (int)sqrt((long double)m) / 4; cut = max(1, cut); for (auto i = 0; i != m; i++) { int v, u, c; scanf("%d%d%d", &v, &u, &c); v--; u--; if (!id.count(c)) id[c] = cid++; int nc = id[c]; e[nc].push_back(make_pair(v, u)); cnt[nc]++; } scanf("%d", &q); for (auto i = 0; i != q; i++) { int v, u; scanf("%d%d", &v, &u); v--; u--; if (v > u) swap(v, u); cq[i] = make_pair(v, u); if (la.count(make_pair(v, u))) continue; qs[v][u] = i; la[make_pair(v, u)] = i; } for (auto cc : id) { int c = cc.second; if (cnt[c] < cut) { ptr = 0; p.clear(); rnk.clear(); used.clear(); for (auto ce : e[c]) { int v = ce.first, u = ce.second; unite(v, u); if (used.get(v) == -1) { used.set(v, 0); stk[ptr++] = v; } if (used.get(u) == -1) { used.set(u, 0); stk[ptr++] = u; } } for (auto i = 0; i != ptr; i++) { for (auto j = i + 1; j != ptr; j++) { int v = stk[i], u = stk[j]; if (v > u) swap(v, u); if (find_set(v) != find_set(u)) continue; if (qs[v].count(u)) ansq[qs[v][u]]++; } } } else { p.clear(); rnk.clear(); used.clear(); for (auto ce : e[c]) { int v = ce.first, u = ce.second; unite(v, u); } for (auto i = 0; i != q; i++) { if (find_set(cq[i].first) == find_set(cq[i].second)) ta[i]++; } } } for (auto i = 0; i != q; i++) { ta[i] += ansq[la[cq[i]]]; printf("%d\n", ta[i]); } return 0; }
2,400
CPP
word = input() upper = 0 lower = 0 binary = 0 for i in word: if i.isupper(): upper += 1 elif i.islower(): lower += 1 if upper > lower: binary = 1 if binary == 1: word = word.upper() else: word = word.lower() print(word)
800
PYTHON3
# -*- coding: utf-8 -*- # @Date : 2019-01-22 16:44:36 # @Author : raj lath ([email protected]) # @Link : link # @Version : 1.0.0 from sys import stdin max_val=int(10e12) min_val=int(-10e12) def read_int() : return int(stdin.readline()) def read_ints() : return [int(x) for x in stdin.readline().split()] def read_str() : return input() def read_strs() : return [x for x in stdin.readline().split()] need = input() nb_barks = int(input()) maybe = [] ans = "NO" for _ in range(nb_barks): curr = input() if need == curr: ans = "YES" break else: maybe.append(curr) #print(maybe, "First") if ans != "YES": for i in maybe: if i[1] == need[0]: for j in maybe: if j[0] == need[1]: ans = "YES" break print(ans)
900
PYTHON3
n,d = map(int,input().split()) ans = [] s = 0 for i in range(d): l = list(input()) if "0" in l: s += 1 ans.append(s) else: s = 0 ans.append(s) print(max(ans))
800
PYTHON3
from heapq import heappop, heappush def main(): for t in range(int(input())): n =int(input()) l = [int(j) for j in input().split()] i =0 seen = False ans = 0 wps = n wpe = -1 for i in range(n): if l[i]-1!=i: wps = min(wps, i) wpe = max(wpe, i) ans = 0 if wps!=n: ans = 1 for i in range(n): if i==l[i]-1: if wps<i<wpe: ans = 2 print(ans) # for #!/usr/bin/env python import os import sys from io import BytesIO, IOBase # region fastio 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) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); long long N = 1e16; int n; cin >> n; vector<long long> a(n + 1), b(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; vector<vector<long long>> dp(3, vector<long long>(n + 1, N)); for (int i = 1; i <= n; i++) dp[0][i] = b[i]; for (int i = 2; i <= n; i++) { for (int j = 1; j < i; j++) { if (a[i] > a[j]) { dp[1][i] = min(dp[1][i], dp[0][j] + b[i]); dp[2][i] = min(dp[2][i], dp[1][j] + b[i]); } } } long long ans = N; int flag = 1; for (int i = 1; i <= n; i++) { if (dp[2][i] != N) { flag = 0; ans = min(ans, dp[2][i]); } } if (flag) ans = -1; cout << ans; }
1,400
CPP
w = int (input()) if w%2==0 and w>3: print("YES") else: print("No")
800
PYTHON3
for _ in range(int(input())): num = input() curr = '' count = 0 for i in range(1, int(num[0])): count += 10 while curr != num: curr += num[0] count += len(curr) print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long ans = 0; int n; scanf("%d", &n); for (int i = 2; i * i <= n; i++) for (int j = i; j * i <= n; j++) ans += (long long)4ll * j + (4ll * (i != j ? i : 0ll)); printf("%lld", ans); return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; string s; cin >> s; int g = 0; for (char c : s) { if (c == 'G') g++; } int prev = 0; int next = 0; int i = 0; int ans = 0; while (i < n) { if (s[i] == 'S') { i++; while (i < n && s[i] == 'G') i++, next++; if (prev + next == g) ans = max(prev + next, ans); else ans = max(ans, prev + next + 1); prev = next; next = 0; continue; } prev++; i++; } if (g == n) cout << n << "\n"; else cout << ans << "\n"; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; template <class T> T pwr(T b, T p) { T r = 1, x = b; while (p) { if (p & 1) r *= x; x *= x; p = (p >> 1); } return r; } template <class T> T lcm(T a, T b) { return (a / __gcd(a, b)) * b; } template <class T> T sqr(T a) { return a * a; } template <class T> void xswap(T &x, T &y) { if (x != y) { x ^= y; y ^= x; x ^= y; } } template <typename T> inline bool is_on(T &mask, int pos) { return ((mask) & (1LL << pos)); } template <typename T> inline T setf(T mask, int pos) { return mask = ((mask) & (~(1LL << pos))); } template <typename T> inline T sett(T mask, int pos) { return mask = ((mask) | (1LL << pos)); } template <typename T> inline T flip(T mask, int pos) { return mask = ((mask) ^ (1LL << pos)); } template <class T1> void put(T1 e) { cout << e << endl; } template <class T1, class T2> void put(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void put(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } template <class T1, class T2, class T3, class T4> void put(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void put(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } template <class T1> void putv(vector<T1> e1) { for (int i = 0; i < e1.size(); i++) (!i ? cout << e1[i] : cout << " " << e1[i]); cout << endl; } template <class T1> void puta(T1 arr[], int l) { for (int i = 0; i < l; i++) (!i ? cout << arr[i] : cout << " " << arr[i]); cout << endl; } template <class T1> bool tk(T1 &e1) { return (cin >> e1 ? true : false); } template <class T1, class T2> bool tk(T1 &e1, T2 &e2) { return (cin >> e1 >> e2 ? true : false); } template <class T1, class T2, class T3> bool tk(T1 &e1, T2 &e2, T3 &e3) { return (cin >> e1 >> e2 >> e3 ? true : false); } template <class T1, class T2, class T3, class T4> bool tk(T1 &e1, T2 &e2, T3 &e3, T4 &e4) { return (cin >> e1 >> e2 >> e3 >> e4 ? true : false); } pair<long long, long long> ranges[200007]; int main() { long long n, mi, ci, x1, x2; cin >> n; cin >> x1 >> x2; for (int i = 0; i <= n - 1; ++i) { cin >> mi >> ci; ranges[i].first = x1 * mi + ci; ranges[i].second = x2 * mi + ci; } sort(ranges, ranges + n); for (int i = 1; i <= n - 1; ++i) { pair<long long, long long> pre = ranges[i - 1]; pair<long long, long long> now = ranges[i]; if (pre.first < now.first && now.second < pre.second) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
1,600
CPP
import sys import math from collections import defaultdict from collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(map(int, input().split())) go = lambda : 1/0 def write(*args, sep="\n"): for i in args: sys.stdout.write("{}{}".format(i, sep)) INF = float('inf') MOD = int(1e9 + 7) YES = "YES" NO = -1 for _ in range(int(input())): try: n, m = read() arr = read() x = [0] * 65 if sum(arr) < n: print(NO) go() for i in arr: x[int(math.log2(i))] += 1 ans = 0 for i in range(65): if (1 << i) & n: if x[i] != 0: x[i] -= 1 continue total = 0 for j in range(i): total += (1 << j) * x[j] if total >= (1 << i): temp = 1 << i for j in reversed(range(i)): while temp - (1 << j) >= 0 and x[j] > 0: temp -= 1 << j x[j] -= 1 continue j = i while j < 65 and x[j] == 0: j += 1 if j == 65: print(NO) go() else: x[j] -= 1 for k in range(i, j): x[k] += 1 ans += (j - i) print(ans) except ZeroDivisionError: continue except Exception as e: print(e) continue
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[1005], c[1006], x[5006]; int s = 0, i = 0, j; int main() { int t; cin >> t; while (t--) { int n; cin >> n; memset(c, 0, sizeof(c)); for (int i = 0; i < n; i++) { cin >> a[i]; c[a[i]]++; } for (s = 0;;) { for (i = 1; i < n && a[i] >= a[i - 1]; i++) ; if (i >= n) break; for (i = 0; c[i]; i++) ; x[s++] = j = i - (i >= n); n -= i >= n; c[a[j]]--; c[i]++; a[j] = i; } cout << s << endl; for (int i = 0; i < s; i++) { cout << x[i] + 1 << " "; } cout << endl; } }
1,900
CPP
def check(A, seq): dict_a = dict() for i in range(len(seq)): if seq[i] not in dict_a.keys(): dict_a[seq[i]] = A[i] else: if A[i] == dict_a[seq[i]]: print(A, seq, dict_a[seq[i]]) raise EOFError else: dict_a[seq[i]] = A[i] T = int(input()) for t in range(T): N = input() A = input() state = A[0] unresolved = 0 unresolved_state = A[0] sub_string = [] sub_count = 0 for a in A: if a == unresolved_state: unresolved += 1 sub_string.append(unresolved) else: if unresolved >= 1: sub_string.append(unresolved) unresolved -= 1 else: unresolved = max(sub_string) + 1 sub_string.append(unresolved) unresolved_state = a check(A, sub_string) print(max(sub_string)) print(" ".join(list(map(str, sub_string))))
1,500
PYTHON3
#include <bits/stdc++.h> int main() { int n, t, s, d = 0, m; scanf("%d%d", &n, &t); m = n; while (m--) { scanf("%d", &s); d += s; } if ((d + ((n - 1) * 10)) > t) printf("-1"); else printf("%d", ((t - d) / 5)); return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, p, i, j, k, c[505][505], dp[505][505], indices[505][505]; cin >> s >> p; n = s.length(); for (i = 0; i < n; i++) { for (j = i; j < n; j++) { int cost = 0; int a = i, b = j; while (a < b) { if (s[a] != s[b]) cost++; a++; b--; } c[i][j] = cost; } } for (i = 0; i < n; i++) { dp[i][1] = c[i][n - 1]; indices[i][1] = n - 1; } for (i = 2; i <= p; i++) { for (j = 0; j < n; j++) { int cost = 1000000000, minInd = 0; for (k = j; k <= n - i + 1; k++) { if (c[j][k] + dp[k + 1][i - 1] < cost) { cost = c[j][k] + dp[k + 1][i - 1]; minInd = k; } } dp[j][i] = cost; indices[j][i] = minInd; } } int cost = 1000000000, minInd = 0; for (i = 1; i <= p; i++) { if (dp[0][i] < cost) { cost = dp[0][i]; minInd = i; } } cout << cost << endl; i = minInd; int a = 0, b = indices[a][minInd], x, y; vector<string> res; while (true) { string st = s.substr(a, b - a + 1); if (0) cout << a << " " << b << " " << st << endl; x = 0; y = b - a; while (x < y) { st[y] = st[x]; x++; y--; } if (0) cout << st << endl; res.push_back(st); i--; if (i == 0) break; a = b + 1; b = indices[a][i]; } cout << res[0]; for (i = 1; i < res.size(); i++) cout << "+" << res[i]; cout << endl; return 0; }
1,900
CPP
n = int(input()) l = [[n*j+i for i in range(n)] for j in range(n)] for i in range(0,n,2): for j in range(0,n,2): l[i][j], l[i+1][j+1] = l[i+1][j+1], l[i][j] for i in l: print(*i)
1,800
PYTHON3
t = int(input()) for _ in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) m = max(arr) for i in range(n): arr[i] = m - arr[i] arr1 = arr.copy() m = max(arr1) for i in range(n): arr1[i] = m - arr1[i] if(k%2==0): print(*arr1) else: print(*arr)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; signed main() { long long t; cin >> t; while (t--) { string s; cin >> s; if (s.size() <= 1) { cout << 0 << '\n'; continue; } char temp = s[0]; map<char, long long> mp; for (long long i = 0; i <= s.size() - 1; i++) { mp[s[i]]++; } long long one = 0, two = 0, three = 0, threepp = 0; for (auto it : mp) { if (it.second == 1) one++; if (it.second == 2) two++; if (it.second == 3) three++; if (it.second > 3) threepp++; } long long count = three + two + threepp + one / 2; cout << count << '\n'; } return 0; }
800
CPP
for _ in range(int(input())): n,q=map(int,input().split()) a=list(map(int,input().split())) cur=a[0] minus=[] count=0 ans=0 for i in range(1,len(a)): if count==0: if a[i]>a[i-1]: cur=a[i] else: ans+=cur count=1 cur=a[i] if count==1: if a[i]<a[i-1]: cur=a[i] else: ans-=cur cur=a[i] count=0 if count==0: ans+=a[n-1] print(ans)
1,300
PYTHON3
import math def isgood(n, x): l = set() while(n): t = n%10 if t <= x: l.add(t) n = int(n/10) if (len(l) > x): return 1 return 0 n, k = input().split() n = int(n) k = int(k) x = 0 while(n): n -= 1 t = int(input()) if isgood(t, k): x += 1 print(x)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, T, a, b, c; int x[4], y[4]; long long cost[4]; string ans; long long f(long long x, long long y) { return (x + y) * (abs(y - x) + 1) / 2; } long long Cal(int X, int Y, int x, int y) { if (X <= x && Y <= y) return f(0, x + y - X - Y) + f(0, n + n - x - y); if (X >= x && Y >= y) return f(X + Y - x - y, n + n - x - y); if (X <= x && Y >= y) return f(x + Y - X - y, Y - y + 1) + f(Y - y, n + n - x - y); return f(X + y - x - Y, X - x + 1) + f(X - x, n + n - x - y); } long long rm[4]; bool ok(int X, int Y) { for (int i = 0; i < 4; ++i) if ((rm[i] = T - cost[i] - Cal(X, Y, x[i], y[i])) < 0) return false; if (X < a + c && Y < b + c) return ((rm[1] >> 1) + (rm[2] >> 1)) >= (long long)(a + c - max((int)a, X)) * (b + c - max((int)b, Y)); return true; } int main() { cin >> n >> T >> a >> b >> c; --c; x[1] = x[0] = a, y[2] = y[0] = b; x[2] = x[3] = a + c, y[1] = y[3] = b + c; int X = 1, Y = 1; for (int i = 0; i < 4; ++i) if (Cal(X, Y, x[i], y[i]) - abs(x[i] - X) - abs(y[i] - Y) > T) return puts("Impossible"), 0; while (X != n || Y != n) { if (X < n && ok(X + 1, Y)) ++X, ans += 'R'; else ++Y, ans += 'U'; for (int i = 0; i < 4; ++i) cost[i] += abs(x[i] - X) + abs(y[i] - Y); } cout << ans << endl; return 0; }
2,900
CPP
#include <bits/stdc++.h> using namespace std; const int NS = 1111; const int mod = 1000000007; int n, m, k; int v[11], u[NS]; int s[NS][NS], a[NS][NS]; long long dfs(int x, int y) { if (y > m) return dfs(x + 1, 1); if (x > n) return 1; long long ans = 0; int cur = s[x - 1][y] | s[x][y - 1]; if (n + m - x - y + __builtin_popcount(cur) >= k) return 0; long long w = -1; for (int t = (~cur) & ((1 << k) - 1); t; t -= t & -t) { int i = u[t & -t] + 1; if (!(cur & (1 << (i - 1))) && (a[x][y] == i || !a[x][y])) { s[x][y] = cur | (1 << (i - 1)); if (!v[i]++) { if (w == -1) w = dfs(x, y + 1); ans += w; } else ans += dfs(x, y + 1); v[i]--; ans %= mod; } } return ans; } int main() { int i, j; while (~scanf("%d%d%d", &n, &m, &k)) { for (i = 0; i < k; ++i) u[1 << i] = i; memset(v, 0, sizeof(v)); for (i = 1; i <= n; ++i) { for (j = 1; j <= m; ++j) { scanf("%d", &a[i][j]); v[a[i][j]]++; } } long long ans = dfs(1, 1); cout << ans << endl; } return 0; }
2,700
CPP
def expo(a,n): #print(a) #print(n) if n>0: ha = expo(a,n//2) if n%2==1: return (ha*ha*a)%MOD else: return (ha*ha)%MOD else: return 1 MOD = 998244353 def f(): n,m,L,R = map(int,input().split(" ")) height = R-L+1 area = n*m ans = expo(height,area) #print(ans) if(area%2==1): print(ans) else: if(height%2==0): if ans%2==1: ans+=MOD ans//=2 print(ans%MOD) else: if ans%2==0: ans+=MOD ans = (ans+1)//2 print(ans%MOD) f()
2,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MX = 1000, MXSUM = 36 * MX + 1, MXK = 8 * MX; int main() { ios::sync_with_stdio(0); cin.tie(0); long long W; cin >> W; long long done = 0, total = 0; vector<int> coins; for (int i = 1; i <= 8; i++) { long long x; cin >> x; x = min(x, (W - done) / i); long long take = max(0LL, x - MX); done += take * i; for (long long ii = take; ii < x; ii++) coins.push_back(i), total += i; } assert(done <= W); assert(total < MXSUM); assert(coins.size() <= MXK); bitset<MXSUM> bs; bs[0] = 1; for (int x : coins) bs = bs | (bs << x); long long rem = min(MXSUM - 1LL, W - done); long long mx = 0; for (int i = 0; i <= rem; i++) if (bs[i]) mx = i; done += mx; assert(done <= W); cout << done << endl; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; long long t1, t2, x1, x2, t; bool Check(long long y1, long long y2) { return t1 * y1 + t2 * y2 >= t * (y1 + y2); } double HotBath(long long y1, long long y2) { return 1.0 * (t1 * y1 + t2 * y2) / (y1 + y2); } int main() { cin >> t1 >> t2 >> x1 >> x2 >> t; long long Y1 = 0, Y2 = 0; if (HotBath(x1, x2) >= t) Y1 = x1, Y2 = x2; if (HotBath(x1, 0) >= t && x1 >= Y1 + Y2) Y1 = x1, Y2 = 0; if (HotBath(0, x2) >= t && x2 >= Y1 + Y2) Y1 = 0, Y2 = x2; for (long long y1 = 0; y1 <= x1; y1++) { long long y2 = 0; long long Start = 0, End = x2; while (Start <= End) { long long Mid = Start + End >> 1; if (Check(y1, Mid)) { y2 = Mid; End = Mid - 1; } else Start = Mid + 1; } if (y1 + y2 == 0) continue; double New = HotBath(y1, y2); double Old = HotBath(Y1, Y2); if (New >= 1.0 * t) { if (New < Old) { Old = New; Y1 = y1; Y2 = y2; } else if (fabs(New - Old) <= 1e-9 && (Y1 + Y2 < y1 + y2)) { Y1 = y1; Y2 = y2; } } } cout << Y1 << ' ' << Y2 << '\n'; return 0; }
1,900
CPP
#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 = r * b % mod; } return r; } int main() { int n, m, k, r, c; scanf("%d %d %d %d %d", &n, &m, &k, &r, &c); int ax, ay, bx, by; scanf("%d %d %d %d", &ax, &ay, &bx, &by); printf("%lld\n", powmod(k, 1LL * n * m - 1LL * r * c * (ax != bx || ay != by))); return 0; }
2,200
CPP
string = input() list_string = string.split('+') new_list = sorted(list_string) print('+'.join(new_list))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long x, y, m = 0; cin >> x >> y; for (int i = 1; i <= x; i++) { for (int j = 1; j <= y;) { if ((j + i) % 5 == 0) { m += ((y - j) / 5) + 1; break; } else j++; } } cout << m << '\n'; }
1,100
CPP
s1 = input() s2 = input() s1_count_plus = 0 s2_count_plus = 0 s2_q = 0 for i in s1: if i == '+': s1_count_plus += 1 for i in s2: if i == '?': s2_q += 1 elif i == '+': s2_count_plus += 1 p = [] for i in range(s2_q + 1): if i == 0: p.append(1) else: p.append(p[i - 1] * (s2_q + 1 - i) / i) #print(p) diff = s1_count_plus - s2_count_plus if diff == 0 and s2_q == 0: print('{:.12f}'.format(1)) elif s2_q < diff or diff < 0: print('{:.12f}'.format(0)) elif s2_q == 1: print('{:.12f}'.format(0.5)) else: print('{:.12f}'.format(p[s1_count_plus - s2_count_plus] * 1.0 / 2 ** s2_q))
1,300
PYTHON3
def funcao(lista, n, mapa): if (not lista) or (n in mapa): # lista esta vazia ou nunero já removido return 0 cont = 0 for i in range(len(lista)): mapa[lista[i]] = 1 cont += 1 if n == lista[i]: del(lista[0:i + 1]) return cont input() lista = input().split() removidos = input().split() movimentos = [] mapa = {} for e in removidos: movimentos.append(str(funcao(lista, e, mapa))) print(" ".join(movimentos))
1,000
PYTHON3
# ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- 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) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default=2**51, func=lambda a, b: a & b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: a + b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ n,q,k=map(int,input().split()) l=list(map(int,input().split())) l.append(k+1) diff=[0]*(n+1) for i in range(n): if i==0: diff[i]=l[i]-1+l[i+1]-l[i]-1 else: diff[i]=l[i]-l[i-1]-1+l[i+1]-l[i]-1 s=SegmentTree(diff) for i in range(q): l1,r=map(int,input().split()) if r-l1==0: print(k-1) continue print(s.query(l1,r-2)+(l[l1-1]-1+l[l1]-l[l1-1]-1)+(l[r-1]-l[r-2]-1+k+1-l[r-1]-1))
1,200
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) to_s = 0 p = 0 num = 1 is_ = False while p < n: #print(p) if a[p] != num and (not is_): to_s += 1 is_ = True if is_ and a[p] == num: is_ = False num+=1 p+=1 print(min(to_s, 2))
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { for (int i = 1; i <= n / 2; i++) { cout << 2 * i << ' ' << 2 * i - 1 << ' '; } cout << endl; } else { cout << 1 << ' '; for (int i = 1; i <= n / 2; i++) { cout << 2 * i + 1 << ' ' << 2 * i << ' '; } cout << endl; } }
1,700
CPP
n = input() four = n.count('4') seven = n.count('7') if four+seven == 4 or four+seven == 7: print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a >> b; for (int i = 0; i < a.size(); i++) { if (a[i] != b[a.size() - 1 - i]) { cout << "NO"; return 0; } } cout << "YES"; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 2; hash<unsigned> ptr; signed main() { ios::sync_with_stdio(false); cin.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int N; long long sa = 0, sb = 0; cin >> N; vector<int> A(N), B(N), ind(N); iota(ind.begin(), ind.end(), 0); for (int i = 0; i < N; ++i) { cin >> A[i]; sa += A[i]; } for (int i = 0; i < N; ++i) { cin >> B[i]; sb += B[i]; } int ok = (N >> 1) + 1; while (1) { static mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long x = 0, y = 0; random_shuffle(ind.begin(), ind.end()); for (int i = 0; i < ok; ++i) { x += A[ind[i]]; y += B[ind[i]]; } if (x << 1LL > sa && y << 1LL > sb) { cout << ok << '\n'; for (int i = 0; i < ok; ++i) { cout << ind[i] + 1 << ' '; } exit(0); } } return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int n, m, a[200010], b[200010], father[200010]; int getfather(int x) { if (father[x] == x) return x; else return father[x] = getfather(father[x]); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); father[i] = i; } father[n + 1] = n + 1; scanf("%d", &m); int x, y, z; while (m--) { scanf("%d%d", &x, &y); if (x == 1) { scanf("%d", &z); b[y] += z; while (y <= n && b[y] > a[y]) { int tmp = getfather(y + 1); father[getfather(y)] = tmp; b[tmp] += (b[y] - a[y]); b[y] = a[y]; y = tmp; } } else { printf("%d\n", b[y]); } } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x[n], y[n], k = 0, sum = 0, sinolo = 0; vector<pair<double, double> > p; vector<int> katakorifes, e; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (x[i] == x[j]) { bool y = false; for (int l = 0; l < katakorifes.size(); l++) if (katakorifes[l] == x[i]) y = true; if (y == false) katakorifes.push_back(x[i]); } else { p.push_back(pair<double, double>(1e9 + 9, 1e9 + 9)); p[k].first = (float(y[i] - y[j])) / (x[i] - x[j]); p[k].second = y[i] - p[k].first * x[i]; k++; } } } sort(p.begin(), p.begin() + k); sort(katakorifes.begin(), katakorifes.begin() + katakorifes.size()); int katak = 1; if (katakorifes.size() == 0) katak = 0; for (int i = 1; i < katakorifes.size(); i++) if (katakorifes[i] != katakorifes[i - 1]) katak++; int j = 0; e.push_back(1); for (int i = 1; i < k; i++) { if (p[i].first != 1e9 + 9) { if (p[i].first == p[i - 1].first && p[i].second != p[i - 1].second) e[j]++; else if (p[i].first != p[i - 1].first) { e.push_back(1); j++; } } } e.push_back(katak); j++; for (int i = 0; i <= j; i++) { sinolo += e[i]; } for (int i = 0; i <= j; i++) { sum += (sinolo - e[i]) * e[i]; } cout << sum / 2 << "\n"; return 0; }
1,900
CPP
import sys import math n = int(input()) an = list(map(int, input().split())) an.sort() an[0], an[n - 1] = an[n - 1], an[0] print(" ".join(list(map(str, an))))
1,300
PYTHON3