solution
stringlengths 10
983k
| difficulty
int64 0
25
| language
stringclasses 2
values |
---|---|---|
import sys
import math
a = int(sys.stdin.readline().strip())
for i in range(a):
num_biji, num_tuhua, bi, tu, k = map(int, sys.stdin.readline().strip().split())
need_b = math.ceil(num_biji/bi)
need_t = math.ceil(num_tuhua/tu)
if need_b + need_t > k:
print(-1)
else:
print(f'{need_b} {need_t}')
| 7 | PYTHON3 |
a,b,c,d,e,f,g,h=map(int,input().split())
print(min(((b*c)//a)//g,(d*e)//a,(f//a)//h)) | 7 | PYTHON3 |
t = int(input())
for _ in range(t):
a, b, x, y, n = [int(x) for x in input().split()]
j = min(n, a-x)
k = min(n, b-y)
if a - j < b - k:
print((a-j)*(b-min((n-j), k)))
else:
print((b-k)*(a-min((n-k), j)))
| 8 | PYTHON3 |
# your code goes here
T = int(input())
for i in range(T):
l, r = input().split(" ")
print(str(int(l)) + " " + str(int(l)*2)) | 7 | PYTHON3 |
#include <iostream>
using namespace std;
int r,g,b,n;
int cnt;
int main(){
cin>>r>>g>>b>>n;
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
if((n-r*i-g*j)%b==0&&n>=r*i+g*j){
cnt++;
}
}
}
cout<<cnt;
return 0;
} | 0 | CPP |
n, k = [int(item) for item in input().split()]
MOD = 10**9 + 7
MAX_N = 10**4
fac = [1] + [0] * MAX_N
fac_inv = [1] + [0] * MAX_N
for i in range(1, n+1):
fac[i] = fac[i-1] * (i) % MOD
fac_inv[i] = fac_inv[i-1] * pow(i, MOD-2, MOD) % MOD
def mod_nCr(n, r):
if n == 0 and r == 0:
return 1
if n < r or n < 0:
return 0
tmp = fac_inv[n-r] * fac_inv[r] % MOD
return tmp * fac[n] % MOD
ans = 0
for i in range(n+1):
base = pow(k, n - i, MOD) * pow(k - 1, i, MOD) - pow(k - 1, n, MOD) + MOD
base % MOD
val = pow(-1, i) * mod_nCr(n, i) * pow(base, n, MOD)
ans += val
ans %= MOD
print(ans) | 11 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
bool vis[7];
int n, top, l, r;
while (scanf("%d", &n) == 1) {
scanf("%d", &top);
memset(vis, false, sizeof(vis));
for (int i = 0; i < n; i++) {
scanf("%d %d", &l, &r);
vis[l] = true;
vis[7 - l] = true;
vis[r] = true;
vis[7 - r] = true;
}
if (vis[top] == false) {
puts("YES");
} else {
puts("NO");
}
}
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int i, j, k, t, ans = 0, n;
string second;
cin >> second;
n = second.length();
long long int even[2], odd[2];
even[0] = even[1] = odd[0] = odd[1] = 0;
long long int ans_even = 0, ans_odd = 0;
for (i = 0; i < n; i++) {
ans_odd++;
j = second[i] - 'a';
if (i % 2 == 0) {
ans_even += odd[j];
ans_odd += even[j];
even[j]++;
} else {
ans_even += even[j];
ans_odd += odd[j];
odd[j]++;
}
}
cout << ans_even << " " << ans_odd << endl;
return 0;
}
| 10 | CPP |
import sys
n, t, c = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
beg = 0
end = 0
ans = 0
for i in range(n):
if a[i] > t:
if end == i-1:
ans += max(end-beg-c+2,0)
beg = i+1
end = i+1
elif i == n-1:
ans += max(n-beg-c+1,0)
else:
end = i
print(ans)
| 8 | PYTHON3 |
n = int(input())
cnt = 1
for i in range(1, n + 1):
res = (n - cnt) // 2
print('*' * res + 'D' * cnt + '*' * res)
if i < (n + 1) // 2:
cnt += 2
else:
cnt -= 2
| 7 | PYTHON3 |
#include <cstdio>
int main() {
int N, A, B;
scanf("%d%d%d", &N, &A, &B);
(B - A) % 2 == 0 ? printf("Alice\n") : printf("Borys\n");
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
double pi = 3.1415926535898;
long long n, k;
int main() {
cin >> n >> k;
vector<long long> v;
for (long long i = 2; i <= (n); i++) {
while (n % i == 0) {
v.push_back(i);
n /= i;
}
}
if (v.size() < k) {
cout << -1;
return 0;
}
if (v.size() == k)
for (int i = 0; i < k; i++) {
cout << v[i] << ' ';
}
if (v.size() > k) {
long long ans = 1;
for (int i = 0; i < k - 1; i++) {
cout << v[i] << ' ';
}
for (int i = k - 1; i < v.size(); i++) {
ans *= v[i];
}
cout << ans << endl;
}
}
| 7 | CPP |
T = int(input())
for testcase in range(1,T+1):
n = int(input())
a = list(map(int, input().split()))
x = min(a)
i = 0
count = n//2
while count > 0:
v = a[i]
if v != x:
print(v,x)
count -= 1
i += 1
| 8 | PYTHON3 |
n,a,b=[int(j) for j in input().split()]
x=[int(j) for j in input().split()]
ans=0
for i in range(n-1):
ans+=min((x[i+1]-x[i])*a,b)
print(ans) | 0 | PYTHON3 |
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
print(max(sum(A[:n+1]+B[n:]) for n in range(N))) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int mayoke = mt19937(0xe869120)() % 20;
vector<string> s(n);
unordered_map<string, set<int> > tbl;
for (int i = (int)(0); i < (int)(n); i++) {
cin >> s[i];
for (int j = (int)(0); j < (int)(9); j++) {
s[i][j] += mayoke;
}
for (int j = (int)(0); j < (int)(9); j++) {
for (int k = (int)(1); k < (int)(10 - j); k++) {
string t = s[i].substr(j, k);
if (tbl[t].size() <= 1) {
tbl[t].insert(i);
}
}
}
}
vector<string> ans(n);
vector<int> len(n, 100);
for (auto &p : tbl) {
if (p.second.size() != 1) {
continue;
}
string u = p.first;
int idx = *p.second.begin();
if (len[idx] > (int)u.length()) {
len[idx] = u.length();
ans[idx] = u;
}
}
for (int i = (int)(0); i < (int)(n); i++) {
for (int j = (int)(0); j < (int)(ans[i].size()); j++) {
ans[i][j] -= mayoke;
}
cout << ans[i] << "\n";
}
}
| 8 | CPP |
num1= 102001
num2= 2
prime=[1]*num1
prime[1]=0
prime[0]=0
for i in range(num2,num1):
j=i
while(j+i<num1):
j+=i
prime[j]=0
l=[]
n,m=map(int,input().split())
for i in range(n):
t=list(map(int,input().split()))
l.append(t)
ans=100000
for i in range(n):
tot=0
for j in range(m):
result=l[i][j]
for k in range(result,num1):
if prime[k]==1:
tot+=k-result
break
ans=min(ans,tot)
for j in range(m):
tot=0
for i in range(n):
result=l[i][j]
for k in range(result,num1):
if prime[k]==1:
tot+=k-result
break
ans=min(ans,tot)
print(ans)
| 8 | PYTHON3 |
string = input()
numbers = string.split(" ")
a = int(numbers[0])
b = int(numbers[1])
c = int(numbers[2])
condition = c - a == 0 or c - a >= b
if (c - a) % b <= 1 and condition:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long long tpp = 9e18;
int tp = 1e9 + 1;
long long n, a[100005];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
;
for (int i = 1; i <= n; i++) cin >> a[i];
;
long long zz = a[1], c = 0;
for (int i = 2; i <= n; i++)
if (a[i] == zz) c++;
if (c == n - 1) {
cout << "YES" << endl;
;
return 0;
;
}
if (n == 1) {
cout << "YES" << endl;
;
return 0;
;
}
sort(a + 1, a + n + 1);
long long x = a[1], t, w = -1, z = -1;
for (int i = 2; i <= n; i++) {
if (a[i] != x) {
t = a[i];
break;
}
}
for (int i = 1; i <= n; i++) {
if (a[i] != t and a[i] != x) {
w = a[i];
break;
}
}
if (w == -1) {
cout << "YES" << endl;
;
} else {
for (int i = 1; i <= n; i++) {
if (a[i] != t and a[i] != x and a[i] != w) {
z = a[i];
break;
}
}
if (z != -1)
cout << "NO" << endl;
else {
if (t > w) {
if ((x + t) == 2 * w)
cout << "YES" << endl;
else
cout << "NO" << endl;
;
}
if (w > t) {
if ((x + w) == 2 * t)
cout << "YES" << endl;
else
cout << "NO" << endl;
;
}
}
}
return 0;
;
}
| 8 | CPP |
t = int(input())
for i in range(t):
n,m,x,y = map(int,input().split())
ans = 0
for j in range(n):
a = input()
if len(a) == 1:
if a[0] == ".":
ans += x
else:
k= 0
while k<len(a):
if k == len(a)-1:
if a[k] == ".":
ans += x
k+=1
else:
if a[k] == ".":
if a[k+1] == ".":
k +=2
if 2*x>y:
ans += y
else:
ans += 2*x
else:
k +=1
ans += x
else:
k += 1
print(ans)
| 8 | PYTHON3 |
s=input().split()
n=int(s[0])
x=int(s[1])
s=input().split()
ans=max(0,int(s[0])+int(s[1])-x)
prev=max(0,int(s[1])-ans)
for i in range(2,len(s)):
now=max(0,int(s[i])+prev-x)
ans+=now
prev=int(s[i])-now
print(ans) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int max_n = 100010;
int Count[max_n] = {0};
int Step[max_n] = {0};
int n, val, ans = INT_MAX;
int visit[max_n] = {0};
void bfs(int k) {
queue<int> que;
que.push(val);
int dis = 1;
Count[val]++;
visit[val] = k;
while (!que.empty()) {
int S = que.size();
while (S--) {
int p = que.front();
que.pop();
if (p * 2 <= 1e5 && visit[p * 2] != k) {
visit[p * 2] = k;
Count[p * 2]++;
Step[p * 2] += dis;
que.push(p * 2);
}
if (p / 2 >= 1 && visit[p / 2] != k) {
visit[p / 2] = k;
Count[p / 2]++;
Step[p / 2] += dis;
que.push(p / 2);
}
}
dis++;
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &val);
bfs(i);
}
for (int i = 1; i <= 1e5; i++) {
if (Count[i] != n) continue;
ans = min(ans, Step[i]);
}
printf("%d", ans);
return 0;
}
| 9 | CPP |
I=lambda:map(int,input().split())
n,m=I()
N=list(I())
p=set()
for i in range(m):
x,y=I();x-=1;y-=1
p.add((x,y)if x<y else(y,x))
r=1e9
for i in range(n):
for j in range(i):
for k in range(j):
if(j,i)in p and(k,i)in p and(k,j)in p:
r = min(r,N[i]+N[j]+N[k])
print(-1 if r>1e8 else r) | 7 | PYTHON3 |
# -*- coding: utf-8 -*-
"""
Created on Sun May 3 21:15:56 2020
@author: Kashem Pagla
"""
n=int(input())
d=list(map(int, input().split()))[:n]
m=[]
mx=max(d)
shit=[]
for i in range(len(d)):
if(mx%d[i]!=0):
m.append(d[i])
for i in range(len(d)):
if(d.count(d[i])==2):
shit.append(d[i])
if(mx in shit):
shit.remove(mx)
if(len(m)>0):
print(mx, max(m))
else:
if(d.count(mx)==2):
print(mx,mx)
# elif(max(shit))
else:
print(mx,max(shit)) | 8 | PYTHON3 |
n, m = tuple(map(int, input().split()))
print(' '.join(map(str, reversed([((n - (m - 1)) * (n - m) // 2), n // m * (m - n % m) * (n // m - 1) // 2 + ((n // m + 1) * (n % m) * (n // m)) // 2]))))
| 8 | PYTHON3 |
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF (1 << 30)
int main()
{
int N, M;
cin >> N >> M;
vector<int> D(N);
vector<int> C(M);
for (int i = 0; i < N; i++)
cin >> D[i];
for (int i = 0; i < M; i++)
cin >> C[i];
vector<vector<int> > dp(M + 1, vector<int>(N + 1, INF));
dp[0][0] = 0;
for (int i = 1; i <= M; i++)
{
for (int j = 0; j <= N; j++)
{
if (j == 0)
{
dp[i][j] = dp[i - 1][j];
}
else
{
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1] + C[i - 1] * D[j - 1]);
}
}
}
cout << dp[M][N] << endl;
return 0;
} | 0 | CPP |
def gcd(a,b):
return a if b==0 else gcd(b,a%b)
def getDivideOperations(a,b):
c=0
while(a>b):
if(a%8==0 and (a>>3)>=b):
a>>=3
elif(a%4==0 and (a>>2)>=b):
a>>=2
elif(a%2==0 and (a>>1)>=b):
a>>=1
else:
return -1
c+=1
return c
def getMultiplyOperations(a,b):
c=0
while(a<b):
if((a<<3)<=b):
a<<=3
elif((a<<2)<=b):
a<<=2
elif((a<<1)<=b):
a<<=1
else:
return -1
c+=1
return c
t=int(input())
while(t!=0):
a,b=map(int,input().split())
if(a!=b):
print(getDivideOperations(a,b) if a>b else getMultiplyOperations(a,b))
else:
print(0)
t-=1
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> cnt[256];
const int mod = 1e9 + 7;
const double eps = 1e-9;
int n, k, h;
pair<pair<int, double>, int> a[100001];
vector<int> solve(double time) {
int it = 1;
vector<int> res;
for (int i = (0); i < (n); i++)
if ((double)(it * h) - time * a[i].first.second < 1e-10)
it++, res.push_back(a[i].second + 1);
return res;
}
bool able(double time) {
int it = 1;
for (int i = (0); i < (n); i++) {
double p = (double)(it * h) - time * a[i].first.second;
if ((double)(it * h) - time * a[i].first.second < 1e-10) it++;
}
return it > k;
}
int main() {
cin >> n >> k >> h;
for (int i = (0); i < (n); i++) cin >> a[i].first.first, a[i].second = i;
for (int i = (0); i < (n); i++) cin >> a[i].first.second;
sort(a, a + n);
double l = 0, r = 20000000000;
for (int i = 0; i < 500; i++) {
double m = (r + l) / 2.0;
if (able(m))
r = m;
else
l = m;
}
vector<int> res = solve(r);
for (int i = (0); i < (res.size()); i++) cout << res[i] << " ";
}
| 8 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long bigmod(long long p, long long e, long long M) {
long long ret = 1;
for (; e > 0; e >>= 1) {
if (e & 1) ret = (ret * p) % M;
p = (p * p) % M;
}
return ret;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long modinverse(long long a, long long M) { return bigmod(a, M - 2, M); }
const int N = 2501;
int a[N][N];
pair<int, pair<int, int> > e[N * N];
bitset<N> row[N];
int main() {
int n;
scanf("%d", &n);
;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) scanf("%d", &a[i][j]);
;
bool flag = 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
if (a[i][j] != a[j][i]) flag = 0;
if (i == j && a[i][j]) flag = 0;
}
if (!flag) {
cout << "NOT MAGIC";
return 0;
}
int ec = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
e[ec++] = make_pair(a[i][j], make_pair(i, j));
}
}
sort(e, e + ec);
reverse(e, e + ec);
for (int i = 0; i < ec && flag; i++) {
int j = i + 1;
while (j < ec && e[j].first == e[i].first) ++j;
for (int k = i; k < j; k++) {
int x = e[k].second.first, y = e[k].second.second;
row[x][y] = 1;
}
for (int k = i; k < j; k++) {
int x = e[k].second.first, y = e[k].second.second;
if ((row[x] | row[y]).count() != n) {
flag = 0;
}
}
i = j - 1;
}
printf("%s", flag ? "MAGIC" : "NOT MAGIC");
return 0;
}
| 12 | CPP |
for _ in range(int(input())):
n,x = [*map(int, input().split())]
print(x+x) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k, i, j, counter = 0, siz;
cin >> n >> k;
siz = n * n;
int arr[n + 5][n + 5];
for (i = n; i >= 1; i--) {
for (j = n; j >= k; j--) {
arr[i][j] = siz;
if (j == k) counter += siz;
siz--;
}
}
for (i = n; i >= 1; i--) {
for (j = k - 1; j >= 1; j--) {
arr[i][j] = siz;
siz--;
}
}
cout << counter << endl;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:667772160")
template <class T1>
void deb(T1 e1) {
cout << e1 << endl;
}
template <class T1, class T2>
void deb(T1 e1, T2 e2) {
cout << e1 << " " << e2 << endl;
}
template <class T1, class T2, class T3>
void deb(T1 e1, T2 e2, T3 e3) {
cout << e1 << " " << e2 << " " << e3 << endl;
}
template <class T1, class T2, class T3, class T4>
void deb(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 deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) {
cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6
<< endl;
}
int main() {
int n, m, ans;
scanf("%d %d", &n, &m);
if (n > m) swap(n, m);
if (n == 1)
ans = m;
else if (n == 2) {
if (m % 4 == 0)
ans = (n * m) / 2;
else if (m % 4 == 1)
ans = n * (m - 1) / 2 + 2;
else if (m % 4 == 2)
ans = n * (m - 2) / 2 + 4;
else if (m % 4 == 3)
ans = n * (m - 3) / 2 + 4;
} else
ans = (n * m + 1) / 2;
deb(ans);
return 0;
}
| 8 | CPP |
a,b,c=input().split(" ")
a,b,c=int(a),int(b), int(c)
a,b,c=min(a,b,c),a+b+c-min(a,b,c)-max(a,b,c),max(a,b,c)
ans=0
while a+b<=c:
ans+=1
a+=1
print(ans) | 7 | PYTHON3 |
import sys
input=sys.stdin.readline
mod =1000000007
pro=6
n=int(input())
pro=pro*pow(4,(1<<n)-2,mod)
pro%=mod
print(pro)
| 11 | PYTHON3 |
#include <bits/stdc++.h>
template <typename Arg1>
void ZZ(const char* name, Arg1&& arg1) {
std::cout << name << " = " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
std::cout.write(names, comma - names) << " = " << arg1;
ZZ(comma, args...);
}
using namespace std;
long long n, m, q;
vector<long long> ar;
vector<set<long long>> v;
long long dist(long long i, long long ind) {
if (*v[i].begin() == ind) {
return *v[i].rbegin() - *v[i].begin();
}
return *v[i].begin() - *v[i].rbegin();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
ar.resize(2 * n);
v.resize(n + 1);
for (long long i = 0; i < 2 * n; i++) {
cin >> ar[i];
v[ar[i]].insert(i);
}
long long ans = 0;
for (long long x = 0; x < n; x++) {
for (long long i = 0; i < 2 * n - 1; i++) {
long long di = dist(ar[i], i);
long long di1 = dist(ar[i + 1], i + 1);
if (di > 1) {
if (di1 < 0 || di - di1 >= 2) {
v[ar[i]].erase(i);
v[ar[i]].insert(i + 1);
v[ar[i + 1]].erase(i + 1);
v[ar[i + 1]].insert(i);
swap(ar[i], ar[i + 1]);
ans++;
}
}
}
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
#include<cstdio>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int WHITE = 0;
//static const int BLACK = 1;
vector<int> V[MAX];
//int n;
int color[MAX] = {0};
void dfs(int u,int c)
{
stack<int> S;
S.push(u);
color[u] = c;
while(!S.empty())
{
int v = S.top();
S.pop();
for(int i=0; i<V[v].size(); i++)
{
int k = V[v][i];
if(color[k] == WHITE)
{
S.push(k);
color[k] = c;
}
}
}
}
int main()
{
int m,n;
scanf("%d%d", &n, &m);
int u,v;
for(int i=0; i<m; i++)
{
scanf("%d%d", &u, &v);
V[u].push_back(v);
V[v].push_back(u);
}
int c = 1;
for(int i=0; i<n; i++)
{
if(color[i] == WHITE)
dfs(i,c++);
}
int q;
scanf("%d", &q);
for(int i=0; i<q; i++)
{
scanf("%d%d",&u,&v);
if(color[u] == color[v])
printf("yes\n");
else
printf("no\n");
}
return 0;
}
| 0 | CPP |
input()
l=list(map(int,input().split()))
print('YES' if sum(i!=j for i,j in zip(l,sorted(l)))<3 else 'NO') | 0 | PYTHON3 |
l=['H','Q','9']
s=input()
c=0
for i in range(len(s)):
if s[i] in l:
c+=1
break
if c!=0:
print("YES")
else:
print("NO") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1;
cin >> s1;
string s2;
cin >> s2;
for (int i = 0; i < s1.length(); i++) {
s1[i] = tolower(s1[i]);
}
for (int i = 0; i < s2.length(); i++) {
s2[i] = tolower(s2[i]);
}
int result = s1.compare(s2) == 0 ? 0 : s1.compare(s2) > 0 ? 1 : -1;
cout << result;
}
| 7 | CPP |
t=int(input())
for _ in range(t):
a,b=map(int,input().split())
if a%b==0:
print(0)
else:
temp=a//b
a-=temp*b
print(b-a)
| 10 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int a[200200];
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
a[i] += a[i - 1];
}
int ans = a[n];
for (int i = n - 1; i > 1; i--) ans = max(ans, a[i] - ans);
cout << ans << endl;
}
| 11 | CPP |
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> ii;
typedef long long ll;
typedef pair <ll, int> lli;
const int Maxn = 100005;
const ll has = 1000000000000000ll;
int n, m, s, t;
vector <ii> neighS[Maxn], neighT[Maxn];
ll distS[Maxn], distT[Maxn];
ll mx;
ll res[Maxn];
void Dijkstra(int v, vector <ii> neigh[], ll dist[])
{
fill(dist, dist + Maxn, has); dist[v] = 0;
priority_queue <lli> Q; Q.push(lli(-dist[v], v));
while (!Q.empty()) {
int v = Q.top().second;
ll d = -Q.top().first; Q.pop();
if (dist[v] != d) continue;
for (int i = 0; i < neigh[v].size(); i++) {
ii u = neigh[v][i];
if (d + u.second < dist[u.first]) {
dist[u.first] = d + u.second;
Q.push(lli(-dist[u.first], u.first));
}
}
}
}
int main()
{
scanf("%d %d %d %d", &n, &m, &s, &t);
for (int i = 0; i < m; i++) {
int u, v, a, b; scanf("%d %d %d %d", &u, &v, &a, &b);
neighS[u].push_back(ii(v, a));
neighS[v].push_back(ii(u, a));
neighT[u].push_back(ii(v, b));
neighT[v].push_back(ii(u, b));
}
Dijkstra(s, neighS, distS);
Dijkstra(t, neighT, distT);
for (int i = n - 1; i >= 0; i--) {
mx = max(mx, has - distS[i + 1] - distT[i + 1]);
res[i] = mx;
}
for (int i = 0; i <= n - 1; i++)
printf("%lld\n", res[i]);
return 0;
} | 0 | CPP |
# square869120Contest #4
# B - Buildings are Colorful!
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b
N, K = map(int, input().split())
*A, = map(int, input().split())
ans = float('inf')
for bit in range(2**N):
cnt = 0
for i in range(N):
if bit >> i & 1:
cnt += 1
if cnt < K:
continue
cost = 0
maxh = 0
for i, a in enumerate(A):
if bit >> i & 1 and maxh >= a:
cost += maxh - a + 1
maxh += 1
elif maxh < a:
maxh = a
if cost < ans:
ans = cost
print(ans)
| 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
vector<int> G[MAXN];
int l[MAXN], r[MAXN], eN;
inline int other(int e, int u) { return (l[e] == u ? r[e] : l[e]); }
stack<int> s;
int comp[MAXN], ind[MAXN], indN;
bool active[MAXN];
void tarjan(int u) {
comp[u] = ind[u] = indN++;
s.push(u);
active[u] = true;
for (int i = 0; i < G[u].size(); i++) {
int v = r[G[u][i]];
if (ind[v] == -1) {
tarjan(v);
comp[u] = min(comp[u], comp[v]);
} else if (active[v])
comp[u] = min(comp[u], ind[v]);
}
if (comp[u] == ind[u])
while (true) {
int v = s.top();
s.pop();
active[v] = false;
comp[v] = ind[u];
if (v == u) break;
}
}
int p[MAXN], toval[MAXN], tN;
int found(int x) {
int ind = lower_bound(toval, toval + tN, x) - toval;
if (ind == tN || toval[ind] != x)
return -1;
else
return ind;
}
inline int tr(int x) { return 2 * x; }
inline int fs(int x) { return 2 * x + 1; }
inline void add(int u, int v) {
l[eN] = u;
r[eN] = v;
G[u].push_back(eN);
eN++;
}
vector<int> H[MAXN];
bool vis[MAXN];
int ord[MAXN], oN, in[MAXN];
void topol(int u) {
vis[u] = true;
for (int i = 0; i < H[u].size(); i++) {
int v = H[u][i];
if (!vis[v]) topol(v);
}
ord[oN++] = u;
}
int ans[MAXN], id[MAXN];
pair<int, int> tmp[MAXN];
int main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
memset(ind, -1, sizeof ind);
int n, a, b;
cin >> n >> a >> b;
for (int i = 0; i < n; i++) {
cin >> p[i];
toval[tN++] = p[i];
}
sort(toval, toval + tN);
tN = unique(toval, toval + tN) - toval;
for (int i = 0; i < n; i++) {
int x = found(a - toval[i]), y = found(b - toval[i]);
if (x != -1) {
add(tr(i), tr(x));
add(fs(i), fs(x));
} else
add(tr(i), fs(i));
if (y != -1) {
add(fs(i), fs(y));
add(tr(i), tr(y));
} else
add(fs(i), tr(i));
}
for (int i = 0; i < tN * 2; i++)
if (ind[i] == -1 && in[i] == 0) tarjan(i);
for (int i = 0; i < tN; i++)
if (comp[fs(i)] == comp[tr(i)]) {
cout << "NO";
return 0;
}
for (int i = 0; i < eN; i++) {
int u = comp[l[i]], v = comp[r[i]];
if (u != v) {
H[u].push_back(v);
in[v]++;
}
}
memset(ans, -1, sizeof ans);
for (int i = 0; i < tN * 2; i++)
if (!vis[i] && in[i] == 0) topol(i);
for (int i = 0; i < tN * 2; i++) id[ord[i]] = i;
for (int i = 0; i < tN * 2; i++) tmp[i] = pair<int, int>(ord[comp[i]], i);
sort(tmp, tmp + tN * 2);
for (int i = 0; i < tN * 2; i++) {
int u = tmp[i].second;
if (ans[u / 2] == -1) ans[u / 2] = u % 2;
}
cout << "YES\n";
for (int i = 0; i < n; i++) cout << ans[found(p[i])] << " ";
}
| 8 | CPP |
"""def power(x, y):
res = 1
x=x
while (y > 0):
if ((y & 1) == 1) :
res = (res * x)
y = y >> 1
x = (x * x)
return res"""
"""def fact(n):
if(n==0):
return 1
if(n==1):
return 1
return fact(n-1)+fact(n-2)"""
"""def sieveOfEratothenes(n):
dp = [True]*(n+1)
dp[1]=True
p=2
while(p*p<n+1):
if(dp[p]==True):
for i in range(p*p, n+1, p):
dp[i]=False
p+=1
return dp"""
import math
#import combinations
t=int(input())
for _ in range(t):
#n=int(input())
#s=input()
#arr=list(map(int,input().strip()))
#b=list(map(int,input().split()))
a,b = map(int,input().split())
print(math.ceil((a*b)/2))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
void maximize(T &lhs, U rhs) {
if (lhs < rhs) lhs = rhs;
}
template <class T, class U>
void minimize(T &lhs, U rhs) {
if (lhs > rhs) lhs = rhs;
}
template <class T, class U>
void maximize(T &lhs, const initializer_list<U> &rhs) {
U max_rhs = max(rhs);
if (lhs < max_rhs) {
lhs = max_rhs;
}
}
template <class T, class U>
void minimize(T &lhs, const initializer_list<U> &rhs) {
U min_rhs = min(rhs);
if (lhs > min_rhs) {
lhs = min_rhs;
}
}
template <class T>
T tabs(T x) {
return (x < (T)0 ? -x : x);
}
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
mt19937 generator(chrono::system_clock::now().time_since_epoch().count());
template <class T = int>
T __rand(T range = numeric_limits<T>::max()) {
return (T)(generator() % range);
}
namespace Task {
class SuffixArray {
public:
static const int lg = 13;
vector<vector<int> > suff;
vector<int> lcp;
vector<int> ranks;
vector<pair<pair<int, int>, int> > curRanks;
int lstr = -1;
public:
SuffixArray(const string &str) : lstr((int)((str).size())) {
suff.resize(lg + 2, vector<int>(lstr + 2, 0));
curRanks.resize(lstr, {{0, 0}, 0});
ranks.resize(lstr, 0);
lcp.resize(lstr, 0);
for (int i = 0; i < lstr; ++i) {
suff[0][i] = str[i] - 'a';
}
for (int j = 1; j <= lg; ++j) {
for (int i = 0; i < lstr; ++i) {
curRanks[i] = {
{suff[j - 1][i], (i + ((1LL) << (j - 1)) < lstr
? suff[j - 1][i + ((1LL) << (j - 1))]
: -1)},
i};
}
sort(curRanks.begin(), curRanks.end());
for (int i = 0; i < lstr; ++i) {
suff[j][curRanks[i].second] =
(i and curRanks[i].first == curRanks[i - 1].first
? suff[j][curRanks[i - 1].second]
: i);
}
}
for (int i = 0; i < lstr; ++i) {
ranks[i] = curRanks[i].second;
}
}
int computeLcp(int rx, int ry) {
if (rx > ry) {
swap(rx, ry);
}
int ret = 0;
for (int j = lg; ~j; --j) {
if (ry + ((1LL) << (j)) - 1 < lstr) {
if (suff[j][rx] == suff[j][ry]) {
ret |= ((1LL) << (j));
rx += ((1LL) << (j));
ry += ((1LL) << (j));
}
}
}
return ret;
}
void buildLcp() {
for (int i = 0; i < lstr - 1; ++i) {
lcp[i] = computeLcp(ranks[i], ranks[i + 1]);
}
}
};
SuffixArray *sa;
void solve() {
string s, t;
cin >> s >> t;
sa = new SuffixArray(s + "#" + t);
sa->buildLcp();
int ans = numeric_limits<int>::max();
auto ok = [&](int i, int j) {
if (i > j) {
swap(i, j);
}
return (0 <= i and i < (int)((s).size()) and (int)((s).size()) < j and
j <= (int)((s).size()) + (int)((t).size()));
};
for (int i = 0; i < sa->lstr - 1; ++i) {
if (i == 0) {
if (sa->lcp[i] > sa->lcp[i + 1]) {
if (ok(sa->ranks[i], sa->ranks[i + 1])) {
minimize(ans, sa->lcp[i + 1] + 1);
}
}
} else if (i == sa->lstr - 2) {
if (sa->lcp[i - 1] < sa->lcp[i]) {
if (ok(sa->ranks[i], sa->ranks[i + 1])) {
minimize(ans, sa->lcp[i - 1] + 1);
}
}
} else if (sa->lcp[i - 1] < sa->lcp[i] and sa->lcp[i] > sa->lcp[i + 1]) {
if (ok(sa->ranks[i], sa->ranks[i + 1])) {
minimize(ans, 1 + max(sa->lcp[i - 1], sa->lcp[i + 1]));
}
}
}
cout << (ans == numeric_limits<int>::max() ? -1 : ans) << '\n';
}
} // namespace Task
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
Task::solve();
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
static inline void canhazfast() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
template <typename T>
T gcd(T a, T b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T extgcd(T a, T b, T &x, T &y) {
T x0 = 1, y0 = 0, x1 = 0, y1 = 1;
while (b) {
T q = a / b;
a %= b;
swap(a, b);
x0 -= q * x1;
swap(x0, x1);
y0 -= q * y1;
swap(y0, y1);
}
x = x0;
y = y0;
return a;
}
static inline int ctz(unsigned x) { return __builtin_ctz(x); }
static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); }
static inline int clz(unsigned x) { return __builtin_clz(x); }
static inline int clzll(unsigned long long x) { return __builtin_clzll(x); }
static inline int popcnt(unsigned x) { return __builtin_popcount(x); }
static inline int popcntll(unsigned long long x) {
return __builtin_popcountll(x);
}
static inline int bsr(unsigned x) { return 31 ^ clz(x); }
static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); }
int main() {
canhazfast();
static int a[300016];
static int dp[5016][5016];
int n, k;
int q, r, m, ans;
cin >> n >> k;
q = n / k;
r = n % k;
m = k - r;
for (int i = 1; i <= n; ++i) cin >> a[i];
sort(a + 1, a + 1 + n);
for (int i = 0; i <= r; ++i) {
for (int j = 0; j <= m; ++j) {
int p = q * (i + j) + i;
dp[i][j] = i || j ? INT_MIN : 0;
if (i) dp[i][j] = max(dp[i][j], dp[i - 1][j] + a[p - q] - a[p - q - 1]);
if (j) dp[i][j] = max(dp[i][j], dp[i][j - 1] + a[p - q + 1] - a[p - q]);
}
}
ans = a[n] - dp[r][m];
cout << ans;
return 0;
}
| 10 | CPP |
a,b=[int(input()) for i in range(2)]
print("GREATER" if a>b else "LESS" if a<b else "EQUAL") | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
map<long long, vector<pair<long long, long long> > > row, col;
map<long long, long long> rr, cc;
bool INSECT(long long x1, long long y1, long long x2, long long y2) {
return x1 <= x2 && x2 <= y1 || x1 <= y2 && y2 <= y1 || x2 <= x1 && x1 <= y2 ||
x2 <= y1 && y1 <= y2;
}
long long SEG(vector<pair<long long, long long> > &x) {
long long a(x[0].first), b(x[0].second), tot(0);
vector<pair<long long, long long> > tmp;
for (long long i = 0; i < x.size(); ++i) {
if (INSECT(a, b, x[i].first, x[i].second)) {
a = min(a, x[i].first), b = max(b, x[i].second);
} else {
tmp.push_back(make_pair((a), (b)));
tot += b - a;
a = x[i].first, b = x[i].second;
}
}
tmp.push_back(make_pair((a), (b)));
tot += b - a;
x.swap(tmp);
return tot;
}
void UNIQ(map<long long, vector<pair<long long, long long> > > &xx,
map<long long, long long> &yy, const long long n) {
for (map<long long, vector<pair<long long, long long> > >::iterator it =
xx.begin();
it != xx.end(); ++it) {
sort(it->second.begin(), it->second.end());
it->second.erase(unique(it->second.begin(), it->second.end()),
it->second.end());
yy[it->first] = n - SEG(it->second);
}
}
void GAO(bool foo, map<long long, vector<pair<long long, long long> > > xx,
const long long n, long long i, long long j) {
long long st(0), ed, sum(0);
vector<pair<long long, long long> > tmp(xx[i]);
if (tmp.empty()) {
ed = j;
} else {
tmp.push_back(make_pair((0), (0)));
tmp.push_back(make_pair((n), (n)));
sort(tmp.begin(), tmp.end());
for (long long k = 0; k + 1 < tmp.size(); ++k) {
if (sum + tmp[k + 1].first - tmp[k].second >= j) {
ed = tmp[k].second + j - sum;
break;
} else {
sum += tmp[k + 1].first - tmp[k].second;
}
}
}
if (foo)
cout << st << ' ' << i << ' ' << ed << ' ' << i << endl;
else
cout << i << ' ' << st << ' ' << i << ' ' << ed << endl;
}
int main() {
long long i, j, k;
long long m, n;
long long xb, yb, xe, ye;
long long tot(0);
cin >> n >> m >> k;
for (i = 0; i < k; ++i) {
cin >> xb >> yb >> xe >> ye;
if (xb == xe)
col[xb].push_back(make_pair((min(yb, ye)), (max(yb, ye))));
else
row[yb].push_back(make_pair((min(xb, xe)), (max(xb, xe))));
}
UNIQ(row, rr, n);
UNIQ(col, cc, m);
for (map<long long, long long>::iterator it = rr.begin(); it != rr.end();
++it)
tot ^= it->second;
if ((m - 1 - rr.size()) & 1) tot ^= n;
for (map<long long, long long>::iterator it = cc.begin(); it != cc.end();
++it)
tot ^= it->second;
if ((n - 1 - cc.size()) & 1) tot ^= m;
if (tot == 0) {
puts("SECOND");
} else {
puts("FIRST");
i = -1, j = -1;
for (map<long long, long long>::iterator it = rr.begin(); it != rr.end();
++it)
if ((it->second ^ tot) < it->second) {
i = it->first, j = it->second - (it->second ^ tot);
break;
}
if (i == -1 && (n ^ tot) < n) {
for (k = 1; k < m; ++k)
if (!rr.count(k)) {
i = k, j = n - (n ^ tot);
break;
}
}
if (i != -1)
GAO(true, row, n, i, j);
else {
for (map<long long, long long>::iterator it = cc.begin(); it != cc.end();
++it)
if ((it->second ^ tot) < it->second) {
i = it->first, j = it->second - (it->second ^ tot);
break;
}
if (i == -1 && (m ^ tot) < m) {
for (k = 1; k < n; ++k)
if (!cc.count(k)) {
i = k, j = m - (m ^ tot);
break;
}
}
GAO(false, col, m, i, j);
}
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
const long long int mod = 1e9 + 7;
using namespace std;
int main() {
int n;
string s;
scanf("%d", &n);
;
cin >> s;
int c = 0;
map<char, int> mp;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
mp[s[j]]++;
if (mp['U'] == mp['D'] && mp['R'] == mp['L']) c++;
}
mp.clear();
}
printf("%d", c);
;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 2e18 + 5;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long m, n, res;
cin >> m >> n;
vector<int> a;
vector<int> r(n);
for (int i = (0); i < (n); i++) {
cout << 1 << endl;
cin >> res;
if (res == 0) exit(0);
if (res == -1)
r[i] = 0;
else
r[i] = 1;
}
int lo = 0, hi = m, c = -1;
while (lo <= hi) {
c++;
int mid = (lo + hi) / 2;
cout << mid << endl;
cin >> res;
if (res == 0) exit(0);
if (res == 1) {
if (r[c % n] == 1)
lo = mid + 1;
else
hi = mid - 1;
} else if (res == -1) {
if (r[c % n] == 0)
lo = mid + 1;
else
hi = mid - 1;
}
}
return 0;
}
| 10 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
int a[1200], b[1200];
queue<int> q;
int i;
for (i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (i = 1; i <= m; i++) {
scanf("%d", &b[i]);
q.push(b[i]);
}
int sum = 0;
int t;
for (i = 1; i <= n; i++) {
t = q.front();
if (t >= a[i]) {
sum++;
q.pop();
} else {
continue;
}
if (q.empty()) break;
}
printf("%d\n", sum);
return 0;
}
| 7 | CPP |
#include <stdio.h>
int main(void) {
int i, j, k, sum = 0, a;
for(i = 0; i < 10; ++i) {
scanf("%d", &a);
sum += a;
}
printf("%d\n", sum);
return 0;
}
| 0 | CPP |
S = input()
print(min([len(max(S.split(s), key=lambda x: len(x))) for s in set(S)])) | 0 | PYTHON3 |
x=int(input())
for i in range(x):
x,y=map(int,input().split())
c=abs(x-y)
a=c//5
b=(c-(a*5))//2
d=(c-(a*5))%2
print(a+b+d) | 7 | PYTHON3 |
#include<deque>
#include<stdint.h>
#include<set>
#include<stack>
#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<iomanip>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
int n,m,d,i,j,col[2010];
char ss[4]={'R','Y','B','G'};
int main()
{
cin>>n>>m>>d;
for (i=d;i<=2000;i++)
{
col[i]=1-col[i-d];
}
for (i=1;i<=n;i++)
{
for (j=1;j<=m;j++)
{
cout<<ss[col[i+j]*2+col[i-j+1000]];
}
cout<<endl;
}
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
long long dp[N];
int main() {
int n;
int m;
scanf("%d%d", &n, &m);
dp[0] = 1;
for (int i = 1; i < n; i++) {
int coef = min(i, n - i + 1);
for (int j = coef; j < n; j++) {
dp[j] += dp[j - coef];
dp[j] %= m;
}
}
long long ans = 0;
for (int i = 0; i < n; i++) {
ans += dp[i] * (n - i) % m;
ans %= m;
}
cout << ans << endl;
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
bool is(string s) {
int len = s.size();
int lp = 0, rp = len - 1;
while (lp <= rp) {
if (s[lp] == s[rp]) {
lp++;
rp--;
} else {
return false;
}
}
return true;
}
int main() {
string s = "";
cin >> s;
int k, count = 0;
scanf("%d", &k);
int n = s.size();
if (n % k != 0) {
printf("NO\n");
return 0;
}
int len = n / k;
for (int i = 0; i < n; i += len) {
string ans = "";
for (int j = 0; j < len; j++) {
ans += s[i + j];
}
if (is(ans)) {
count++;
}
}
if (count == k) {
printf("YES\n");
} else {
printf("NO\n");
}
return 0;
}
| 7 | CPP |
m,n=map(int,input().split())
a=list(map(int,input().split()))
c=list(map(int,input().split()))
dic2={}
dic={}
for i in range(m):
dic2[i+1]=[c[i],a[i]]
#dic.sort(key=lambda x:x[0])
ss = sorted(dic2.items(), key=lambda kv: kv[1][0])
for i in ss:
dic[i[0]]=i[1]
items=list(dic.keys())
shoro=0
ans=""
for i in range(n):
sum=0
t,d=map(int,input().split())
x=min(dic[t][1],d)
dic[t][1]-=x
sum+=x*dic[t][0]
d-=x
if d>0:
for j in range(shoro,len(items)):
if dic[items[j]][1]==0:
shoro+=1
continue
x=min(dic[items[j]][1],d)
dic[items[j]][1]-=x
sum+=x*dic[items[j]][0]
d-=x
if d==0:
break
if d!=0:
sum=0
ans+=str(sum)+"\n"
print(ans)
| 8 | PYTHON3 |
n,k,m = list(map(int, input().split()))
t = sorted(map(int, input().split()))
st = sum(t)
res = 0
for x in range(min(m//st, n)+1):
rem = m-x*st
r = x*(k+1)
# for i in range(k):
# y = min(rem//t[i], n-x)
# rem -= t[i]*y
# m += y
for i in range(k):
for _ in range(n-x):
if rem >= t[i]:
rem -= t[i]
r += 1
res = max(res, r)
print(res) | 8 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin>>N;
int a[2][N];
for(int i=0;i<2;i++){
for(int j=0;j<N;j++){
cin>>a[i][j];
}
}
int res;
int temp;
int s=a[0][0];
for(int j=0;j<N;j++)s+=a[1][j];
res=temp=s;
for(int j=0;j<N-1;j++){
temp+=a[0][j+1]-a[1][j];
res=max(res,temp);
}
cout<<res;
return 0;
} | 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a[100002], x;
unsigned int n, k;
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (unsigned int i = 1; i <= n; i++) {
if (i * (i + 1) / 2 >= k) {
x = i * (i + 1) / 2 - k;
cout << a[i - x];
break;
}
}
return 0;
}
| 8 | CPP |
X = int(input())
print(int(X/500)*1000+int(X%500/5)*5) | 0 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long int n;
cin >> n;
long long int a[n];
long long int x = 0;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
x ^= a[i];
}
if (x != 0 && n % 2 == 0)
cout << "NO"
<< "\n";
else {
cout << "YES"
<< "\n";
vector<pair<pair<long long int, long long int>, long long int>> vp;
for (long long int i = 0; i < n - 2; i += 2) {
vp.push_back({{i + 1, i + 2}, i + 3});
}
if (n % 2 == 0) {
for (long long int i = n - 4; i >= 2; i -= 2) {
vp.push_back({{i + 1, i}, i - 1});
}
} else {
for (long long int i = n - 3; i >= 2; i -= 2) {
vp.push_back({{i + 1, i}, i - 1});
}
}
cout << vp.size() << "\n";
for (auto x : vp) {
cout << x.first.first << " " << x.first.second << " " << x.second << "\n";
}
}
return 0;
}
| 10 | CPP |
"""
Sometimes some words like "localization" or "internationalization" are so long that writing them many times in one text is quite tiresome.
Let's consider a word too long, if its length is strictly more than 10 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: we write down the first and the last letter of a word and between them we write the number of letters between the first and the last letters.
That number is in decimal system and doesn't contain any leading zeroes.
Thus, "localization" will be spelt as "l10n", and "internationalization» will be spelt as "i18n".
You are suggested to automatize the process of changing the words with abbreviations.
At that all too long words should be replaced by the abbreviation and the words that are not too long should not undergo any changes.
"""
banyak_kata = int(input())
if (banyak_kata>0 and banyak_kata <101):
for i in range(banyak_kata):
kata = input()
if (len(kata)>10):
print(kata[0]+str(len(kata)-2)+kata[len(kata)-1])
else:
print(kata)
| 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
const long long INF=0x3f3f3f3f3f3f3f3fLL;
const int M=100005;
long long dp[M];
int P[M],Q[M];
long long mi=INF;
int n,e,t,l,r;
int main(){
scanf("%d%d%d",&n,&e,&t);
for(int i=1;i<=n;++i)
scanf("%d",P+i);
Q[r++]=0;
for(int i=0,j=1;j<=n;++j){
while(t<=2*(P[j]-P[i+1])){
mi=min(mi,dp[i]-P[i]-2*P[i+1]);
if(l<r && Q[l]<=i)
++l;
++i;
}
dp[j]=3LL*P[j]+mi;
if(l<r)
dp[j]=min(dp[j],dp[Q[l]]+P[j]-P[Q[l]]+t);
while(l<r && dp[j]-P[j]<=dp[Q[r-1]]-P[Q[r-1]])
--r;
Q[r++]=j;
}
printf("%lld\n",dp[n]+e-P[n]);
return 0;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define EPS (1e-8)
typedef complex<double> P;
typedef P Vec;
struct Line {
P p;
Vec v;
Line() {}
Line(P p, Vec v) : p(p), v(v) {}
P getPoint(double t) {
return p + v * t;
}
};
struct Circle {
P p;
double r;
Circle() {}
Circle(P p, double r) : p(p), r(r) {}
};
double dot(Vec a, Vec b) { return a.real() * b.real() + a.imag() * b.imag(); }
double cross(Vec a, Vec b) { return a.real() * b.imag() - a.imag() * b.real(); }
bool isInCir(Circle &c, P &p) { return norm(c.p - p) <= c.r*c.r + EPS; }
bool isInSeg(Line &s, P &p) {
Vec a = s.v, b = p - s.p;
double l1 = abs(a), l2 = abs(b);
return dot(a, b) >= l1*l2 - EPS && dot(a, b) <= l1*l2 + EPS && l1 >= l2 - EPS;
}
bool isInTri(P &a, P &b, P &c, P &d) {
P p[4];
p[0] = a; p[1] = b; p[2] = c; p[3] = d;
int cnt = 0;
REP(i, 3) {
Vec v = p[3] - p[i];
Line s(p[i], p[(i + 1) % 3] - p[i]);
if (isInSeg(s, p[3])) return true;
if (cross(s.v, v) < -EPS) cnt++;
}
return (cnt == 0 || cnt == 3);
}
double distanceSP(Line &s, P &p) {
Vec a = p - s.p, b = p - s.getPoint(1.0);
if (dot(s.v, a) < -EPS) return abs(a);
if (dot(s.v, a) > dot(s.v, s.v) + EPS) return abs(b);
return abs(cross(s.v, a)) / abs(s.v);
}
int main() {
double x1, y1, x2, y2, x3, y3, x4, y4, r;
while (cin >> x1 >> y1, x1 || y1) {
cin >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> r;
P p[4] = {P(x1, y1), P(x2, y2), P(x3, y3), P(x4, y4)};
Line seg[3] = {Line(p[0], p[1] - p[0]), Line(p[1], p[2] - p[1]), Line(p[2], p[0] - p[2])};
Circle c(p[3], r);
double m = min(distanceSP(seg[0], c.p), min(distanceSP(seg[1], c.p), distanceSP(seg[2], c.p)));
if (isInCir(c, p[0]) && isInCir(c, p[1]) && isInCir(c, p[2]))
puts("b");
else if (isInTri(p[0], p[1], p[2], p[3])) {
if (c.r <= m + EPS) puts("a");
else puts("c");
}
else if (c.r < m - EPS) puts("d");
else puts("c");
}
return 0;
} | 0 | CPP |
x=int(input())
y=int(input())
z=int(input())
res=[]
ma=0
n=x+y*z
l=x*(y+z)
s=x*y*z
p=(x+y)*z
d=x+y+z
res.append(n)
res.append(l)
res.append(s)
res.append(p)
res.append(d)
print(max(res)) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
Point() {}
Point(const int &_x, const int &_y) { x = _x, y = _y; }
};
Point operator-(const Point &a, const Point &b) {
return Point(a.x - b.x, a.y - b.y);
}
long long operator*(const Point &a, const Point &b) {
return 1ll * a.x * b.y - 1ll * a.y * b.x;
}
bool operator<(const Point &a, const Point &b) {
return a.x == b.x ? a.y < b.y : a.x < b.x;
}
set<Point> S;
void update(Point x) {
set<Point>::iterator nxt = S.lower_bound(x), pre = prev(nxt);
if (nxt != S.end() && (*pre - x) * (*nxt - x) >= 0) return;
if (nxt != S.end())
while (next(nxt) != S.end() && (x - *nxt) * (*next(nxt) - *nxt) >= 0)
++nxt, S.erase(prev(nxt));
while (pre != S.begin() && (*prev(pre) - *pre) * (x - *pre) >= 0)
--pre, S.erase(next(pre));
S.insert(x);
}
long long m;
int n;
int query(int x, int y) {
set<Point>::iterator it = S.lower_bound(Point((y + x - 1) / x, 0));
if (it == S.end()) return 0;
Point delta = *it - *prev(it);
return 1ll * delta.y * (y - 1ll * x * prev(it)->x) +
1ll * x * prev(it)->y * delta.x <=
m * delta.x;
}
int main() {
scanf("%d%lld", &n, &m);
int lstans = 0;
S.insert(Point(0, 0));
for (int i = 1, opt, x, y; i <= n; i++) {
scanf("%d%d%d", &opt, &x, &y);
x = (x + lstans) % 1000000 + 1, y = (y + lstans) % 1000000 + 1;
if (opt == 1)
update(Point(x, y));
else {
int ans = query(x, y);
if (ans)
lstans = i, puts("YES");
else
puts("NO");
}
}
}
| 12 | CPP |
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<map>
#include<fstream>
#include<queue>
#include<time.h>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const int MAX_N = 300 + 5;
int a[51];
void init() {
for (int i = 0; i <= 50; i++)a[i] = 1e9;
for (int i = 0; i < (1 << 9); i++) {
int s = 0, p = 0;
for (int j = 0; j < 9; j++) {
if ((i & (1 << j)) == 0)continue;
s += j + 1;
p = p * 10 + j + 1;
}
if (s <= 50)a[s] = min(a[s], p);
}
}
int main() {
int T;
cin >> T;
init();
while (T--) {
int x;
cin >> x;
if (a[x] == 1e9)cout << -1 << endl;
else cout << a[x]<<endl;
}
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int f;
cin >> f;
cout << ((f - 30) >> 1) << endl;
return 0;
}
| 0 | CPP |
import math
n=int(input())
a=list(map(int,input().split()))
ans = a[n-1]
for i in range(n-2,-1,-1):
a[i] = min(a[i],a[i+1]-1)
a[i] = max(0,a[i])
ans+=a[i]
print(ans) | 8 | PYTHON3 |
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")
# ------------------------------
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def RLL(): return list(map(int, sys.stdin.readline().rstrip().split()))
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
def toord(c): return ord(c)-ord('a')
def lcm(a, b): return a*b//lcm(a, b)
mod = 998244353
INF = float('inf')
from math import factorial, sqrt, ceil, floor, gcd
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush
# ------------------------------
# f = open('./input.txt')
# sys.stdin = f
from bisect import bisect_left
def main():
n = N()
arr = RLL()
q = N()
rec = []
recp = []
alp = [-1]*(n+1)
for i in range(q):
now = RLL()
if now[0]==1:
p, x = now[1: ]
arr[p-1] = x
alp[p-1] = i
else:
x = now[1]
while rec and x>=rec[-1]:
rec.pop()
recp.pop()
rec.append(x)
recp.append(i)
if len(rec)==0:
print(*arr)
exit()
for i in range(n):
p = bisect_left(recp, alp[i])
if p<len(rec):
arr[i] = max(arr[i], rec[p])
print(*arr)
if __name__ == "__main__":
main()
| 8 | PYTHON3 |
"""
NTC here
"""
from sys import stdin, setrecursionlimit
setrecursionlimit(10**7)
def iin(): return int(stdin.readline())
def lin(): return list(map(int, stdin.readline().split()))
# range = xrange
# input = raw_input
MAX_INT=99999999
def p3(a):
ans=[]
while a:
ans.append(a%3)
a//=3
return ans
def main():
t=iin()
while t:
t-=1
n=iin()
pw=p3(n)+[0,0]
# print(pw)
l=len(pw)
ch=1
for i in range(l-3,-1,-1):
if pw[i]>1 :
if ch:
pw[i]=0
pw[i+1]+=1
ch=0
else:
pw[i]=0
if ch==0:
pw[i]=0
for i in range(l-1):
if pw[i]>1:pw[i]=0;pw[i+1]+=1
ans=0
pw=pw[::-1]
for i in pw:
ans=ans*3+i
print(ans)
main()
# try:
# main()
# except Exception as e: print(e) | 9 | PYTHON3 |
#pragma GCC optimize("trapv")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
const int maxn = 303;
char get(int a, int b, int s, int l, int i) {
if(i < l + (l/s)) return i%(s+1) == s ? 'B' : 'A';
if(i >= a + b - (a-l-1)*(s+1)) {
i -= a + b - (a-l-1)*(s+1);
return i%(s+1) == 0 ? 'A' : 'B';
}
i -= l + (l/s);
return i == 0 ? 'A' : 'B';
}
void solve() {
int A, B, C, D, rev = 0;
cin >> A >> B >> C >> D;
if(A < B) swap(A, B), rev = 1;
int S = (A+B)/(B+1);
int l = 0;
for(int i = 1<<29; i>>=1;) {
l += i;
if(l > A || ((l/S) + (A-l-1)*1ll*S < B+(l==A))) l-= i;
}
int cnt = 0;
while(((l/S) + (A-l-1)*1ll*S >= B+(l==A))) ++l <= A, assert(++cnt < 2);
//cout << l << '\n';
C--, D--;
while(C <= D) {
if(!rev) cout << get(A, B, S, l, C);
else cout << char(get(A, B, S, l, A+B-C-1)^'A'^'B');
C++;
}
cout << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while(t--) solve();
}
//0 1 0 1 0 1 0 1 2 2
| 0 | CPP |
n = int(input())
ans = -1
left = 0
for _ in range(n):
l = list(map(int,input().rstrip().split()))
a = l[0]
b = l[1]
left -=a
left+=b
ans = max(ans,left)
print(ans)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long x, y, m;
long long total = 0;
cin >> x >> y >> m;
if (x >= m || y >= m) {
cout << 0 << endl;
return 0;
}
if (x <= 0 && y <= 0) {
cout << -1 << endl;
return 0;
}
if (x > y) swap(x, y);
if (x <= 0) {
total = -x / y;
x = x + total * y;
}
while (y < m) {
x += y;
if (x > y) swap(x, y);
total++;
}
cout << total << endl;
return 0;
}
| 7 | CPP |
import os
import sys
from io import BytesIO, IOBase
import math
from decimal import *
getcontext().prec = 30
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")
def binary_search(data, val, le):
test = le
if val >= data[-1]:
return [test - 1, test - 1]
elif val <= data[0]:
return [0, 0]
high_index = test - 1
low_index = 0
while high_index > low_index:
index = (high_index + low_index) // 2
sub = data[index]
if data[low_index] == val:
return [low_index, low_index]
elif sub == val:
return [index, index]
elif data[high_index] == val:
return [high_index, high_index]
elif sub > val:
if high_index == index:
return sorted([high_index, low_index])
high_index = index
else:
if low_index == index:
return sorted([high_index, low_index])
low_index = index
return sorted([high_index, low_index])
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
for _ in range(int(input())):
a, b, c = map(int, input().split(" "))
# required sticks = c-1 + c*b
# one move = (x-1)
print(int(c + math.ceil(Decimal((c * b + c-1) / Decimal(a - 1)))))
| 7 | PYTHON3 |
year = str(int(input()) + 1)
while(len(set(year)) != 4):
year =str(int(year) + 1)
print(year)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, n, ans = 0;
cin >> n;
vector<int> b2(1000);
vector<int> bad(1000);
vector<int> b1(1000);
for (int i = 0; i < 1000; i++) {
b2[i] = 0;
bad[i] = 0;
b1[i] = 0;
}
for (int i = 0; i < n; i++) {
cin >> a >> b;
b2[a - 1]++;
if (a == b) bad[a - 1]++;
b1[b - 1]++;
}
for (int i = 0; i < 1000; i++) {
if (b1[i] == 0) ans += b2[i];
if (b1[i] == 1 && bad[i] == 1) ans++;
}
cout << ans << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> g[200100];
int p[200100];
vector<int> cmp[200100];
int cmpNo, root[200100], finalroot;
bool vis[200100], safe[200100], inCycle[200100];
bool cy[200100];
void dfs(int u, int par) {
inCycle[u] = true;
cmp[cmpNo].push_back(u);
vis[u] = true;
if (u == p[u]) safe[cmpNo] = true, root[cmpNo] = u, finalroot = u;
for (int v : g[u])
if (!vis[v])
dfs(v, u);
else if (inCycle[v] && par != v) {
cy[u] = true;
cy[v] = true;
}
inCycle[u] = false;
}
int main() {
finalroot = 0;
int n;
scanf("%d", &n);
for (long long i = 1; i < n + 1; i++) scanf("%d", &p[i]);
for (long long i = 1; i < n + 1; i++) {
if (i == p[i]) continue;
g[i].push_back(p[i]), g[p[i]].push_back(i);
}
for (long long i = 1; i < n + 1; i++)
if (p[p[i]] == i) cy[i] = cy[p[i]] = true;
for (long long i = 1; i < n + 1; i++)
if (!vis[i]) {
dfs(i, 0);
cmpNo++;
}
int ans = 0;
if (finalroot == 0) {
bool found = false;
for (long long i = 0; i < cmpNo; i++) {
for (int j : cmp[i])
if (cy[j]) {
finalroot = j;
safe[i] = true;
ans++;
p[j] = j;
root[i] = j;
found = true;
break;
}
if (found) break;
}
}
for (long long i = 0; i < cmpNo; i++) {
if (!safe[i]) {
int x;
for (int j : cmp[i])
if (cy[j]) x = j;
p[x] = finalroot;
ans++;
} else if (root[i] != finalroot) {
p[root[i]] = finalroot;
ans++;
}
}
printf("%d\n", ans);
for (long long i = 1; i < n + 1; i++) printf("%d ", p[i]);
printf("\n");
return 0;
}
| 10 | CPP |
# coding: utf-8
n = int(input())
name = []
for i in range(n):
name.append(sorted(input().split()))
p = [int(i) for i in input().split()]
pre = name[p[0]-1][0]
for index in p[1:]:
if name[index-1][0]>pre:
pre = name[index-1][0]
elif name[index-1][1]>pre:
pre = name[index-1][1]
else:
print('NO')
break
else:
print('YES')
| 9 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 101000;
int n, K, head[N], etot, dfn[N], dfp[N], plca[N][20], dep[N], tim, sum;
struct edge {
int v, next;
} g[N << 1];
set<int> st;
void add_edge(int u, int v) {
g[etot] = (edge){v, head[u]};
head[u] = etot++;
}
void dfs(int u, int fa, int deep) {
dep[u] = deep;
plca[u][0] = fa;
dfn[u] = ++tim;
dfp[tim] = u;
for (int i = 1; i < 20; i++)
if (plca[u][i - 1] != -1) plca[u][i] = plca[plca[u][i - 1]][i - 1];
for (int i = head[u]; i != -1; i = g[i].next) {
int v = g[i].v;
if (v == fa) continue;
dfs(v, u, deep + 1);
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a, b);
int delta = dep[a] - dep[b];
for (int i = 19; i >= 0; i--)
if (delta >> i & 1) a = plca[a][i];
if (a != b) {
for (int i = 19; i >= 0; i--)
if (plca[a][i] != plca[b][i]) a = plca[a][i], b = plca[b][i];
a = plca[a][0];
}
return a;
}
int dis(int a, int b) { return dep[a] + dep[b] - dep[lca(a, b)] * 2; }
int calc(int u) {
if (st.size() == 0) return 0;
int prev, next;
set<int>::iterator it;
it = st.lower_bound(dfn[u]);
next = dfp[it == st.end() ? *st.begin() : *it];
prev = dfp[it == st.begin() ? *st.rbegin() : *--it];
return dis(u, prev) + dis(u, next) - dis(prev, next);
}
int main() {
cin >> n >> K;
memset(head, -1, sizeof(head));
memset(plca, -1, sizeof(plca));
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
add_edge(a, b);
add_edge(b, a);
}
dfs(1, -1, 0);
int ans = 0, j = 1, d;
for (int i = 1; i <= n; i++) {
while (j <= n && ((d = calc(j)) + sum) / 2 + 1 <= K)
sum += d, st.insert(dfn[j++]);
ans = max(ans, j - i);
st.erase(dfn[i]);
sum -= calc(i);
}
cout << ans << endl;
return 0;
}
| 10 | CPP |
import sys
input=sys.stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int, input().split()))
ans=[]
for i in range(0, n, 2):
if a[i]==a[i+1]: ans.extend([a[i], a[i]])
else: ans.append(0)
print(len(ans))
print(*ans) | 7 | PYTHON3 |
# URL: http://codeforces.com/contest/546/problem/A
# PROBLEM RESTATEMENT
# Consider the sum s = k + 2k + 3k + ... wk (given k, w, and int n).
# What is s - n?
# APPROACH
# (1) Factor out k => k * (1 + 2 + 3 + ... + w)
# (2) The sum of an arithmetic series from 1 to n is n * (n + 1) / 2
# (3) Compute k * w * (w + 1) / 2 - n.
def solve(k: int, n: int, w: int) -> int:
cost = int(k * w * (w + 1) / 2)
if cost - n > 0:
return cost - n
return 0
k, n, w = list(map(int, input().split()))
print(solve(k, n, w))
| 7 | PYTHON3 |
# your code goes here
string = input()
unique = []
all = "}"
for x in string[1::3]:
if x not in unique:
unique.append(x)
if string[1] in all:
print(0)
else:
print(len(unique))
| 7 | PYTHON3 |
n=input()
s=input()
a=0
d=0
i=0
for x in s:
if s[i]=="A":
a+=1
i+=1
else:
d+=1
i+=1
if a>d:
print("Anton")
elif d>a:
print("Danik")
elif d==a:
print("Friendship") | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4;
int n;
int a[N], aa[N], b[N], bb[N];
struct node {
int x, y, xx, yy;
node(int X = 0, int Y = 0, int XX = 0, int YY = 0) {
x = X, y = Y, xx = XX, yy = YY;
}
};
vector<node> ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
aa[i] = x;
a[x] = i;
}
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
b[x] = i;
bb[i] = x;
}
for (int i = 1; i <= n; i++) {
node u = node(i, b[i], a[i], i);
if (b[i] == i && a[i] == i) continue;
ans.push_back(u);
int y = b[i], x = a[i], yy = bb[i], xx = aa[i];
aa[a[xx] = x] = xx;
bb[b[yy] = y] = yy;
}
printf("%d\n", ans.size());
for (auto u : ans) printf("%d %d %d %d\n", u.x, u.y, u.xx, u.yy);
return 0;
}
| 10 | CPP |
n = int(input())
string = ''
for j in range(1, n+1):
string += str(j)
print(string[n-1])
| 7 | PYTHON3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int W,H,x,y,r;
cin>>W>>H>>x>>y>>r;
if(x-0>=r&&y-0>=r&&W-x>=r&&H-y>=r)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
} | 0 | CPP |
#include <iostream>
#include <vector>
#include <queue>
#include<map>
#include<algorithm>
#include<set>
#include<iomanip>
#define rep(i,n) for(int i = 0;i < n;i++)
#define req(i,n) for(int i = 1; i<=n;i++)
#define rrep(i,n) for(int i = n-1;i >=0;i--)
#define ALL(a) a.begin(),a.end()
#define PI acos(-1)
using namespace std;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
std::fill((T*)array, (T*)(array + N), val);
}
typedef long long int ll;
typedef long double ld;
const int MOD = 1e9 + 7;
int dx[4] = { 0,0,1,-1 }, dy[4] = { 1,-1,0,0 }, sum = 0,h,w;
int main() {
int n, c,sum=0,p; cin >> n >> c;
n++;rep(i, c) cin >> p, sum += p;
if (sum % n != 0) {
sum += n;
}cout << sum / n << endl;
}
| 0 | CPP |
#include <bits/stdc++.h>
using namespace std;
struct node {
int po, pow;
} no[100010];
bool cmp(const node &x, const node &y) { return x.po < y.po; }
int dp[100010];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d%d", &no[i].po, &no[i].pow);
}
sort(no, no + n, cmp);
int ans = 0;
for (int i = 0; i < n; i++) {
int l = 0, r = i - 1, mid, po = -1;
while (l <= r) {
mid = (l + r) / 2;
if (no[mid].po < no[i].po - no[i].pow) {
po = mid;
l = mid + 1;
} else
r = mid - 1;
}
if (po == -1)
dp[i] = 1;
else {
dp[i] = dp[po] + 1;
}
ans = max(ans, dp[i]);
}
printf("%d\n", n - ans);
return 0;
}
| 9 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
long long x[111], y[111];
long long ax, ay, bx, by, a, b, t;
int main() {
cin >> x[0] >> y[0] >> ax >> ay >> bx >> by;
int n = 0;
for (; x[n] < INF && y[n] < INF; ++n) {
x[n + 1] = x[n] * ax + bx, y[n + 1] = y[n] * ay + by;
if (x[n + 1] >= INF || y[n + 1] >= INF) break;
}
cin >> a >> b >> t;
int ans = 0;
for (int l = 0; l <= n; ++l) {
for (int r = l; r <= n; ++r) {
unsigned long long to =
min(abs(a - x[l]) + abs(b - y[l]), abs(a - x[r]) + abs(b - y[r]));
to += abs(x[l] - x[r]) + abs(y[l] - y[r]);
if (to <= t) ans = max(ans, r - l + 1);
}
}
cout << ans << endl;
return 0;
}
| 8 | CPP |
m = int(input())
l = [int(x) for x in input().split(" ")]
p = 1
n = 0
ans = int
for i in range(m):
if(l[i]>0):
l[i] = 1
else:
l[i] = -1
if(i>0):
l[i]*=l[i-1]
if(l[i]==1):
p+=1
else:
n+=1
ans = (m*(m+1))//2
print(p*n," ",ans-(p*n)) | 8 | PYTHON3 |
n,k = map(int,input().split())
arr = list(map(int,input().split()))
lis = []
for i in range(n-1) :
lis.append(arr[i+1]-arr[i])
#print(lis)
lis.sort()
sum = 0
for i in range((n-k)) :
sum+=lis[i]
print(sum) | 9 | PYTHON3 |
def solve(n,ar):
#find alternating subsequence without flip
res = 1
for i in range(n-1):
if ar[i] != ar[i+1]:
res += 1
print(min(res+2,n))
if __name__ == '__main__':
n = int(input())
ar = list(input())
solve(n,ar) | 7 | PYTHON3 |
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
if a[i] >= a[i-1]:
continue
dif = a[i-1] - a[i]
l, r = -1, 40
while r - l > 1:
tmp = (l + r) // 2
if pow(2, tmp) - 1 >= dif:
r = tmp
else:
l = tmp
ans = max(ans, r)
a[i] = a[i-1]
print(ans) | 9 | PYTHON3 |
for w in range(int(input())):
n,k=tuple(map(int,input().split()))
a=list(map(int,input().split()))
b=[0]*n
count=0
t=0
l=[]
for i in range(n):
if(a[i]%2!=0):
count+=1
b[i]=1
if(k>count):
print("NO")
else:
if(k==1):
if(count%2!=0):
print("YES")
print(n)
else:
print("NO")
else:
y=k-1
if((k%2!=0 and count%2!=0) or (k%2==0 and count%2==0)):
print("YES")
for i in range(n):
if(y>0):
if(b[i]==1):
print(i+1,end=" ")
y-=1
else:
break
print(n)
else:
print("NO") | 8 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
long n, a[100000] = {0}, m;
long long h(int a) {
long long s = 0;
while (a > 0) {
s += a % 10;
a = a / 10;
}
return s;
}
int main() {
cin >> n >> m;
if (m == 0 && n > 1) {
cout << "No solution";
return 0;
}
long j = 10 + m - 1;
if (n == 1) {
cout << m;
return 0;
}
cout << j / 2 << j - j / 2;
for (int k = 3; k <= n; ++k) cout << 0;
return 0;
}
| 7 | CPP |
n=int(input())
l=[int(input()) for i in range(n)]
a=sorted(l)
b=a[-1]
c=a[-2]
for i in l:
if i!=b:
print(b)
else:
print(c)
| 0 | PYTHON3 |
Subsets and Splits