solution
stringlengths 10
159k
| difficulty
int64 0
3.5k
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a * (b / (gcd(a, b))); }
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
inline long long toLL(string s) {
long long v;
istringstream sin(s);
sin >> v;
return v;
}
template <class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
using namespace std;
vector<int> felNos;
int main() {
int N, E;
int a, b, c, d;
cin >> N >> E;
if (N >= E || N == 4) {
cout << "-1";
return 0;
}
cin >> a >> b >> c >> d;
for (int i = 1; i <= N; i++) {
if (i != a && i != b && i != c && i != d) felNos.push_back(i);
}
int sz = ((int)((felNos).size()));
printf("%d %d", a, c);
for (int i = 0; i < sz; i++) printf(" %d", felNos[i]);
printf(" %d %d", d, b);
printf("\n");
printf("%d %d", c, a);
for (int i = 0; i < sz; i++) printf(" %d", felNos[i]);
printf(" %d %d", b, d);
}
| 1,600 | CPP |
def main():
n = int(input())
a, b = 1, 1
out = []
for i in range(1, n + 1):
while b < i:
a, b = b, a + b
if b == i:
out.append('O')
else:
out.append('o')
print(''.join(out))
main()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n;
cin >> n;
cout << ceil(log2(n + 1));
}
| 1,300 | CPP |
n = int(input())
tab = list(map(int, input().split()))
if all([x == 1 for x in tab]):
print('1 ' * (n - 1) + '2')
else:
print(' '.join(['1'] + list(map(str, sorted(tab)[:-1]))))
| 1,300 | PYTHON3 |
#20A - BerOS system
#Firstly, we take the input from the user
path = input()
i = 0
empty = None
#If the input is empty, we set the empty variable to be true
if len(path) == 0:
empty = True
#We iterate through the input, on the condition that it's not empty
while i != len(path)-1 and not empty:
#If the input only consists of a single '/',
#we terminate the iteration
if path == '/':
break
#Else, we keep looking for 2 '/' characters
#next to each other. When finding such case,
#we get rid of the make the path[] overlook the 2nd one.
#It will keep doing this, till there are no 2 '/' characters
#next to each other
if path[i] == '/' and path[i+1] == '/':
path = path[:i] + path[i+1:]
i -= 1
i += 1
#Time to print, considering different input possibilities
if 'root/' == path[-5:]:
print(path)
elif empty:
print('')
elif path == '/':
print('/')
elif path[-1] == '/':
print(path[:-1])
else:
print(path) | 1,700 | PYTHON3 |
import math
from collections import defaultdict, Counter, deque
INF = float('inf')
def gcd(a, b):
while b:
a, b = b, a%b
return a
def primeFactor(n):
if n % 2 == 0:
return 2
i = 3
while (i ** 2) <= n:
if n % i == 0:
return i
i += 1
return n
def main():
n = int(input())
if n % 2 == 0: print('Mahmoud')
else: print('Ehab')
if __name__ == "__main__":
# t = int(input())
t = 1
for _ in range(t):
main()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long h;
int n, m, k, c[100005];
long long a[100005];
vector<long long> setofk;
bool done[100005];
long long d[10004];
set<pair<long long, int> > heap;
void dijkstra() {
for (int i = 0; i < k; i++) d[i] = (long long)1e18;
d[1] = 1;
heap.clear();
heap.insert(make_pair(1, 1));
while (!heap.empty()) {
int u = heap.begin()->second;
heap.erase(heap.begin());
for (int i = 0; i < setofk.size(); i++) {
long long dis = d[u] + setofk[i];
int v = dis % k;
if (dis < d[v]) {
if (d[v] != (long long)1e18) heap.erase(make_pair(d[v], v));
d[v] = dis;
heap.insert(make_pair(d[v], v));
}
}
}
}
struct node {
int val, ind;
} seg[(1 << 17) << 1];
void init() {
for (int i = (1 << 17) - 1; i > 0; i--) {
seg[i].val = max(seg[i << 1].val, seg[i << 1 | 1].val);
if (seg[i].val == seg[i << 1].val)
seg[i].ind = seg[i << 1].ind;
else
seg[i].ind = seg[i << 1 | 1].ind;
}
}
void update(int ind, int inc) {
seg[(1 << 17) + ind].val += inc;
for (int i = ((1 << 17) + ind) >> 1; i > 0; i >>= 1) {
seg[i].val = max(seg[i << 1].val, seg[i << 1 | 1].val);
if (seg[i].val == seg[i << 1].val)
seg[i].ind = seg[i << 1].ind;
else
seg[i].ind = seg[i << 1 | 1].ind;
}
}
void solve1(long long x) {
setofk.push_back(x);
dijkstra();
for (int i = 1; i <= n; i++) {
int mod = a[i] % k;
if (a[i] >= d[mod] and !done[i]) {
done[i] = 1;
update(i, c[i]);
}
}
}
void solve2(int x, int y) {
c[x] -= y;
if (done[x]) update(x, -y);
}
int solve3() {
int ans = seg[1].val;
int i = seg[1].ind;
update(i, -c[i]);
return ans;
}
int main() {
cin >> h >> n >> m >> k;
for (int i = 1; i <= n; i++) cin >> a[i] >> c[i];
setofk.push_back(k);
dijkstra();
for (int i = 1; i <= n; i++) {
int mod = a[i] % k;
if (a[i] >= d[mod]) {
done[i] = 1;
update(i, c[i]);
}
}
for (int i = 0; i <= n; i++) seg[(1 << 17) + i].ind = i;
init();
while (m--) {
int id;
cin >> id;
if (id == 1) {
long long x;
cin >> x;
solve1(x);
} else if (id == 2) {
int x, y;
cin >> x >> y;
solve2(x, y);
} else
cout << solve3() << "\n";
}
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
double n, k, tot, x[2], y[2];
int main() {
cin >> n >> k;
cin >> x[0] >> y[0];
while (--n) {
cin >> x[1] >> y[1];
tot += sqrt((abs(x[1] - x[0]) * abs(x[1] - x[0])) +
(abs(y[1] - y[0]) * abs(y[1] - y[0])));
x[0] = x[1];
y[0] = y[1];
}
tot *= k / 50;
cout << setprecision(9) << tot << endl;
return 0;
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int p[300005], far, dist;
int d[300005];
vector<int> G[300005];
void dfs(int v, int fat, int c, int d = 0) {
p[v] = c;
if (d > dist) dist = d, far = v;
for (int x : G[v])
if (x != fat) dfs(x, v, c, d + 1);
}
int f(int a) {
if (p[a] == a) return a;
return p[a] = f(p[a]);
}
void me(int a, int b) {
int x = f(a), y = f(b);
p[x] = y;
d[y] = max({d[y], d[x], (d[y] + 1) / 2 + (d[x] + 1) / 2 + 1});
}
int main() {
int n, m, q, a, b;
scanf("%d%d%d", &n, &m, &q);
while (m--) {
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (!p[i]) {
dist = -1;
dfs(i, i, i);
dist = -1;
dfs(far, far, i);
d[i] = dist;
}
}
while (q--) {
scanf("%d%d", &m, &a);
if (m == 1)
printf("%d\n", d[f(a)]);
else {
scanf("%d", &b);
if (f(a) != f(b)) me(a, b);
}
}
return 0;
}
| 2,100 | CPP |
import os,io
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
b = [(a[i]-a[i-1],i-1) for i in range(1,n)]
b = sorted(b)[::-1]
b = [b[i][1]+1 for i in range(k-1)]
b = sorted(b)
b.append(n)
s = 0
st = 0
for i in b:
s += a[i-1]-a[st]
st = i
print(s) | 1,400 | PYTHON3 |
x, y , z = map(int,input().split())
if x + z > y and y + z < x:
print ('+')
elif y + z > x and x + z < y:
print('-')
elif z == 0 and x == y:
print(0)
else:
print('?')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
void RI() {}
template <typename... T>
void RI(int& head, T&... tail) {
scanf("%d", &head);
RI(tail...);
}
int n;
pair<int, int> x[5010];
int main() {
RI(n);
for (int i = 0; i < int(n); i++) RI(x[i].first, x[i].second);
sort(x, x + n);
int t = 0;
for (int i = 0; i < int(n); i++) {
if (x[i].second >= t)
t = x[i].second;
else
t = x[i].first;
}
printf("%d\n", t);
return 0;
}
| 1,400 | CPP |
from sys import stdin as cin
t = int( input() )
for i in range(t):
a = list(map(int, cin.readline().split(" ")))
a.sort()
if a[0] < 2:
print("YES")
elif a[1] <= 2:
print("YES")
else:
print("NO")
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const long double scale = 1e+9;
long long gcd(long long a, long long b) {
if (b > a) {
return gcd(b, a);
}
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
long long expo(long long a, long long b, long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % mod;
a = (a * a) % mod;
b = b >> 1;
}
return res;
}
long long mminvprime(long long a, long long b) { return expo(a, b - 2, b); }
bool revsort(long long a, long long b) { return a > b; }
void swap(int &x, int &y) {
int temp = x;
x = y;
y = temp;
}
long long combination(long long n, long long r, long long m, long long *fact,
long long *ifact) {
long long val1 = fact[n];
long long val2 = ifact[n - r];
long long val3 = ifact[r];
return (((val1 * val2) % m) * val3) % m;
}
void google(int t) { cout << "Case #" << t << ": "; }
vector<long long> sieve(int n) {
int *arr = new int[n + 1]();
vector<long long> vect;
for (int i = 2; i <= n; i++)
if (arr[i] == 0) {
vect.push_back(i);
for (int j = 2 * i; j <= n; j += i) arr[j] = 1;
}
return vect;
}
long long mod_add(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
long long mod_mul(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
long long mod_sub(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
long long mod_div(long long a, long long b, long long m) {
a = a % m;
b = b % m;
return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}
long long phin(long long n) {
long long number = n;
if (n % 2 == 0) {
number /= 2;
while (n % 2 == 0) n /= 2;
}
for (long long i = 3; i <= sqrt(n); i += 2) {
if (n % i == 0) {
while (n % i == 0) n /= i;
number = (number / i * (i - 1));
}
}
if (n > 1) number = (number / n * (n - 1));
return number;
}
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0) return false;
return true;
}
long long nextPrime(long long N) {
if (N <= 1) return 2;
long long prime = N;
bool found = false;
while (!found) {
prime++;
if (isPrime(prime)) found = true;
}
return prime;
}
bool palin(string s, long long i, long long j) {
if (i > j) return false;
while (i < j) {
if (s[i] != s[j]) return false;
i++;
j--;
}
return true;
}
bool isperfect(long double x) {
if (x >= 0) {
long long s = sqrt(x);
return (s * s == x);
}
return false;
}
bool cmp(pair<long long, long long> a, pair<long long, long long> b) {
return a.first < b.first;
}
long long power(long long num, long long p, long long m) {
long long x = 1;
while (p > 0) {
if (p & 1) {
x = (x * num) % m;
num--;
}
num = (num * num) % m;
p /= 2;
}
return x % m;
}
vector<long long> factor(long long x) {
vector<long long> res;
for (long long i = 2; i * i <= x; i++) {
if (x % i == 0) res.push_back(i);
while (x % i == 0) {
x /= i;
}
}
if (x != 1) res.push_back(x);
return res;
}
long long inverseFact(long long n) {
return power(n, 998244353 - 2, 998244353);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
string s, t;
cin >> s >> t;
long long i = 0;
long long res = 0, res2 = 0;
while (i < n) {
if ((s[i] == '1' && t[i] == '0') || (t[i] == '1' && s[i] == '0')) {
res += 2;
i++;
continue;
} else if ((s[i] == '1' && t[i] == '1')) {
if (i == n - 1) break;
if ((s[i + 1] == '0' && t[i + 1] == '1') ||
(s[i + 1] == '1' && t[i + 1] == '0')) {
i += 2;
res += 2;
} else if (s[i + 1] == '0' && t[i + 1] == '0') {
i += 2;
res += 2;
} else
i++;
} else {
if (i == n - 1) {
res += 1;
break;
}
if (s[i + 1] == '1' && t[i + 1] == '1') {
res += 2;
i += 2;
} else {
res += 1;
i++;
}
}
}
reverse(s.begin(), s.end());
reverse(t.begin(), t.end());
i = 0;
while (i < n) {
if ((s[i] == '1' && t[i] == '0') || (t[i] == '1' && s[i] == '0')) {
res2 += 2;
i++;
} else if ((s[i] == '1' && t[i] == '1')) {
if (i == n - 1) break;
if ((s[i + 1] == '0' && t[i + 1] == '1') ||
(s[i + 1] == '1' && t[i + 1] == '0')) {
i += 2;
res2 += 2;
} else if (s[i + 1] == '0' && t[i + 1] == '0') {
i += 2;
res2 += 2;
} else
i++;
} else {
if (i == n - 1) {
res2 += 1;
break;
}
if (s[i + 1] == '1' && t[i + 1] == '1') {
res2 += 2;
i += 2;
} else {
res2 += 1;
i++;
}
}
}
cout << max(res2, res) << "\n";
}
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
long long res = 1;
a %= 1000000007;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % 1000000007 % 1000000007;
a = a * a % 1000000007;
}
return res;
}
vector<pair<long long, long long> > ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long i, j, k, n, q, t, m;
string s;
cin >> t;
while (t--) {
cin >> n >> k;
cin >> s;
ans.clear();
long long op = 0;
string tp;
for (i = 0; i < 2 * (k - 1); i++) {
if (i % 2 == 0) {
if (s[i] == '(') {
continue;
} else {
for (j = i + 1; j < n; j++) {
if (s[j] == '(' && s[j + 1] != ')') {
break;
}
}
op++;
ans.push_back(make_pair(i + 1, j + 1));
reverse(s.begin() + i, s.begin() + j + 1);
}
} else {
if (s[i] == ')') {
continue;
} else {
for (j = i + 1; j < n; j++) {
if (s[j] == ')' && s[j - 1] != '(') {
break;
}
}
op++;
ans.push_back(make_pair(i + 1, j + 1));
reverse(s.begin() + i, s.begin() + j + 1);
}
}
}
for (i = 2 * (k - 1); i < n - 1; i++) {
if (i == 2 * (k - 1)) {
if (s[i] == '(') {
continue;
} else {
for (j = i + 1; j < n; j++) {
if (s[j] == '(') {
break;
}
}
op++;
ans.push_back(make_pair(i + 1, j + 1));
reverse(s.begin() + i, s.begin() + j + 1);
}
} else {
if ((i - 2 * (k - 1)) % 2 == 1) {
if (s[i] == '(') {
continue;
} else {
for (j = i + 1; j < n; j++) {
if (s[j] == '(') {
break;
}
}
op++;
ans.push_back(make_pair(i + 1, j + 1));
reverse(s.begin() + i, s.begin() + j + 1);
}
} else {
if (s[i] == ')') {
continue;
} else {
for (j = i + 1; j < n; j++) {
if (s[j] == ')') {
break;
}
}
op++;
ans.push_back(make_pair(i + 1, j + 1));
reverse(s.begin() + i, s.begin() + j + 1);
}
}
}
}
cout << op << "\n";
for (i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << "\n";
}
}
return 0;
}
| 1,700 | CPP |
n, m=map(int, input().split())
t,i=0,0
for v in range(m):
p=input()
if "0" in p:
i+=1
if i>t:
t=i
else:
i=0
print(t) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int ss[maxn];
int n, dd[maxn];
bool vis[maxn];
int main() {
scanf("%d", &n);
memset(vis, 0, sizeof(vis));
memset(ss, 0, sizeof(ss));
for (int i = 1; i <= n; i++) {
scanf("%d", &dd[i]);
}
queue<int> Q;
Q.push(1);
vis[1] = 1;
while (!Q.empty()) {
int tmp = Q.front();
Q.pop();
if (tmp > 2 && !vis[tmp - 1]) {
ss[tmp - 1] = ss[tmp] + 1;
vis[tmp - 1] = 1;
Q.push(tmp - 1);
}
if (tmp < n && !vis[tmp + 1]) {
ss[tmp + 1] = ss[tmp] + 1;
vis[tmp + 1] = 1;
Q.push(tmp + 1);
}
if (!vis[dd[tmp]] && dd[tmp] != tmp) {
ss[dd[tmp]] = ss[tmp] + 1;
vis[dd[tmp]] = 1;
Q.push(dd[tmp]);
}
}
printf("%d", ss[1]);
for (int i = 2; i <= n; i++) {
printf(" %d", ss[i]);
}
printf("\n");
return 0;
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc, i, j, k;
int a, b, c;
cin >> a >> b >> c;
int n;
cin >> n;
vector<int> v(n);
for (i = 0; i < n; i++) cin >> v[i];
sort((v).begin(), (v).end());
int mx = 0;
for (i = 0; i < n; i++) {
if (b < a and c > a) {
if (v[i] > b and v[i] < c) mx++;
continue;
}
if (b > a) {
if (v[i] < b) mx++;
continue;
}
if (a > c) {
if (v[i] > c) mx++;
continue;
}
}
cout << mx;
return 0;
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
static const int N = 50;
long long a[N + 1];
int k = 1;
void print(int x) {
if (k) {
printf("%i", x);
k = 0;
return;
}
printf(" %i", x);
}
void funk(int n, long long k, int i) {
if (!n) {
printf("\n");
return;
}
if (a[n - 1] >= k) {
print(i);
funk(n - 1, k, i + 1);
} else {
print(i + 1);
print(i);
funk(n - 2, k - a[n - 1], i + 2);
}
}
int main() {
a[0] = 1;
a[1] = 1;
for (int i = 2; i <= N; i++) a[i] = a[i - 1] + a[i - 2];
int n;
long long k;
scanf("%i", &n);
cin >> k;
funk(n, k, 1);
return 0;
}
| 1,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
long long p[100001], sum = 0;
for (int i = 0; i <= n; i++) {
cin >> p[i];
}
sort(p, p + n);
for (int i = 0; i < n - 1; i++) {
sum += p[i];
}
if (((sum + p[n - 1]) & 1) == 1 || sum < p[n - 1])
cout << "NO";
else
cout << "YES" << endl;
return 0;
}
| 1,500 | CPP |
k=list(map(int,input().split()))
n=input()
s=k[0]*n.count('1')+k[1]*n.count('2')+k[2]*n.count('3')+k[3]*n.count('4')
print(s) | 800 | PYTHON3 |
k = int(input())
l = int(input())
m = int(input())
n = int(input())
d = int(input())
dragon = 0
for i in range(1, d+1):
if i % k == 0:
dragon += 1
elif i % l == 0:
dragon += 1
elif i % m == 0:
dragon += 1
elif i % n == 0:
dragon += 1
print(dragon)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<long long int> fact(200005, 1);
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
long long int modInverse(long long int n, long long int p) {
return power(n, p - 2, p);
}
long long int nCrModPFermat(long long int n, long long int r, long long int p) {
if (n < r) return 0;
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) %
p) %
p;
}
void solve() {
long long int n;
cin >> n;
long long int a[n];
long long int m = INT_MIN;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
m = max(m, a[i]);
}
long long int mx = 0, d = 0;
for (long long int i = 0; i < n; i++) {
if (a[i] == m) {
mx++;
}
if (abs(a[i] - m) > 1) {
d++;
}
}
if (mx > 1) {
cout << fact[n];
return;
}
if (d + mx == n) {
cout << 0;
return;
}
long long int ans = 0;
for (long long int i = 0; i <= d; i++) {
ans +=
((fact[n - 1 - i] % 998244353) *
(fact[i] % 998244353 * nCrModPFermat(d, i, 998244353) % 998244353)) %
998244353;
}
long long int x = (fact[n] % 998244353 - ans % 998244353) % 998244353;
if (x < 0) x += 998244353;
cout << x;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
cin >> t;
for (long long int i = 1; i <= 200005; i++) {
fact[i] = (fact[i - 1] % 998244353 * i) % 998244353;
}
while (t--) {
solve();
cout << "\n";
}
return 0;
}
| 1,500 | CPP |
n = int(input())
a = list(map(int, input().split()))
cnt = 0
while max(a[1:]) >= a[0]:
i = 0
for j in range(len(a)):
if a[j] == max(a):
i = j
a[i] -= 1
a[0] += 1
cnt += 1
print(cnt)
| 1,200 | PYTHON3 |
t = int(input())
for testcase in range(t):
str = input()
s=""
n = len(str)
if len(str)==2:
print(str,'\n')
else:
for i in range(0,n,+2):
s+=str[i]
print(s+str[n-1],'\n')
| 800 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
inline long long input() {
long long n;
cin >> n;
return n;
}
long long poww(long long a, long long b, long long md) {
return (!b ? 1
: (b & 1 ? a * poww(a * a % md, b / 2, md) % md
: poww(a * a % md, b / 2, md) % md));
}
const int MAXN = 100 + 10;
const int MAXLG = 18;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const long long INF = 8e18;
long long dp[MAXN][MAXN][MAXN];
long long ps[MAXN][MAXN][MAXN];
string str[MAXN][MAXN][MAXN];
string s1, s2, virus;
long long f[MAXN][26];
long long pi[MAXN];
long long ans;
string res;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> s1 >> s2 >> virus;
for (int i = 1; i < virus.size(); i++) {
long long j = pi[i - 1];
while (j and virus[j] != virus[i]) j = pi[j - 1];
if (virus[i] == virus[j]) j++;
pi[i] = j;
}
for (int i = 0; i < virus.size(); i++) {
for (int c = 0; c < 26; c++) {
if (i and c != virus[i] - 'A') {
f[i][c] = f[pi[i - 1]][c];
} else {
f[i][c] = i + (c == virus[i] - 'A');
}
}
}
s1 = "#" + s1;
s2 = "#" + s2;
for (int i = 1; i < s1.size(); i++) {
for (int j = 1; j < s2.size(); j++) {
for (int k = 0; k < virus.size(); k++) {
ps[i][j][k] = ps[i - 1][j - 1][k];
str[i][j][k] = str[i - 1][j - 1][k];
ps[i][j][k] = max(ps[i][j][k], ps[i - 1][j][k]);
if (ps[i][j][k] == ps[i - 1][j][k]) str[i][j][k] = str[i - 1][j][k];
ps[i][j][k] = max(ps[i][j][k], ps[i][j - 1][k]);
if (ps[i][j][k] == ps[i][j - 1][k]) str[i][j][k] = str[i][j - 1][k];
if (s1[i] != s2[j]) continue;
string tmp;
for (int l = 0; l < virus.size(); l++) {
long long x = f[l][s1[i] - 'A'];
if (x == virus.size()) continue;
if (x == k) {
dp[i][j][k] = max(dp[i][j][k], ps[i - 1][j - 1][l] + 1);
if (dp[i][j][k] == ps[i - 1][j - 1][l] + 1)
tmp = str[i - 1][j - 1][l] + s1[i];
}
}
ps[i][j][k] = max(dp[i][j][k], ps[i][j][k]);
if (ps[i][j][k] == dp[i][j][k]) str[i][j][k] = tmp;
}
}
}
for (int i = 0; i < virus.size(); i++) {
if (ps[s1.size() - 1][s2.size() - 1][i] > ans) {
ans = ps[s1.size() - 1][s2.size() - 1][i];
res = str[s1.size() - 1][s2.size() - 1][i];
}
}
if (ans == 0) {
cout << 0 << "\n";
return 0;
}
cout << res << "\n";
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long gcdf(long long a, long long b) {
while (a != 0 && b != 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
return max(a, b);
}
long long power(long long a, long long b, long long m) {
long long ans = 1;
while (b) {
if (b & 1) ans = (ans * a) % m;
b /= 2;
a = (a * a) % m;
}
return ans;
}
void boost() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main() {
boost();
int t;
cin >> t;
while (t--) {
long long n, m, i;
cin >> n >> m;
long long a = max(n, m);
long long b = min(n, m);
if ((2 * b - a) % 3 == 0 && (2 * b - a) >= 0) {
cout << "YES" << endl;
continue;
} else
cout << "NO" << endl;
}
}
| 1,300 | CPP |
from sys import stdin
for __ in range(int(stdin.readline())):
n, x = list(map(int, stdin.readline().split()))
a = list(map(int, stdin.readline().split()))
a.sort(reverse=True)
pre = 0
ans = 0
for i in range(n):
pre += a[i]
if pre < x*(i+1):
break;
else:
ans += 1
print(ans) | 1,100 | PYTHON3 |
n=int(input())
h=int(n/100)
n-=h*100
t=int(n/20)
n-=t*20
ten=int(n/10)
n-=ten*10
f=int(n/5)
n-=f*5
print(h+t+ten+f+n)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, an[2][5000], p0[5000], p1[5000], out[5000];
inline int q(int i, int j) {
printf("? %d %d\n", i, j);
fflush(stdout);
int an;
scanf("%d", &an);
return an;
}
int main() {
scanf("%d", &n);
if (n == 1) {
printf("!\n1\n0\n");
return 0;
}
for (int j = 0; j < (int)(n); ++j) an[0][j] = q(0, j);
for (int i = 0; i < (int)(n); ++i) an[1][i] = q(i, 0);
int c = 0;
for (int i = 0; i < (int)(n); ++i) {
bool ok = true;
for (int j = 0; j < (int)(n); ++j)
if ((p1[j] = an[0][j] ^ i) >= n) {
ok = false;
break;
}
if (!ok) continue;
for (int j = 0; j < (int)(n); ++j) p0[p1[j]] = j;
if (p0[0] != i) ok = false;
for (int j = 0; j < (int)(n); ++j)
if ((p1[0] ^ p0[j]) != an[1][j]) {
ok = false;
break;
}
if (ok) {
++c;
for (int j = 0; j < (int)(n); ++j) out[j] = p0[j];
}
}
printf("!\n%d\n%d", c, out[0]);
for (int i = 1; i < (int)(n); ++i) printf(" %d", out[i]);
printf("\n");
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 5;
int n, k, suma = 0;
int a[N];
int main() {
suma = 0;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i <= n; i++) suma += a[i];
if (2 * suma >= n * (2 * k - 1)) return puts("0"), 0;
printf("%d\n", max(n * (2 * k - 1) - 2 * suma, 0));
return 0;
}
| 900 | CPP |
t=int(input())
while t>0:
t=t-1
a,b=map(int,input().split())
if a==b:
print("0 0")
elif a<b:
temp=int(b-a)
ans=min(a%temp,temp-a%temp)
print(temp,ans)
else:
temp=int(a-b)
ans=min(b%temp,temp-b%temp)
print(temp,ans)
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, a, b;
cin >> t;
while (t--) {
cin >> a >> b;
if (a == b) {
cout << 0 << '\n';
} else {
if (a < b) {
if ((b - a) % 2 == 0) {
cout << 2 << '\n';
} else {
cout << 1 << '\n';
}
} else {
if ((a - b) % 2 == 1) {
cout << 2 << '\n';
} else {
cout << 1 << '\n';
}
}
}
}
}
| 800 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200007;
const int apsz = 26;
inline int lowbit(int x) { return x & (-x); }
struct BIT {
public:
vector<long long> a;
int n;
BIT(int nn = maxn) {
n = nn;
a = vector<long long>(n + 1, 0);
}
long long Query(int L, int R) {
long long ansl, ansr;
ansl = ansr = 0;
if (L > R) return 0;
for (int i = L - 1; i; i -= lowbit(i)) ansl += a[i];
for (int i = R; i; i -= lowbit(i)) ansr += a[i];
return ansr - ansl;
}
void Add(int pos, int v) {
for (int i = pos; i <= n; i += lowbit(i)) a[i] += v;
}
};
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> pos(n + 1);
BIT tr1(n);
BIT tr2(n);
for (int i = (1); i <= (n); ++i) {
int x;
cin >> x;
pos[x] = i;
}
long long inv = 0;
for (int i = (1); i <= (n); ++i) {
long long ans = 0;
inv += i - 1 - tr1.Query(1, pos[i]);
tr1.Add(pos[i], 1), tr2.Add(pos[i], pos[i]);
if (i == 1) {
cout << 0;
continue;
}
long long L, R;
L = 1, R = n;
while (L < R) {
long long mid = L + (R - L + 1) / 2;
if (tr1.Query(1, mid) * 2 > i)
R = mid - 1;
else
L = mid;
}
long long MID = L;
long long cntL = tr1.Query(1, MID);
long long cntR = i - cntL;
long long sumL = tr2.Query(1, MID);
long long sumR = tr2.Query(MID + 1, n);
ans = MID * cntL - (0 + cntL - 1) * cntL / 2 - sumL;
ans += sumR - (MID + 1) * cntR - (0 + cntR - 1) * cntR / 2;
cout << " " << ans + inv;
}
cout << endl;
return 0;
}
| 2,300 | CPP |
input()
a=input()
print(("EASY","HARD")["1"in a]) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
vector<vector<int>> g;
vector<int> down;
vector<int> order;
vector<int> pv;
void Dfs(int v) {
order.push_back(v);
down[v] = 0;
for (int u : g[v]) {
if (u == pv[v]) {
continue;
}
pv[u] = v;
Dfs(u);
down[v] = max(down[v], down[u] + 1);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
g.resize(n);
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
--x;
--y;
g[x].push_back(y);
g[y].push_back(x);
}
down.resize(n);
order.clear();
pv.resize(n);
pv[0] = -1;
Dfs(0);
vector<int> ans(n + 1, 1);
vector<int> up(n, 0);
vector<int> children;
vector<int> cc;
vector<vector<int>> vcc(n);
for (int it = 0; it < (int)order.size(); it++) {
children.clear();
cc.clear();
int v = order[it];
for (int u : g[v]) {
if (u == pv[v]) {
continue;
}
children.push_back(u);
cc.push_back(down[u] + 1);
}
cc.push_back(up[v]);
if (up[v] != 0) {
cc.push_back(0);
}
sort(cc.rbegin(), cc.rend());
int sz = (int)cc.size();
for (int i = 1; i < sz - 1; i++) {
ans[2 * cc[i] - 1] = max(ans[2 * cc[i] - 1], i + 1);
}
for (int i = 1; i < sz; i++) {
if (cc[i] < cc[i - 1]) {
ans[2 * cc[i] + 1] = max(ans[2 * cc[i] + 1], i + 1);
}
}
for (int i = 1; i < sz - 1; i++) {
ans[2 * cc[i]] = max(ans[2 * cc[i]], i + 1);
}
if (pv[v] != -1) {
int id_up = 0;
while (cc[id_up] != up[v]) {
++id_up;
}
int first = -1;
int second = -1;
for (int i = 0; i < sz; i++) {
if (i == id_up) {
continue;
}
if (first == -1) {
first = cc[i];
} else {
second = cc[i];
break;
}
}
int ptr = 0;
for (int h = second; h >= 1; h--) {
while (ptr < sz && cc[ptr] >= h) {
++ptr;
}
int realz = ptr - (id_up < ptr);
auto iter = lower_bound(vcc[pv[v]].begin(), vcc[pv[v]].end(), h);
realz += (int)(vcc[pv[v]].end() - iter) - 1;
ans[2 * h] = max(ans[2 * h], realz);
}
}
vcc[v] = cc;
reverse(vcc[v].begin(), vcc[v].end());
for (int u : children) {
if (cc[0] == down[u] + 1) {
up[u] = cc[1] + 1;
} else {
up[u] = cc[0] + 1;
}
}
}
for (int i = n; i >= 3; i--) {
ans[i - 2] = max(ans[i - 2], ans[i]);
}
for (int i = 1; i <= n; i++) {
if (i > 1) {
cout << " ";
}
cout << ans[i];
}
cout << '\n';
return 0;
}
| 2,900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
long n;
cin >> n;
int m[100002];
long i;
for (i = 0; i < n; i++) cin >> m[i];
long x;
x = m[0];
for (i = 1; i < n; i++) {
if (x > m[i]) x = m[i];
}
int j;
int l = n;
i = 0;
while (m[i] != x) i++;
for (j = i + 1; j < n; j++) {
if (m[j] == x) {
if (l > j - i) l = j - i;
i = j;
}
}
cout << l;
return 0;
}
| 1,100 | CPP |
def poly():
tab = []
n = 0
boo = 0
for i in range(int(input())):
tab = []
n = int(input())
boo = 0
tab.append(input().split(" "))
if int(tab[0][0]) < int(tab[0][1]):
boo = 1
for j in range(n-1):
tab.append(input().split(" "))
if int(tab[1][0]) - int(tab[0][0]) < int(tab[1][1]) - int(tab[0][1]):
boo = 1
if int(tab[1][0]) < int(tab[0][0]):
boo = 1
if int(tab[1][1]) < int(tab[0][1]):
boo = 1
tab.pop(0)
if boo == 0:
print("YES")
else:
print("NO")
poly()
| 1,200 | PYTHON3 |
import sys
from functools import reduce
from collections import Counter
import time
import datetime
from math import sqrt,gcd
# def time_t():
# print("Current date and time: " , datetime.datetime.now())
# print("Current year: ", datetime.date.today().strftime("%Y"))
# print("Month of year: ", datetime.date.today().strftime("%B"))
# print("Week number of the year: ", datetime.date.today().strftime("%W"))
# print("Weekday of the week: ", datetime.date.today().strftime("%w"))
# print("Day of year: ", datetime.date.today().strftime("%j"))
# print("Day of the month : ", datetime.date.today().strftime("%d"))
# print("Day of week: ", datetime.date.today().strftime("%A"))
def ip(): return int(sys.stdin.readline())
def sip(): return sys.stdin.readline()
def mip(): return map(int,sys.stdin.readline().split())
def mips(): return map(str,sys.stdin.readline().split())
def lip(): return list(map(int,sys.stdin.readline().split()))
def matip(n,m):
lst=[]
for i in range(n):
arr = lip()
lst.append(arr)
return lst
def factors(n): # find the factors of a number
return list(set(reduce(list.__add__, ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
def minJumps(arr, n): #to reach from 0 to n-1 in the array in minimum steps
jumps = [0 for i in range(n)]
if (n == 0) or (arr[0] == 0):
return float('inf')
jumps[0] = 0
for i in range(1, n):
jumps[i] = float('inf')
for j in range(i):
if (i <= j + arr[j]) and (jumps[j] != float('inf')):
jumps[i] = min(jumps[i], jumps[j] + 1)
break
return jumps[n-1]
def dic(arr): # converting list into dict of count
return Counter(arr)
def check_prime(n):
if n<2:
return False
for i in range(2,int(n**(0.5))+1,2):
if n%i==0:
return False
return True
# --------------------------------------------------------- #
# sys.stdin = open('input.txt','r')
# sys.stdout = open('output.txt','w')
# --------------------------------------------------------- #
n = ip()
while n:
n-=1
m,k = mip()
if m==k:
print(0)
elif k>m:
print(k-m)
else:
if m%2==0 and k==0:
print(0)
elif m%2!=0 and k==0:
print(1)
elif m%2==0 and k%2==0:
print(0)
elif m%2!=0 and k%2!=0:
print(0)
else:
print(1) | 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int A[300500], pos[300500];
int pre[300500], a, b, t;
int dp[300500];
long long Ans;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &A[i]);
pos[A[i]] = i;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d", &a, &b);
if (pos[a] < pos[b]) t = a, a = b, b = t;
if (pos[b] > pre[pos[a]]) pre[pos[a]] = pos[b];
}
dp[1] = 1;
for (int i = 2; i <= n; i++) dp[i] = min(dp[i - 1] + 1, i - pre[i]);
for (int i = 1; i <= n; i++) {
Ans += dp[i];
}
printf("%I64d\n", Ans);
return 0;
}
| 1,800 | CPP |
from math import sqrt
n = int(input())
for i in range(2, int(sqrt(n))+1):
if(n%i == 0):
print(i,end='')
print(n//i) | 0 | PYTHON3 |
n, t = input().split()
l=list(map(int,input().split()))
x=86400
c=0
for i in range(int(n)):
c+=x-l[i]
if c>=int(t):
print(i+1)
break
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
bool compare(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
if (a.second == b.second) return a.first < b.first;
return a.second < b.second;
}
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n, t, a, b;
cin >> n >> t >> a >> b;
pair<long long int, long long int> arr[n];
vector<long long int> targs;
for (int i = 0; i < n; i++) cin >> arr[i].first;
for (int i = 0; i < n; i++) cin >> arr[i].second;
sort(arr, arr + n, compare);
for (int i = 0; i < n; i++) targs.push_back(arr[i].second - 1);
targs.push_back(t);
pair<long long int, long long int> preComp[n];
for (int i = 0; i < n; i++) preComp[i].first = preComp[i].second = 0;
if (arr[0].first == 1)
preComp[0].first++;
else
preComp[0].second++;
for (int i = 1; i < n; i++) {
if (arr[i].first == 1) {
preComp[i].first = preComp[i - 1].first + 1;
preComp[i].second = preComp[i - 1].second;
}
if (arr[i].first == 0) {
preComp[i].second = preComp[i - 1].second + 1;
preComp[i].first = preComp[i - 1].first;
}
}
long long int targ = 0, gm = 0, last = -1;
for (auto targ : targs) {
long long int lo, hi;
lo = 0, hi = n - 1;
while (lo <= hi) {
long long int mid = (lo + hi) / 2;
if (arr[mid].second > targ)
hi = mid - 1;
else {
lo = mid + 1;
last = mid;
}
}
long long int aC, bC, val;
if (last == -1)
aC = bC = 0;
else {
aC = preComp[last].first;
bC = preComp[last].second;
}
val = (aC * b) + (bC * a);
if (val <= targ) {
long long int count = last + 1;
long long int left = targ - val;
long long int easyLeft, hardLeft;
easyLeft = hardLeft = 0;
if (last == -1) {
easyLeft = preComp[n - 1].second;
hardLeft = preComp[n - 1].first;
} else {
easyLeft = preComp[n - 1].second - preComp[last].second;
hardLeft = preComp[n - 1].first - preComp[last].first;
}
if (left >= a) {
long long int temp = left / a;
if (temp > easyLeft) temp = easyLeft;
count += temp;
left -= a * temp;
}
if (left >= b) {
long long int temp = left / b;
if (temp > hardLeft) temp = hardLeft;
count += temp;
left -= b * temp;
}
gm = max(gm, count);
}
targ++;
}
cout << gm << endl;
}
}
| 1,800 | CPP |
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<sstream>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define rep(i,x,n) for(int i=x;i<=n;i++)
#define per(i,n,x) for(int i=n;i>=x;i--)
#define sz(a) int(a.size())
#define rson mid+1,r,p<<1|1
#define pii pair<int,int>
#define lson l,mid,p<<1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps=1e-8;
const int mod=1e9+7;
const int N=1e5+10;
const int inf=1e9;
int t;
ll k;
int main(){
//ios::sync_with_stdio(false);
//freopen("in","r",stdin);
cin>>t;
while(t--){
cin>>k;
if(k&1) puts("-1");
else{
vector<int>v;
for(int i=61;i>=2;i--){
ll ret=(1ll<<i)-2;
while(k>=ret){
k-=ret;
v.pb(1);
for(int j=1;j<i-1;j++) v.pb(0);
}
}
cout<<sz(v)<<endl;
for(int x:v) cout<<x<<' ';
cout<<endl;
}
}
return 0;
} | 1,900 | CPP |
input()
ma = -1
mi = 10001
k = 0
for x in map(int,input().split()):
if x > ma:
ma = x
k += 1
if x < mi:
mi = x
k += 1
print(k-2) | 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
queue<int> q;
void bfs(int m);
int m, n, a, b, s, h, num1, num2;
int main() {
while (cin >> m >> a >> b) {
n = 0;
s = (2 * a + b) * 2;
num1 = 4;
num2 = 2;
bfs(m);
cout << n << endl;
}
return 0;
}
void bfs(int m) {
while (s > 0) {
q.push(m);
while (!q.empty()) {
h = q.front();
q.pop();
if (h >= a && num1 != 0) {
h = h - a;
num1--;
}
if (h >= b && num2 != 0) {
h = h - b;
num2--;
}
if ((h >= a && num1 != 0) || (h >= b && num2 != 0)) {
q.push(h);
}
}
s = a * num1 + b * num2;
n++;
}
}
| 1,600 | CPP |
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-9;
const int mod = 1e9 + 7;
const long long INF = 1e12 + 1;
const int N = 1e6 + 9;
int n, m;
int a[N];
long long ans[N], san;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= n; ++i) {
san += a[i];
if (san >= m)
ans[i] = san / m, san -= (san / m) * m;
else
ans[i] = 0;
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " ";
}
exit(0);
}
| 900 | CPP |
n, m = map(int, input().split())
val = [0] * 5
for i in range(5):
val[i] = (m // 5) + (1 if m % 5 >= 4 - i and i != 4 else 0)
print((sum(val) * (n // 5)) + sum(val[:n % 5]))
| 1,100 | PYTHON3 |
import os
import sys
from io import BytesIO, IOBase
import math
from queue import Queue
import itertools
import bisect
import heapq
#sys.setrecursionlimit(100000)
#^^^TAKE CARE FOR MEMORY LIMIT^^^
def main():
pass
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")
def binary(n):
return (bin(n).replace("0b", ""))
def decimal(s):
return (int(s, 2))
def pow2(n):
p = 0
while (n > 1):
n //= 2
p += 1
return (p)
def primeFactors(n):
l = []
while n % 2 == 0:
l.append(2)
n = n / 2
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
l.append(i)
n = n / i
if n > 2:
l.append(int(n))
return (l)
def isPrime(n):
if (n == 1):
return (False)
else:
root = int(n ** 0.5)
root += 1
for i in range(2, root):
if (n % i == 0):
return (False)
return (True)
def maxPrimeFactors(n):
maxPrime = -1
while n % 2 == 0:
maxPrime = 2
n >>= 1
for i in range(3, int(math.sqrt(n)) + 1, 2):
while n % i == 0:
maxPrime = i
n = n / i
if n > 2:
maxPrime = n
return int(maxPrime)
def countcon(s, i):
c = 0
ch = s[i]
for i in range(i, len(s)):
if (s[i] == ch):
c += 1
else:
break
return (c)
def lis(arr):
n = len(arr)
lis = [1] * n
for i in range(1, n):
for j in range(0, i):
if arr[i] > arr[j] and lis[i] < lis[j] + 1:
lis[i] = lis[j] + 1
maximum = 0
for i in range(n):
maximum = max(maximum, lis[i])
return maximum
def isSubSequence(str1, str2):
m = len(str1)
n = len(str2)
j = 0
i = 0
while j < m and i < n:
if str1[j] == str2[i]:
j = j + 1
i = i + 1
return j == m
def maxfac(n):
root = int(n ** 0.5)
for i in range(2, root + 1):
if (n % i == 0):
return (n // i)
return (n)
def p2(n):
c=0
while(n%2==0):
n//=2
c+=1
return c
def seive(n):
primes=[True]*(n+1)
primes[1]=primes[0]=False
for i in range(2,n+1):
if(primes[i]):
for j in range(i+i,n+1,i):
primes[j]=False
p=[]
for i in range(0,n+1):
if(primes[i]):
p.append(i)
return(p)
def ncr(n, r, p):
num = den = 1
for i in range(r):
num = (num * (n - i)) % p
den = (den * (i + 1)) % p
return (num * pow(den,
p - 2, p)) % p
def denofactinverse(n,m):
fac=1
for i in range(1,n+1):
fac=(fac*i)%m
return (pow(fac,m-2,m))
def numofact(n,m):
fac=1
for i in range(1,n+1):
fac=(fac*i)%m
return(fac)
def sod(n):
s=0
while(n>0):
s+=n%10
n//=10
return s
n,m=map(int,input().split())
if(n==1 and m==1):
print(0)
else:
if(n==1):
for i in range(0,m):
print(i+2,end=" ")
elif(m==1):
for i in range(0,n):
print(i+2)
else:
ans=[[] for i in range(0,n)]
for i in range(0,m):
ans[0].append(i+2)
mul=ans[0][-1]
for r in range(1,n):
tbm=mul+r
for c in range(0,m):
ans[r].append(ans[0][c]*tbm)
for i in ans:
print(*i) | 1,400 | PYTHON3 |
from collections import Counter
for t in range(int(input())):
st=input()
tot=len(st)
m=int(input())
arr=list(map(int,input().split()))
sor=sorted(st)
count=Counter(st)
final=['']*m
while arr.count(-1)!=m:
c=arr.count(0)
g=[]
for i in range(m):
if arr[i]==0:
g.append(i)
x=sor[-1]
if count[x]>=c:
for i in range(count[x]):
del sor[-1]
for i in range(m):
if arr[i]==0:
arr[i]=-1
final[i]=x
if arr[i]!=-1:
for j in g:
arr[i]-=abs(i-j)
else:
for i in range(count[x]):
del sor[-1]
print(''.join(final))
| 1,800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
const long long inf = 0x3f3f3f3f3f3f3f3f;
typedef long long ll;
#define pb push_back
int n, m;
int a[N];
int g[N], v[N<<1], nxt[N<<1], tot, fa[N];
vector<int> c[N];
ll f[N], ans;
inline void add(int x, int y) {
v[++tot] = y, nxt[tot] = g[x], g[x] = tot;
}
void dfs(int x, int ff, int dep) {
fa[x] = ff, c[dep].pb(x), m = max(m, dep);
for (int i = g[x]; i; i = nxt[i]) {
int y = v[i];
if (y == ff) continue;
dfs(y, x, dep + 1);
}
}
inline void solve() {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
int x;
scanf("%d", &x);
add(i, x), add(x, i);
}
for (int i = 2; i <= n; i++) scanf("%d", &a[i]);
dfs(1, 0, 0);
for (int r = 1; r <= m; r++) {
ll max0 = -inf, min0 = inf, max1 = -inf, max2 = -inf;
for (int i = 0; i < (int)c[r].size(); i++) {
int x = c[r][i];
max0 = max(max0, (ll)a[x]), min0 = min(min0, (ll)a[x]);
max1 = max(max1, f[fa[x]] + a[x]);
max2 = max(max2, f[fa[x]] - a[x]);
}
for (int i = 0; i < (int)c[r].size(); i++) {
int x = c[r][i];
f[x] = max(f[fa[x]] + max(max0 - a[x], a[x] - min0), max(max1 - a[x], max2 + a[x]));
if (r == m) ans = max(ans, f[x]);
}
}
printf("%lld\n", ans);
}
inline void init() {
ans = m = tot = 0;
for (int i = 1; i <= n; i++) c[i].clear(), g[i] = 0, f[i] = 0;
}
int main()
{
int T;
scanf("%d", &T);
while (T--) {
init();
solve();
}
return 0;
} | 2,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long int n, m, k, p, sum = 0, x;
cin >> n >> m >> k >> p;
long long int a[n][m];
priority_queue<long long int> pqr, pqs;
for (long long int i = 0; i < n; i++) {
sum = 0;
for (long long int j = 0; j < m; j++) {
cin >> a[i][j];
sum += a[i][j];
}
pqr.push(sum);
}
for (long long int j = 0; j < m; j++) {
sum = 0;
for (long long int i = 0; i < n; i++) {
sum += a[i][j];
}
pqs.push(sum);
}
vector<long long int> v1, v2;
v1.push_back(0);
v2.push_back(0);
for (long long int i = 0; i < k; i++) {
x = pqr.top();
pqr.pop();
v1.push_back(x);
x -= m * p;
pqr.push(x);
}
for (long long int i = 0; i < k; i++) {
x = pqs.top();
pqs.pop();
v2.push_back(x);
x -= n * p;
pqs.push(x);
}
for (long long int i = 1; i <= k; i++) {
v1[i] += v1[i - 1];
v2[i] += v2[i - 1];
}
long long int ans = -1e+18;
for (long long int i = 0; i <= k; i++) {
ans = max(ans, v1[i] + v2[k - i] - (k - i) * i * p);
}
cout << ans << endl;
}
| 2,000 | CPP |
N = int(input())
A = []
for i in range(N):
k = int(input())
s = ''
for j in range(k):
s = s + str(j+1) + ' '
s = s[:-1]
print(s) | 800 | PYTHON3 |
t = int(input())
while(t>0):
a,b,c = [int(i) for i in input().split()]
sum = (abs(a-b)+abs(b-c)+abs(a-c))
if sum>4:
sum = sum-4
else:
sum = 0
print(sum)
t-=1 | 900 | PYTHON3 |
n=int(input().strip())
s=0;
l=[]
for i in range(n):
a=tuple(map(int,input().strip().split()))
l.append(a)
k=int(input().strip())
for i in range(n):
if(k<=l[i][1]and k>=l[i][0]):
print(n-i)
break | 800 | PYTHON3 |
x,y,z=map(int,input().split())
if x>y+z:
ans='+'
elif y>x+z:
ans='-'
elif abs(x-y)+z==0:
ans='0'
else:
ans='?'
print(ans)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
map<int, int> front, back;
int main() {
int n, f, b, shortag = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> f >> b;
front[f]++;
if (f != b) {
back[b]++;
}
}
int ans = 1000000009, maxf = 0;
int mid = (n + 1) / 2;
for (auto i : front) {
if (i.second + back[i.first] >= mid) {
shortag = mid - i.second;
maxf = max(0, shortag);
ans = min(ans, maxf);
}
}
for (auto i : back) {
if (i.second + front[i.first] >= mid) {
shortag = mid - front[i.first];
maxf = max(0, shortag);
ans = min(ans, maxf);
}
}
if (ans > n) {
cout << -1;
} else {
cout << ans;
}
return 0;
}
| 1,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e18;
const int N = 1e5 + 5;
bool cmp(pair<int, int> a, pair<int, int> b) {
return (a.first - b.first) < (a.second - b.second);
}
void solve() {
int n;
cin >> n;
vector<int> v(n);
int i = 0;
int l = 1, r = n;
while (l < r) {
v[i] = l, v[i + 1] = r;
l++, r--;
i += 2;
}
if (n & 1) v[n - 1] = l;
for (auto i : v) cout << i << " ";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << showpoint;
cout << setprecision(9);
int test = 1;
while (test--) {
solve();
}
return 0;
}
| 1,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int to, next;
} edge[1000100];
int n, m, flag, vis[1000100];
int tot, head[1000100], ans;
void init() {
tot = 0;
memset(head, -1, sizeof(head));
}
void add_edge(int u, int v) {
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u, int val) {
vis[u] = val;
if (vis[u]) ans++;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (vis[v] != -1) {
if (vis[v] == vis[u]) flag = 0;
continue;
}
dfs(v, vis[u] ^ 1);
}
}
int main() {
int t;
while (scanf("%d%d", &n, &m) != EOF) {
init();
memset(vis, -1, sizeof(vis));
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
flag = 1;
ans = 0;
for (int i = 1; i <= n; i++)
if (vis[i] == -1) dfs(i, 1);
if (!flag)
printf("-1\n");
else {
printf("%d\n", ans);
for (int i = 1; i <= n; i++)
if (vis[i] == 1) printf("%d ", i);
printf("\n");
printf("%d\n", n - ans);
for (int i = 1; i <= n; i++)
if (!vis[i]) printf("%d ", i);
printf("\n");
}
}
return 0;
}
| 1,500 | CPP |
from collections import Counter, defaultdict
BS="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def to_base(s, b):
res = ""
while s:
res+=BS[s%b]
s//= b
return res[::-1] or "0"
alpha = "abcdefghijklmnopqrstuvwxyz"
from math import floor, ceil,pi
import sys
primes = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373,1381,1399,1409,1423,1427,1429,1433,1439,1447,1451,1453,1459,1471,1481,1483,1487,1489,1493,1499,1511,1523,1531,1543,1549,1553,1559,1567,1571,1579,1583,1597,1601,1607,1609,1613,1619,1621,1627,1637,1657,1663,1667,1669,1693,1697,1699,1709,1721,1723,1733,1741,1747,1753,1759,1777,1783,1787,1789,1801,1811,1823,1831,1847,1861,1867,1871,1873,1877,1879,1889,1901,1907,1913,1931,1933,1949,1951,1973,1979,1987,1993,1997,1999,2003,2011,2017,2027,2029,2039,2053,2063,2069,2081,2083,2087,2089,2099,2111,2113,2129,2131,2137,2141,2143,2153,2161,2179,2203,2207,2213,2221,2237,2239,2243,2251,2267,2269,2273,2281,2287,2293,2297,2309,2311,2333,2339,2341,2347,2351,2357,2371,2377,2381,2383,2389,2393,2399,2411,2417,2423,2437,2441,2447,2459,2467,2473,2477,2503,2521,2531,2539,2543,2549,2551,2557,2579,2591,2593,2609,2617,2621,2633,2647,2657,2659,2663,2671,2677,2683,2687,2689,2693,2699,2707,2711,2713,2719,2729,2731,2741,2749,2753,2767,2777,2789,2791,2797,2801,2803,2819,2833,2837,2843,2851,2857,2861,2879,2887,2897,2903,2909,2917,2927,2939,2953,2957,2963,2969,2971,2999,3001,3011,3019,3023,3037,3041,3049,3061,3067,3079,3083,3089,3109,3119,3121,3137,3163,3167,3169,3181,3187,3191,3203,3209,3217,3221,3229,3251,3253,3257,3259,3271,3299,3301,3307,3313,3319,3323,3329,3331,3343,3347,3359,3361,3371,3373,3389,3391,3407,3413,3433,3449,3457,3461,3463,3467,3469,3491,3499,3511,3517,3527,3529,3533,3539,3541,3547,3557,3559,3571,3581,3583,3593,3607,3613,3617,3623,3631,3637,3643,3659,3671,3673,3677,3691,3697,3701,3709,3719,3727,3733,3739,3761,3767,3769,3779,3793,3797,3803,3821,3823,3833,3847,3851,3853,3863,3877,3881,3889,3907,3911,3917,3919,3923,3929,3931,3943,3947,3967,3989,4001,4003,4007,4013,4019,4021,4027,4049,4051,4057,4073,4079,4091,4093,4099,4111,4127,4129,4133,4139,4153,4157,4159,4177,4201,4211,4217,4219,4229,4231,4241,4243,4253,4259,4261,4271,4273,4283,4289,4297,4327,4337,4339,4349,4357,4363,4373,4391,4397,4409,4421,4423,4441,4447,4451,4457,4463,4481,4483,4493,4507,4513,4517,4519,4523,4547,4549,4561,4567,4583,4591,4597,4603,4621,4637,4639,4643,4649,4651,4657,4663,4673,4679,4691,4703,4721,4723,4729,4733,4751,4759,4783,4787,4789,4793,4799,4801,4813,4817,4831,4861,4871,4877,4889,4903,4909,4919,4931,4933,4937,4943,4951,4957,4967,4969,4973,4987,4993,4999,5003,5009,5011,5021,5023,5039,5051,5059,5077,5081,5087,5099,5101,5107,5113,5119,5147,5153,5167,5171,5179,5189,5197,5209,5227,5231,5233,5237,5261,5273,5279,5281,5297,5303,5309,5323,5333,5347,5351,5381,5387,5393,5399,5407,5413,5417,5419,5431,5437,5441,5443,5449,5471,5477,5479,5483,5501,5503,5507,5519,5521,5527,5531,5557,5563,5569,5573,5581,5591,5623,5639,5641,5647,5651,5653,5657,5659,5669,5683,5689,5693,5701,5711,5717,5737,5741,5743,5749,5779,5783,5791,5801,5807,5813,5821,5827,5839,5843,5849,5851,5857,5861,5867,5869,5879,5881,5897,5903,5923,5927,5939,5953,5981,5987,6007,6011,6029,6037,6043,6047,6053,6067,6073,6079,6089,6091,6101,6113,6121,6131,6133,6143,6151,6163,6173,6197,6199,6203,6211,6217,6221,6229,6247,6257,6263,6269,6271,6277,6287,6299,6301,6311,6317,6323,6329,6337,6343,6353,6359,6361,6367,6373,6379,6389,6397,6421,6427,6449,6451,6469,6473,6481,6491,6521,6529,6547,6551,6553,6563,6569,6571,6577,6581,6599,6607,6619,6637,6653,6659,6661,6673,6679,6689,6691,6701,6703,6709,6719,6733,6737,6761,6763,6779,6781,6791,6793,6803,6823,6827,6829,6833,6841,6857,6863,6869,6871,6883,6899,6907,6911,6917,6947,6949,6959,6961,6967,6971,6977,6983,6991,6997,7001,7013,7019,7027,7039,7043,7057,7069,7079,7103,7109,7121,7127,7129,7151,7159,7177,7187,7193,7207,7211,7213,7219,7229,7237,7243,7247,7253,7283,7297,7307,7309,7321,7331,7333,7349,7351,7369,7393,7411,7417,7433,7451,7457,7459,7477,7481,7487,7489,7499,7507,7517,7523,7529,7537,7541,7547,7549,7559,7561,7573,7577,7583,7589,7591,7603,7607,7621,7639,7643,7649,7669,7673,7681,7687,7691,7699,7703,7717,7723,7727,7741,7753,7757,7759,7789,7793,7817,7823,7829,7841,7853,7867,7873,7877,7879,7883,7901,7907,7919
]
import timeit
def primef(n, plst = []):
if n==1:
return plst
else:
for m in primes:
if n%m==0:
return primef(n//m, plst+[m])
return primef(1, plst+[n])
def lmii():
return list(map(int, input().split()))
def ii():
return int(input())
def countOverlapping(string,sub):
count = start = 0
while True:
start = string.find(sub, start)+1
if start > 0:
count += 1
else:
return count
n,k = lmii()
gotA = []
gotB = []
got = []
nums = [list(map(int, sys.stdin.readline().split())) for i in range(n)]
nums.sort()
for i in range(n):
a,b,c = nums[i]
#print(a,b,c, gotA, gotB)
if b+c==2:
got.append(a)
elif b+c !=0:
if b==0:
if gotB:
aa,bb,cc = gotB.pop(0)
got.append(aa+a)
else:
gotA.append((a,b,c))
else:
if gotA:
aa,bb,cc = gotA.pop(0)
got.append(aa+a)
else:
gotB.append((a,b,c))
got.sort()
if len(got) < k:
print(-1)
else:
print(sum(got[:k]))
| 1,600 | PYTHON3 |
n = int(input())
for i in range(n):
word = input()
lenght = len(word)
if lenght > 10:
word1 = list(word)
lenght1 = lenght - 2
lenght2 = lenght - 1
print(str(word1[0]) + str(lenght1) + str(word1[lenght2]))
else:
print(word) | 800 | PYTHON3 |
## ! лучшее решение
import sys
from heapq import heappop, heappush
input = sys.stdin.readline
def dijkstra(graph, start=0):
n = len(graph)
dist = [float('inf')] * n
parents = [-1] * n
dist[start] = 0.0
queue = [(0.0, start)]
while queue:
path_len, v = heappop(queue)
if path_len == dist[v]:
for (w, edge_len) in graph[v]:
if edge_len + path_len < dist[w]:
dist[w] = edge_len + path_len
parents[w] = v
heappush(queue, (edge_len + path_len, w))
return dist, parents
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(m):
a, b, w = map(int, input().split())
graph[a-1].append((b-1, w))
graph[b-1].append((a-1, w))
dist, parents = dijkstra(graph)
if parents[n - 1] == -1:
print(-1)
else:
res, parent = [], n - 1
while parent != parents[0]:
res.append(parent + 1)
parent = parents[parent]
res.reverse()
print(*res)
| 1,900 | PYTHON3 |
import sys
lines = []
for line in sys.stdin:
if 'exit' == line.rstrip():
break
lines.append(line)
t = int(lines[0])
def lcm(x,y):
max = x * y
a = [0]*(max+1)
for i in range(0,max+1,x):
a[i] = 1
for i in range(0,max+1,y):
a[i] += 1
if a[i] == 2 and i != 0:
return i
for case in range(1,t+1):
l, r = [int(x) for x in lines[case].split(' ')][0], [int(x) for x in lines[case].split(' ')][1]
if 2 * l <= r:
print(str(l)+" "+str(2*l))
else:
print("-1 -1")
'''
found = False
for i in range(l,r+1):
if found:
break
for j in range(i+1,r+1):
if i * j > r:
continue
if l <= lcm(i,j) <= r:
print(str(i)+" "+str(j))
found = True
break
if not found:
print("-1 -1")
'''
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10, modd = 1e9 + 7, inf = 0x3f3f3f3f, INF = 0x7fffffff,
hb = 131, hmod1 = 0x48E2DCE7, hmod2 = 0x60000005;
const int dir[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
const double eps = 1e-8;
template <class T>
inline void sc(T &x) {
char c;
x = 0;
while ((c = getchar()) < '0')
;
while (c >= '0' && c <= '9') x = x * 10 + (c - 48), c = getchar();
}
inline long long min(long long a, long long b) { return a < b ? a : b; }
inline long long max(long long a, long long b) { return a > b ? a : b; }
inline long long gcd(long long a, long long b) {
return b == 0 ? a : gcd(b, a % b);
}
inline long long exgcd(long long a, long long b, long long &x, long long &y) {
long long d;
(b == 0 ? (x = 1, y = 0, d = a)
: (d = exgcd(b, a % b, y, x), y -= a / b * x));
return d;
}
inline long long qpow(long long a, long long n) {
long long sum = 1;
while (n) {
if (n & 1) sum = sum * a % modd;
a = a * a % modd;
n >>= 1;
}
return sum;
}
inline long long qmul(long long a, long long n) {
long long sum = 0;
while (n) {
if (n & 1) sum = (sum + a) % modd;
a = (a + a) % modd;
n >>= 1;
}
return sum;
}
inline long long inv(long long a) { return qpow(a, modd - 2); }
inline long long madd(long long a, long long b) {
return (a % modd + b % modd) % modd;
}
inline long long mmul(long long a, long long b) { return a % modd * b % modd; }
inline void uadd(long long &a, long long b) { a = madd(a, b); }
inline void umul(long long &a, long long b) { a = mmul(a, b); }
int n, k, cnt, pre[maxn], rnk[maxn];
int find(int x) { return x == pre[x] ? x : pre[x] = find(pre[x]); }
void merge(int x, int y) {
x = find(x);
y = find(y);
if (pre[x] == pre[y]) return;
if (rnk[x] > rnk[y]) swap(x, y);
rnk[y] += rnk[x];
pre[x] = y;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b;
cin >> n >> k;
for (int i = 1; i <= n; i++) pre[i] = i, rnk[i] = 1;
for (int i = 0; i < k; i++) {
cin >> a >> b;
merge(a, b);
}
for (int i = 1; i <= n; i++)
if (i == pre[i]) cnt += (rnk[i] - 1);
cout << k - cnt << endl;
}
| 1,700 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, m;
char map[110][110];
int data[110][110];
int main() {
scanf("%d %d", &n, &m);
char da[110];
for (int i = 0; i < n; i++) {
scanf("%s", da);
for (int j = 0; j < m; j++) {
data[i][j] = da[j] - '0';
}
}
bool flag[110];
memset(flag, false, sizeof(flag));
for (int j = 0; j < m; j++) {
int dd = -1;
vector<int> tmp;
for (int i = 0; i < n; i++) {
if (data[i][j] > dd) {
dd = data[i][j];
tmp.clear();
tmp.push_back(i);
} else if (data[i][j] == dd) {
tmp.push_back(i);
}
}
for (int k = 0; k < tmp.size(); k++) {
flag[tmp[k]] = true;
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (flag[i]) ans++;
}
printf("%d\n", ans);
return 0;
}
| 900 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q, i, j, t, cnt = 1;
cin >> t;
while (t--) {
cnt = 0;
cin >> n;
int a[n + 1];
bool flag = 1;
for (i = 0; i < n; i++) cin >> a[i];
sort(a, a + n);
for (i = 1; i < n; i++) {
if (a[i] - a[i - 1] == 1) {
flag = 0;
break;
}
}
if (flag)
cout << 1 << endl;
else
cout << 2 << endl;
}
}
| 800 | CPP |
k=int(input())
s=sorted(input())
s1=s[::k]*k
if sorted(s1)==s:
print(''.join(s1))
else:
print(-1) | 1,000 | PYTHON3 |
n = int(input())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
from itertools import product
st = set((*a, *b))
npairs = sum((x ^ y) in st for x, y in product(a, b))
print('Koyomi' if npairs & 1 else 'Karen') | 1,100 | PYTHON3 |
#_________________ Mukul Mohan Varshney _______________#
#Template
import sys
import os
import math
import copy
from math import gcd
from bisect import bisect
from io import BytesIO, IOBase
from math import sqrt,floor,factorial,gcd,log,ceil
from collections import deque,Counter,defaultdict
from itertools import permutations, combinations
#define function
def Int(): return int(sys.stdin.readline())
def Mint(): return map(int,sys.stdin.readline().split())
def Lstr(): return list(sys.stdin.readline().strip())
def Str(): return sys.stdin.readline().strip()
def Mstr(): return map(str,sys.stdin.readline().strip().split())
def List(): return list(map(int,sys.stdin.readline().split()))
def Hash(): return dict()
def Mod(): return 1000000007
def Ncr(n,r,p): return ((fact[n])*((ifact[r]*ifact[n-r])%p))%p
def Most_frequent(list): return max(set(list), key = list.count)
def Mat2x2(n): return [List() for _ in range(n)]
# Driver Code
def solution():
for i in range(Int()):
n=Int()
a=List()
ans=0
for i in range(n):
if(a[i]==1):
ans+=1
else:
break
if(ans==n):
ans+=1
if(ans%2==0):
print("First")
else:
print("Second")
#Call the solve function
if __name__ == "__main__":
solution() | 1,100 | PYTHON3 |
n = int(input())
mat = sum(list(map(int,input().split())))
sat = sum(sorted(list(map(int,input().split())))[-2::])
print("YES" if mat<=sat else "NO")
| 900 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
scanf("%d", &t);
while (t--) {
int L, v, l, r, sum = 0, z = 0;
for (int i = 0; i < 10000; i++) z++;
int s1, s2, s3;
for (int i = 0; i < 10000; i++) z++;
scanf("%d%d%d%d", &L, &v, &l, &r);
s1 = L / v;
if (l % v == 0)
s2 = l / v - 1;
else
s2 = l / v;
s3 = r / v;
for (int i = 0; i < 10000; i++) z++;
printf("%d\n", s1 - s3 + s2);
}
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long dp[25][15][15][7][4], n, t;
int main() {
cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
cin >> n >> t;
dp[2][0][0][1][0] = 3;
dp[2][0][0][1][1] = 0;
dp[2][0][0][2][0] = 2;
dp[2][0][0][2][1] = 1;
dp[2][0][0][3][0] = 1;
dp[2][0][0][3][1] = 2;
dp[2][0][0][4][0] = 0;
dp[2][0][0][4][1] = 3;
for (int i = 2; i <= n; i++) {
for (int j = 0; j <= t; j++) {
for (int k = 0; k <= t - 1; k++) {
for (int l = 1; l <= 4; l++) {
for (int o = 0; o <= 1; o++) {
if (o == 0) {
for (int it = 1; it <= 4; it++) {
if (it > l) {
dp[i + 1][j][k + 1][it][1] += dp[i][j][k][l][o];
} else if (it < l) {
dp[i + 1][j][k][it][0] += dp[i][j][k][l][o];
}
}
} else if (o == 1) {
for (int it = 1; it <= 4; it++) {
if (it > l) {
dp[i + 1][j][k][it][1] += dp[i][j][k][l][o];
} else if (it < l) {
dp[i + 1][j + 1][k][it][0] += dp[i][j][k][l][o];
}
}
}
}
}
}
}
}
long long count1 = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 0; j <= 1; j++) {
count1 += dp[n][t][t - 1][i][j];
}
}
cout << count1;
}
| 1,900 | CPP |
y = input().split()
n = int(y[0])
x = int(y[1])
count = 0; i= 1
while(i <= n):
if(x%i == 0 and i*n >= x):
count += 1
i+=1
print(count)
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long chk(long long *b, long long n) {
long long mx = -1;
for (int i = 0; i < n; i++) mx = max(mx, b[i]);
return mx;
}
void solve() {
long long n, mx = 1000000007, c;
cin >> n;
long long a[n], b[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
for (int k = i, s = j; k <= j; k++, s--) b[k] &= b[s];
c = chk(b, n);
mx = min(mx, c);
}
}
cout << mx << endl;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
int n;
cin >> n;
while (n--) {
solve();
}
return 0;
}
| 900 | CPP |
input()
X = list(map(int, input().split()))
Rate = [0 for i in range(len(X))]
count = 1
Set = set(X)
Set = sorted(Set, reverse=True)
for i in Set:
for j in range(len(X)):
if X[j] == i:
Rate[j] = count
count += X.count(i)
print(*Rate)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const double g = 10.0, eps = 1e-7;
const int N = 1000 + 10, maxn = 500 + 100, inf = 0x3f3f3f;
char p[4] = {'R', 'B', 'Y', 'G'};
string ans;
bool ok(string s) {
for (int i = 0; i + 3 < s.size(); i++) {
int rr = 0, bb = 0, yy = 0, gg = 0;
for (int j = 0; j < 4; j++) {
if (s[i + j] == 'R')
rr++;
else if (s[i + j] == 'B')
bb++;
else if (s[i + j] == 'Y')
yy++;
else
gg++;
}
if (rr != 1 || gg != 1 || yy != 1 || bb != 1) return 0;
}
return 1;
}
void dfs(int x, string t) {
if (x == t.size()) {
if (ok(t)) ans = t;
return;
}
if (t[x] == '!') {
for (int i = 0; i < 4; i++) {
t[x] = p[i];
if (x >= 3) {
int rr = 0, bb = 0, yy = 0, gg = 0;
for (int j = 0; j < 4; j++) {
if (t[x - j] == 'R')
rr++;
else if (t[x - j] == 'B')
bb++;
else if (t[x - j] == 'Y')
yy++;
else
gg++;
}
if (rr != 1 || gg != 1 || yy != 1 || bb != 1) continue;
}
dfs(x + 1, t);
}
} else
dfs(x + 1, t);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int kr = 0, kb = 0, ky = 0, kg = 0;
dfs(0, s);
for (int i = 0; i < s.size(); i++) {
if (s[i] == '!') {
if (ans[i] == 'R')
kr++;
else if (ans[i] == 'B')
kb++;
else if (ans[i] == 'Y')
ky++;
else if (ans[i] == 'G')
kg++;
}
}
cout << kr << " " << kb << " " << ky << " " << kg << endl;
return 0;
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
int ar[111111];
int main() {
int k1, k2, k3;
int fk1, fk2, fk3;
fk1 = fk2 = fk3 = 0;
int t1, t2, t3;
scanf("%d%d%d %d%d%d", &k1, &k2, &k3, &t1, &t2, &t3);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &ar[i]);
set<pair<long long, pair<int, int> > > S;
queue<int> qk1, qk2, qk3;
for (int i = 0; i < n; i++) S.insert(make_pair(ar[i], make_pair(3, i)));
long long ans = 0;
while (!S.empty()) {
long long ti = (*S.begin()).first;
int mode = (*S.begin()).second.first;
int person = (*S.begin()).second.second;
S.erase(S.begin());
if (mode == 0) {
qk2.push(person);
fk1--;
} else if (mode == 1) {
qk3.push(person);
fk2--;
} else if (mode == 2) {
ans = max(ans, ti - ar[person]);
fk3--;
} else {
qk1.push(person);
}
while (!qk1.empty() && fk1 < k1) {
person = qk1.front();
qk1.pop();
S.insert(make_pair(ti + t1, make_pair(0, person)));
fk1++;
}
while (!qk2.empty() && fk2 < k2) {
person = qk2.front();
qk2.pop();
S.insert(make_pair(ti + t2, make_pair(1, person)));
fk2++;
}
while (!qk3.empty() && fk3 < k3) {
person = qk3.front();
qk3.pop();
S.insert(make_pair(ti + t3, make_pair(2, person)));
fk3++;
}
}
cout << ans;
printf("\n");
}
| 1,800 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool ok(const int box[4]) { return box[0] <= box[2] and box[1] <= box[3]; }
int test(const int box[4]) {
assert(ok(box));
printf("? %d %d %d %d\n", box[0], box[1], box[2], box[3]);
fflush(stdout);
int ret;
scanf("%d", &ret);
return ret;
}
void search(int box[4], int i, int ok) {
int minv, maxv;
bool inc;
if (i == 0 or i == 1) {
inc = true;
minv = box[i];
maxv = box[i + 2];
} else {
inc = false;
maxv = box[i];
minv = box[i - 2];
}
int tmp[4];
memcpy(tmp, box, sizeof(tmp));
int &mid = tmp[i];
while (minv < maxv) {
mid = (minv + maxv) / 2;
if (inc and mid == minv) mid++;
if (test(tmp) >= ok) {
if (inc)
minv = mid;
else
maxv = mid;
} else {
if (inc)
maxv = mid - 1;
else
minv = mid + 1;
}
}
box[i] = minv;
}
int main() {
int n;
scanf("%d", &n);
int bb[4] = {1, 1, n, n};
search(bb, 0, 2);
search(bb, 1, 2);
search(bb, 2, 2);
search(bb, 3, 2);
int first[4];
memcpy(first, bb, sizeof(first));
search(first, 0, 1);
search(first, 1, 1);
search(first, 2, 1);
search(first, 3, 1);
;
for (int i = 0; i < 4; i++) {
int tmp[4];
memcpy(tmp, bb, sizeof(tmp));
tmp[i] += i < 2 ? 1 : -1;
if (ok(tmp) and bb[i] == first[i] and test(tmp) == 1) {
bb[i] = tmp[i];
break;
}
};
search(bb, 0, 1);
search(bb, 1, 1);
search(bb, 2, 1);
search(bb, 3, 1);
;
printf("! %d %d %d %d %d %d %d %d\n", first[0], first[1], first[2], first[3],
bb[0], bb[1], bb[2], bb[3]);
fflush(stdout);
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n[4];
int counter = 0;
for (int i = 0; i < 4; i++) cin >> n[i];
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
if ((n[i] == n[j]) && (i != j)) counter++;
}
}
if (counter == 3)
cout << 2 << endl;
else if (counter == 6)
cout << 3 << endl;
else
cout << counter << endl;
return 0;
}
| 800 | CPP |
s = input()
t = input()
D = {}
count = 1
for i in range(len(s)):
if s[i] != t[i]:
D[i] = 1
'''print(D)
for i in range(len(s)):
if i in D:
print(i)'''
if len(D) %2 == 1:
print('impossible')
else :
for i in range(len(s)):
if i not in D:
print(s[i],end = '')
else :
if count <= len(D)//2:
print(s[i],end = '')
count += 1
else :
print(t[i],end = '')
| 1,100 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct Latarnia {
double x, y, a;
friend istream& operator>>(istream& wejscie, Latarnia& dane) {
wejscie >> dane.x >> dane.y >> dane.a;
dane.a = (dane.a / 180.0) * M_PI;
return wejscie;
}
};
const int MAX = 20;
const double EPS = 10e-6, INF = 10e6;
int n;
Latarnia t[MAX];
double l, r, wynik[1 << MAX];
void wczytaj_dane() {
cin >> n >> l >> r;
for (int i = 0; i < (n); i++) cin >> t[i];
}
double przedluz(double x0, Latarnia& f) {
double x = x0 - f.x, y = -f.y, a = f.a;
double dx = x * cos(a) - y * sin(a), dy = x * sin(a) + y * cos(a);
if (dy > -EPS) return INF;
return f.x - (dx / dy) * f.y;
}
void wypelnij_wynik() {
wynik[0] = l;
for (int i = (1); i <= ((1 << n) - 1); i++) {
wynik[i] = l;
for (int j = 0; j < (n); j++)
if (((i >> j) & 1) == 1) {
int m = i - (1 << j);
wynik[i] = max(wynik[i], przedluz(wynik[m], t[j]));
}
}
}
void zrob_test() {
wczytaj_dane();
wypelnij_wynik();
cout << min(wynik[(1 << n) - 1], r) - l << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(12);
zrob_test();
return 0;
}
| 2,200 | CPP |
l=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,-1]
a,b=input().split()
if(b==str(l[l.index(int(a))+1])):
print("YES")
else:
print("NO") | 800 | PYTHON3 |
a=input()
b=len(a)
c=[i for i in range(b)]
r=0
for j in range(b):
if a[j]!='a' and a[j]!='e' and a[j]!='i' and a[j]!='o' and a[j]!='u' and a[j]!='A' and a[j]!='E' and a[j]!='I' and a[j]!='O' and a[j]!='U' and a[j]!='y' and a[j]!='Y':
c[r]=a[j]
r+=1
m=0
print('.',end='')
while m<r-1:
if ord(c[m])<96:
c[m]=chr(ord(c[m])+32)
print(c[m],end='.')
m+=1
if ord(c[m])<96:
c[m]=chr(ord(c[m])+32)
print(c[m])
| 1,000 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int mp[3][200001];
char ch[200001];
struct data {
int d1, d2, d3, d4;
};
struct segtree {
int l, r;
data d;
} t[600001];
data merge(data a, data b) {
data tmp;
tmp.d1 = min(500000000, min(a.d1 + b.d1, a.d2 + b.d3) + 1);
tmp.d2 = min(500000000, min(a.d1 + b.d2, a.d2 + b.d4) + 1);
tmp.d3 = min(500000000, min(a.d3 + b.d1, a.d4 + b.d3) + 1);
tmp.d4 = min(500000000, min(a.d3 + b.d2, a.d4 + b.d4) + 1);
return tmp;
}
void build(int k, int l, int r) {
t[k].l = l;
t[k].r = r;
if (l == r) {
t[k].d.d1 = t[k].d.d2 = t[k].d.d3 = t[k].d.d4 = 500000000;
if (mp[1][l]) t[k].d.d1 = 0;
if (mp[2][l]) t[k].d.d4 = 0;
if (mp[1][l] && mp[2][l]) t[k].d.d2 = t[k].d.d3 = 1;
return;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
t[k].d = merge(t[k << 1].d, t[k << 1 | 1].d);
}
data query(int k, int x, int y) {
int l = t[k].l, r = t[k].r;
if (x == l && y == r) return t[k].d;
int mid = (l + r) >> 1;
if (mid >= y)
return query(k << 1, x, y);
else if (mid < x)
return query(k << 1 | 1, x, y);
else
return merge(query(k << 1, x, mid), query(k << 1 | 1, mid + 1, y));
}
int ask(int x, int y) {
int a = (x - 1) % n + 1, b = (y - 1) % n + 1;
if (a > b) {
swap(x, y);
swap(a, b);
}
data tmp = query(1, a, b);
if (x <= n && y <= n) return tmp.d1;
if (x <= n && y > n) return tmp.d2;
if (x > n && y <= n) return tmp.d3;
if (x > n && y > n) return tmp.d4;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= 2; i++) {
scanf("%s", ch);
for (int j = 1; j <= n; j++)
if (ch[j - 1] == '.') mp[i][j] = 1;
}
build(1, 1, n);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
int ans = ask(x, y);
if (ans < 500000000)
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}
| 2,200 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5;
int n, x, y;
long long dp[N];
inline int read() {
int f = 1, x = 0;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return f * x;
}
int main() {
n = read(), x = read(), y = read();
for (int i = (1), iend = (n); i <= iend; i++) {
if (i & 1)
dp[i] =
min(dp[i - 1] + x, min(dp[i >> 1] + x + y, dp[(i + 1) >> 1] + x + y));
else
dp[i] = min(dp[i - 1] + x, dp[i >> 1] + y);
}
printf("%lld", dp[n]);
return 0;
}
| 2,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v[3];
vector<int> l;
double a[500010];
int main() {
int n, x, y;
bool chh = false;
double ans = DBL_MAX;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d", &x, &y);
int tmp = 0;
if (y > 0) tmp = 1;
v[tmp].push_back(pair<int, int>(x, y));
if (tmp == 1) {
l.push_back(x);
}
}
double lo = 0, hi = 1000000010;
double tmp = 0;
for (int r = 0; r < 100; r++) {
bool ch = false;
double mid = (lo + hi) / 2;
for (int i = 0; i < v[1].size(); i++) {
a[i] = (double)v[1][i].first + (double)(mid * v[1][i].second);
if (i > 0) a[i] = max(a[i], a[i - 1]);
}
for (int i = 0; i < v[0].size(); i++) {
tmp = v[0][i].first + (double)(mid * v[0][i].second);
int ind = upper_bound(l.begin(), l.end(), v[0][i].first) - l.begin();
ind--;
if (ind >= 0 && a[ind] >= tmp) {
ch = true;
break;
}
}
if (ch) {
hi = mid;
ans = min(ans, mid);
chh = true;
} else
lo = mid;
}
if (chh)
printf("%.20lf\n", ans);
else
printf("-1\n");
return 0;
}
| 2,300 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n, k;
cin >> n >> k;
long long i;
deque<long long> d;
map<long long, long long> mp;
for (i = 0; i < n; i++) {
long long x;
cin >> x;
if (mp[x] == 0) {
d.push_front(x);
mp[x] = 1;
if (d.size() > k) {
mp[d.back()] = 0;
d.pop_back();
}
}
}
cout << d.size() << endl;
for (auto i : d) {
cout << i << " ";
}
return 0;
}
| 1,000 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
for (int i = 0; i < test; i++) {
int n;
cin >> n;
int c = 0;
int d = 0;
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int b[n];
for (int i = 0; i < n; i++) {
cin >> b[i];
}
int k = 0, m = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 1) {
d++;
}
if (a[i] == -1) {
c++;
}
}
for (int i = n - 1; i >= 0; i--) {
if (a[i] == b[i]) {
if (a[i] == -1) {
c--;
}
if (a[i] == 1) {
d--;
}
} else if (a[i] > b[i]) {
if (a[i] == -1) {
c--;
} else {
if (a[i] == 1) {
d--;
}
}
if (c >= 1) {
a[i] = b[i];
} else {
k++;
cout << "NO" << endl;
break;
}
} else {
if (a[i] == 1) {
d--;
} else {
if (a[i] == -1) {
c--;
}
}
if (d >= 1) {
a[i] = b[i];
} else {
m++;
cout << "NO" << endl;
break;
}
}
}
if (k == 0 && m == 0) {
cout << "YES" << endl;
}
}
}
| 1,100 | CPP |
#include <bits/stdc++.h>
using namespace std;
char _;
const int maxN = 200 * 1000 + 5;
const int maxM = 5000 + 5;
const int mod = 1000 * 1000 * 1000 + 7;
const int base = 701;
const int SQ = 500;
const int maxL = 20;
int val[maxN];
vector<int> a[maxN];
int h[maxN], sz[maxN], ss[maxN];
int V, S;
int dp[maxN], D[maxN];
bool gg(int v) { return (sz[v] == 0); }
void dfs(int v, int par = -1) {
sz[v] = (val[v] < V);
ss[v] = 1;
dp[v] = 0;
int bes = 0;
for (auto u : a[v])
if (u - par) {
h[u] = h[v] + 1;
dfs(u, v);
sz[v] += sz[u];
ss[v] += ss[u];
if (gg(u))
dp[v] += ss[u];
else
bes = max(bes, dp[u]);
}
dp[v] += bes;
if (val[v] < V)
dp[v] = 0;
else
dp[v]++;
}
int n;
void DFS(int v, int par = -1) {
int sum = 0;
vector<pair<int, int> > pp;
if (sz[v] == S)
sum += (n - ss[v]);
else
pp.push_back({D[v], -1});
for (auto u : a[v])
if (u - par)
if (gg(u))
sum += ss[u];
else {
pp.push_back({dp[u], u});
sort(pp.rbegin(), pp.rend());
if (pp.size() > 2) pp.pop_back();
}
sum++;
for (auto u : a[v])
if (u - par) {
if (val[v] >= V) {
int tmp = sum;
if (gg(u)) tmp -= ss[u];
for (int i = 0; i < (int)pp.size(); i++)
if (pp[i].second != u) {
tmp += pp[i].first;
break;
}
D[u] = tmp;
} else
D[u] = 0;
DFS(u, v);
}
}
int k;
bool check(int t) {
V = t;
S = 0;
for (int i = 0; i < n; i++) S += (val[i] < V);
dfs(0);
DFS(0);
for (int i = 0; i < n; i++) {
if (val[i] < V) continue;
int ans = 1;
int bes = 0;
for (auto u : a[i])
if (h[u] > h[i]) {
if (gg(u))
ans += ss[u];
else
bes = max(bes, dp[u]);
} else {
if (sz[i] == S)
ans += (n - ss[i]);
else
bes = max(bes, D[i]);
}
if (ans + bes >= k) return true;
}
return false;
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> val[i];
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
v--;
u--;
a[v].push_back(u);
a[u].push_back(v);
}
int lo = 0, hi = 1000 * 1000 + 5;
while (hi - lo > 1) {
int mid = (hi + lo) >> 1;
if (check(mid))
lo = mid;
else
hi = mid;
}
cout << lo << endl;
return 0;
}
| 2,600 | CPP |
t = int(input())
for i in range(t):
count = 0
n = int(input())
while(n > 1):
if n % 3 != 0:
break
if n % 6 == 0:
n = n//6
count += 1
else:
n = n * 2
count += 1
if n == 1:
print(count)
else:
print(-1) | 900 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx")
using namespace std;
template <typename T, typename U>
inline void smin(T &a, const U &b) {
if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, const U &b) {
if (a < b) a = b;
}
char gchar() {
static char tbuf1[1000], *tmp_st = tbuf1, *tmp_ed = tbuf1;
if (tmp_st == tmp_ed) {
int len = fread(tbuf1, 1, 1000, stdin);
if (len == 0) return -1;
tmp_ed = tbuf1 + len;
tmp_st = tbuf1;
}
return *(tmp_st++);
}
inline bool gn(int &x) {
char c, sg = 0;
while (c = gchar(), (c > '9' || c < '0') && c != '-' && ~c)
;
if (c == -1) return 0;
for ((c == '-' ? sg = 1, c = gchar() : 0), x = 0; c >= '0' && c <= '9';
c = gchar())
x = (x << 1) + (x << 3) + c - '0';
if (sg) x = -x;
return 1;
}
int id[100100], LL[400400], RR[400400];
int xx, XX, yy, YY, zz, ZZ;
int Xmn = 100100, Ymn = 100100, Zmn = 100100, Xmx, Ymx, Zmx;
int x[100100], y[100100], z[100100], xmn[400400], xmx[400400], ymn[400400],
ymx[400400];
vector<int> vec[400400];
inline void select(int L, int mid, int R, int *val) {
R--;
while (L + 1 <= R) {
if (L + 1 == R) {
if (val[id[L]] > val[id[R]]) swap(id[L], id[R]);
return;
}
int S = L + 1, X = S + rand() * rand() % (R - L);
swap(id[L], id[X]);
if (val[id[L]] > val[id[R]]) swap(id[L], id[R]);
if (val[id[L]] > val[id[S]]) swap(id[L], id[S]);
if (val[id[S]] > val[id[R]]) swap(id[S], id[R]);
int i = L, j = R, p = val[id[S]];
while (1) {
while (i <= R && val[id[i]] <= p) i++;
while (j >= L && val[id[j]] >= p) j--;
if (i > j) break;
swap(id[i], id[j]);
}
if (mid <= j)
R = j;
else if (mid >= i)
L = i;
else
break;
}
}
inline void build(int u, int L, int R, int dp = 0) {
LL[u] = L, RR[u] = R;
if (L + 1 == R) {
xmn[u] = xmx[u] = x[id[L]];
ymn[u] = ymx[u] = y[id[L]];
vec[u].push_back(z[id[L]]);
return;
}
int ls = u << 1, rs = ls | 1, mid = L + R >> 1;
if (dp)
select(L, mid, R, x);
else
select(L, mid, R, y);
dp ^= 1;
build(ls, L, mid, dp);
build(rs, mid, R, dp);
for (int i = 0, j = 0, mn;;) {
mn = i < vec[ls].size() ? vec[ls][i] : 100100;
if (j < vec[rs].size() && mn >= vec[rs][j])
vec[u].push_back(vec[rs][j++]);
else if (mn < 100100)
vec[u].push_back(vec[ls][i++]);
else
break;
}
xmn[u] = min(xmn[ls], xmn[rs]), xmx[u] = max(xmx[ls], xmx[rs]);
ymn[u] = min(ymn[ls], ymn[rs]), ymx[u] = max(ymx[ls], ymx[rs]);
}
inline bool calc(int u) {
if (xmx[u] < xx || xmn[u] > XX || ymx[u] < yy || ymn[u] > YY) return false;
if (xx <= xmn[u] && xmx[u] <= XX && yy <= ymn[u] && ymx[u] <= YY) {
return upper_bound(vec[u].begin(), vec[u].end(), ZZ) -
lower_bound(vec[u].begin(), vec[u].end(), zz);
}
if (LL[u] + 1 == RR[u]) return false;
int ls = u << 1;
if (calc(ls)) return true;
return calc(ls | 1);
}
inline bool in(int x, int y, int z) {
return Xmn <= x && x <= Xmx && Ymn <= y && y <= Ymx && Zmn <= z && z <= Zmx;
}
int main() {
srand(time(0));
int n, m, k;
for (int i = 2; i--;) gn(n), gn(m), gn(k);
while (n--) {
gn(xx), smin(Xmn, xx), smax(Xmx, xx);
gn(yy), smin(Ymn, yy), smax(Ymx, yy);
gn(zz), smin(Zmn, zz), smax(Zmx, zz);
}
for (int i = 0; i < m; i++) {
gn(x[i]), gn(y[i]), gn(z[i]);
if (in(x[i], y[i], z[i])) return puts("INCORRECT");
id[i] = i;
}
if (m) build(1, 0, m);
puts("CORRECT");
while (k--) {
int x, y, z;
gn(x), gn(y), gn(z);
if (in(x, y, z))
puts("OPEN");
else if (m) {
xx = min(Xmn, x), XX = max(Xmx, x);
yy = min(Ymn, y), YY = max(Ymx, y);
zz = min(Zmn, z), ZZ = max(Zmx, z);
if (calc(1))
puts("CLOSED");
else
puts("UNKNOWN");
} else
puts("UNKNOWN");
}
}
| 2,700 | CPP |
n,k=(int(x) for x in input().split())
p=[int(x) for x in input().split()]
p.sort()
q=[]
for i in range(len(p)):
if p[i]>0:
q=p[i:len(p)]
break
q.sort(reverse=True)
if len(q)<=k:
print(len(q))
else:
num=k
for i in range(k,len(q)):
if q[i]==q[k-1]:
num+=1
else:
break
print(num)
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, t;
long long p[200];
struct Point {
int x, y;
} a[200];
bool cmp(const Point &a, const Point &b) { return a.y < b.y; }
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].x, &a[i].y);
sort(a + 1, a + n + 1, cmp);
scanf("%d", &t);
for (int i = 1; i <= t; ++i) cin >> p[i];
p[t + 1] = (long long)(1) << 60;
int k = 1;
long long sum = 0, kk = 0;
for (int i = 1; i <= n; ++i) {
if (p[k] - kk <= a[i].x) {
sum += (p[k] - kk) * k * a[i].y;
a[i].x -= p[k] - kk;
kk = p[k];
k++;
i--;
} else {
sum += (long long)a[i].x * k * a[i].y;
kk += a[i].x;
a[i].x = 0;
}
}
cout << sum << endl;
return 0;
}
| 1,600 | CPP |
def solve():
n = int(input())
a = [1] * n
for i in range(n):
if i % 3 == 2:
a[i] = 3
print(*a, sep = ' ')
test = int(input())
for _ in range(test):
solve()
| 800 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const LL INF = 0x3f3f3f3f;
const LL INF64 = 0x3f3f3f3f3f3f3f3f;
const int N = 5e4 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
int sy, a, b;
cin >> sy >> a >> b;
int n;
cin >> n;
vector<pair<int, int>> segs(n);
vector<LL> pre1(n), pre2(n);
for (int i = 0; i < n; ++i) {
cin >> segs[i].first >> segs[i].second;
int tmp = segs[i].first - segs[i].second;
pre1[i] = (i == 0 ? 0 : pre1[i - 1]) + tmp;
pre2[i] = (i == 0 ? 0 : pre2[i - 1]) - tmp;
}
auto get_range = [](const vector<LL> &pre, int l, int r) {
if (l > r) return 0LL;
return pre[r] - (l == 0 ? 0 : pre[l - 1]);
};
auto fit = [&](double x) {
if (x > b)
x = b;
else if (x < a)
x = a;
return x;
};
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
auto f = [&](LL x2) { return 1.0 * (sy * (x - x2) + y * x2) / y; };
int l = 0, r = n - 1;
while (l < r) {
int mid = r - (r - l) / 2;
int x2 = segs[mid].first;
if (f(x2) < a)
l = mid;
else
r = mid - 1;
}
int t1 = l + 1;
double ans = 0;
ans += fit(f(segs[l].second)) - fit(f(segs[l].first));
l = l + 1;
r = n - 1;
while (l < r) {
int mid = l + (r - l) / 2;
int x2 = segs[mid].second;
if (f(x2) > b)
r = mid;
else
l = mid + 1;
}
int t2 = l - 1;
if (l < n) ans += fit(f(segs[l].second)) - fit(f(segs[l].first));
ans += 1.0 * sy * get_range(pre1, t1, t2) / y + get_range(pre2, t1, t2);
cout << ans << '\n';
}
return 0;
}
| 2,400 | CPP |
import sys
t = int(input())
for _ in range(t):
line = input()
d = int(line[0])*10
l = 4
while l > len(line):
d -= l
l -= 1
print(d)
| 800 | PYTHON3 |
S = input()
if "0000000" in S or "1111111" in S:
print("YES")
else:
print ("NO")
| 900 | PYTHON3 |
#include <bits/stdc++.h>
const int maxn = (1 << 22) + 1;
int n, m;
int points[maxn];
bool marked[maxn] = {false}, visited[maxn] = {false};
void dfs(int p) {
visited[p] = true;
if (marked[p]) {
int q = (1 << n) - 1 - p;
if (!visited[q]) dfs(q);
}
for (int i = 0; i < n; i++) {
int q = p & ~(1 << i);
if (!visited[q]) dfs(q);
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) {
int p;
scanf("%d", &p);
points[i] = p;
marked[p] = true;
}
int count = 0;
for (int i = 0; i < m; i++) {
int p = points[i];
if (!visited[p]) {
count++;
visited[p] = true;
int q = (1 << n) - 1 - p;
dfs(q);
}
}
printf("%d\n", count);
return 0;
}
| 2,500 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 10, step = 442, len = N / 440;
char s2[N], z[90];
int s[N], sw[len], sum[len][2], ans[len][2], l[len], r[len];
void calc(int l, int r, int &a, int &b, int &sum1, int &sum0, int w) {
a = b = sum1 = sum0 = 0;
int *ss = s + l;
for (int i = r - l; i >= 0; --i) sum1 += *(ss++) ^= w;
sum0 = r - l + 1 - sum1;
ss = s + l;
int a1 = sum1, a0 = sum0;
for (int i = r - l; i >= 0; --i) {
a1 -= *ss;
a0 -= (*ss) ^ 1;
a1++, a0++;
if (a < a1) a = a1;
if (b < a0) b = a0;
a1 -= *ss;
a0 -= (*(ss++)) ^ 1;
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
scanf(" %s", s2);
for (int i = 0; i < n; ++i) s[i] = s2[i] != '4';
memset(l, -1, sizeof l);
for (int i = 0, j = 0;; i += step, j++) {
l[j] = i;
r[j] = min(i + step - 1, n - 1);
calc(l[j], r[j], ans[j][0], ans[j][1], sum[j][0], sum[j][1], 0);
if (r[j] == n - 1) break;
}
for (int _ = 0; _ < m; ++_) {
scanf(" %s", z);
if (z[0] == 'c') {
int best = 0;
int s0 = 0, s1 = 0;
for (int j = 0; l[j] != -1; j++) s1 += sum[j][sw[j]];
for (int j = 0; l[j] != -1; j++) {
s1 -= sum[j][sw[j]];
best = max(best, ans[j][sw[j]] + s1 + s0);
s0 += sum[j][sw[j] ^ 1];
}
printf("%d\n", best);
} else {
int left, right;
scanf("%d %d", &left, &right);
left--, right--;
for (int j = 0; l[j] != -1; j++) {
if (r[j] < left) continue;
if (l[j] > right) break;
if (l[j] >= left && r[j] <= right) {
sw[j] ^= 1;
continue;
}
int rr = min(r[j], right), ll = max(l[j], left);
int *ss = s + ll;
for (int i = rr - ll; i >= 0; --i) *(ss++) ^= 1;
calc(l[j], r[j], ans[j][0], ans[j][1], sum[j][0], sum[j][1], sw[j]);
sw[j] = 0;
}
}
}
return 0;
}
| 2,400 | CPP |
def solve():
many = int(input())
group = 1
polar1 = 0
polar2 = 0
i = 0
for _ in range(many):
magnets = list(map(int, input()))
if i == 0:
polar1 = magnets[1]
else:
polar2 = magnets[0]
if polar2 == polar1:
group += 1
polar1 = magnets[1]
i = 0
i += 1
print(group)
solve()
| 800 | PYTHON3 |
Subsets and Splits