id
stringlengths
6
117
description
stringlengths
29
13k
code
stringlengths
9
465k
language
class label
4 classes
test_samples
dict
source
class label
5 classes
512_B. Fox And Jumping_1000
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After app...
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const int N = 305; int c[N], l[N]; int gcd(int a, int b) { while (b) { a %= b; swap(a, b); } return a; } long long rrand() { long long a = rand(); long long b = rand(); return a + (b >> 16); } int main() { in...
2C++
{ "input": [ "5\n10 20 30 40 50\n1 1 1 1 1\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n", "3\n100 99 9900\n1 1 1\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "8\n2 3 5 7 11 13 17 19\n4 8 7 1 5 2 6 3\n", "1\n2\n2\n", "1\n1000000...
2CODEFORCES
512_B. Fox And Jumping_1001
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After app...
def gcd(x,y): while x % y > 0: x, y = y, x % y return y n = int(input()) a, b, c = [int(x) for x in input().split()], [int(x) for x in input().split()], [{} for i in range(n)] def f(i,g): if g == 1: return 0 if i == n: return 100000000000 if g in c[i]: return c[i][g] ...
3Python3
{ "input": [ "5\n10 20 30 40 50\n1 1 1 1 1\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n", "3\n100 99 9900\n1 1 1\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "8\n2 3 5 7 11 13 17 19\n4 8 7 1 5 2 6 3\n", "1\n2\n2\n", "1\n1000000...
2CODEFORCES
512_B. Fox And Jumping_1002
Fox Ciel is playing a game. In this game there is an infinite long tape with cells indexed by integers (positive, negative and zero). At the beginning she is standing at the cell 0. There are also n cards, each card has 2 attributes: length li and cost ci. If she pays ci dollars then she can apply i-th card. After app...
import java.util.List; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.util.InputMismatchException; import java.util.ArrayList; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import ...
4JAVA
{ "input": [ "5\n10 20 30 40 50\n1 1 1 1 1\n", "8\n4264 4921 6321 6984 2316 8432 6120 1026\n4264 4921 6321 6984 2316 8432 6120 1026\n", "3\n100 99 9900\n1 1 1\n", "7\n15015 10010 6006 4290 2730 2310 1\n1 1 1 1 1 1 10\n", "8\n2 3 5 7 11 13 17 19\n4 8 7 1 5 2 6 3\n", "1\n2\n2\n", "1\n1000000...
2CODEFORCES
536_C. Tavas and Pashmaks_1003
Tavas is a cheerleader in the new sports competition named "Pashmaks". <image> This competition consists of two part: swimming and then running. People will immediately start running R meters after they finished swimming exactly S meters. A winner is a such person that nobody else finishes running before him/her (the...
from itertools import imap import sys n = input() def parseints(s, n): r, i, x = [0] * n, 0, 0 for c in imap(ord, s): if c == 32: r[i], i, x = x, i + 1, 0 else: x = x * 10 + c - 48 return r z = parseints(sys.stdin.read().replace('\n', ' '), n * 2) # p = zip(z[::2],...
1Python2
{ "input": [ "3\n1 2\n1 1\n2 1\n", "3\n1 3\n2 2\n3 1\n", "5\n10 50\n10 50\n10 50\n10 50\n10 50\n", "2\n10000 10000\n10000 10000\n", "3\n10000 10000\n10000 10000\n10000 10000\n", "3\n1000 3000\n1500 1500\n3000 1000\n", "43\n75 16\n70 7\n75 16\n54 2\n70 7\n54 2\n70 7\n54 2\n70 7\n54 2\n54 2\...
2CODEFORCES
536_C. Tavas and Pashmaks_1004
Tavas is a cheerleader in the new sports competition named "Pashmaks". <image> This competition consists of two part: swimming and then running. People will immediately start running R meters after they finished swimming exactly S meters. A winner is a such person that nobody else finishes running before him/her (the...
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; struct Point { long long x, y; bool operator<(const Point &p) const { if (x != p.x) return x > p.x; else return y > p.y; } } pt[maxn]; int stk[maxn], stnum; set<pair<long long, long long> > has; bool check(Point a, Point...
2C++
{ "input": [ "3\n1 2\n1 1\n2 1\n", "3\n1 3\n2 2\n3 1\n", "5\n10 50\n10 50\n10 50\n10 50\n10 50\n", "2\n10000 10000\n10000 10000\n", "3\n10000 10000\n10000 10000\n10000 10000\n", "3\n1000 3000\n1500 1500\n3000 1000\n", "43\n75 16\n70 7\n75 16\n54 2\n70 7\n54 2\n70 7\n54 2\n70 7\n54 2\n54 2\...
2CODEFORCES
536_C. Tavas and Pashmaks_1005
Tavas is a cheerleader in the new sports competition named "Pashmaks". <image> This competition consists of two part: swimming and then running. People will immediately start running R meters after they finished swimming exactly S meters. A winner is a such person that nobody else finishes running before him/her (the...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class C { private static Comparator<int[]> cmp = new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { if(a...
4JAVA
{ "input": [ "3\n1 2\n1 1\n2 1\n", "3\n1 3\n2 2\n3 1\n", "5\n10 50\n10 50\n10 50\n10 50\n10 50\n", "2\n10000 10000\n10000 10000\n", "3\n10000 10000\n10000 10000\n10000 10000\n", "3\n1000 3000\n1500 1500\n3000 1000\n", "43\n75 16\n70 7\n75 16\n54 2\n70 7\n54 2\n70 7\n54 2\n70 7\n54 2\n54 2\...
2CODEFORCES
560_D. Equivalent Strings_1006
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2...
from copy import deepcopy s1=raw_input() s2=raw_input() def findlexmin(s1): n=len(s1) sv=deepcopy(n) c=0 while sv%2==0: sv=sv/2 c=c+1 ans=[] for i in range(0,n,sv): ans.append(s1[i:i+sv]) #print ans t=[] for i in range(0,c): t=[] for j in ra...
1Python2
{ "input": [ "aaba\nabaa\n", "aabb\nabab\n", "aabbaaaa\naaaaabab\n", "qgiufelsfhanx\naaaaaaaaaaaaa\n", "abcddd\nbacddd\n", "azzz\nzzaz\n", "zzaa\naazz\n", "yhwepqwyhwepqwyhwepqweahnqtueahnqtueahnqtuyhwepqwyhwepqwyhwepqwyhwepqweahnqtueahnqtuyhwepqweahnqtueahnqtueahnqtueahnqtueahnqtueahn...
2CODEFORCES
560_D. Equivalent Strings_1007
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2...
#include <bits/stdc++.h> using namespace std; string minEquivalent(string s) { if (s.length() % 2) return s; string s1 = minEquivalent(s.substr(0, s.length() / 2)); string s2 = minEquivalent(s.substr(s.length() / 2, s.length() / 2)); if (s1 < s2) return s1 + s2; else return s2 + s1; } int main() { i...
2C++
{ "input": [ "aaba\nabaa\n", "aabb\nabab\n", "aabbaaaa\naaaaabab\n", "qgiufelsfhanx\naaaaaaaaaaaaa\n", "abcddd\nbacddd\n", "azzz\nzzaz\n", "zzaa\naazz\n", "yhwepqwyhwepqwyhwepqweahnqtueahnqtueahnqtuyhwepqwyhwepqwyhwepqwyhwepqweahnqtueahnqtuyhwepqweahnqtueahnqtueahnqtueahnqtueahnqtueahn...
2CODEFORCES
560_D. Equivalent Strings_1008
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2...
def equals(a, b): if (a == b): return True len_a, len_b = len(a), len(b) if (len_a & 1 or len_b & 1): return False if (len_a == 1): return False as1 = a[0:len_a//2] as2 = a[len_a//2:(len_a//2)*2] bs1 = b[:len_b//2] bs2 = b[len_b//2:(len_b//2)*2] return (equals(as1, bs2) and equals...
3Python3
{ "input": [ "aaba\nabaa\n", "aabb\nabab\n", "aabbaaaa\naaaaabab\n", "qgiufelsfhanx\naaaaaaaaaaaaa\n", "abcddd\nbacddd\n", "azzz\nzzaz\n", "zzaa\naazz\n", "yhwepqwyhwepqwyhwepqweahnqtueahnqtueahnqtuyhwepqwyhwepqwyhwepqwyhwepqweahnqtueahnqtuyhwepqweahnqtueahnqtueahnqtueahnqtueahnqtueahn...
2CODEFORCES
560_D. Equivalent Strings_1009
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2...
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); T...
4JAVA
{ "input": [ "aaba\nabaa\n", "aabb\nabab\n", "aabbaaaa\naaaaabab\n", "qgiufelsfhanx\naaaaaaaaaaaaa\n", "abcddd\nbacddd\n", "azzz\nzzaz\n", "zzaa\naazz\n", "yhwepqwyhwepqwyhwepqweahnqtueahnqtueahnqtuyhwepqwyhwepqwyhwepqwyhwepqweahnqtueahnqtuyhwepqweahnqtueahnqtueahnqtueahnqtueahnqtueahn...
2CODEFORCES
586_F. Lizard Era: Beginning_1010
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initially, the attitude of each o...
import copy def fill(k): global di for i in range(3): x = [tasks[k][0], tasks[k][1], tasks[k][2]] x[i] = 0 mi = min(x) x[0],x[1],x[2] = x[0]-mi, x[1]-mi, x[2]-mi for p in di[k+1]: u,v,w = x[0]+p[0],x[1]+p[1],x[2]+p[2] mi = min(u,v,w) u,v,w = u-mi, v-mi, w-mi di[k][(u,v,w)] = 1 def recurse(k): ...
1Python2
{ "input": [ "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n", "2\n1 0 0\n1 1 0\n", "3\n1 0 0\n0 1 0\n0 0 1\n", "3\n7089544 9134148 -5332724\n368810 1638695 7889905\n-3866235 -4257263 5802154\n", "16\n-3253484 -6513322 5617669\n-8870526 9976385 -7313669\n5682511 -1202928 -7057533\n47...
2CODEFORCES
586_F. Lizard Era: Beginning_1011
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initially, the attitude of each o...
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:134217728") using namespace std; const long long MOD = 1000000000 + 7; const long long MAXN = 100000 + 100; const long long MAGIC = 123123123; const double PI = 4 * atan(1.); const double EPS = 1E-7; struct cmp_for_set { bool operator()(const int& a, const int&...
2C++
{ "input": [ "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n", "2\n1 0 0\n1 1 0\n", "3\n1 0 0\n0 1 0\n0 0 1\n", "3\n7089544 9134148 -5332724\n368810 1638695 7889905\n-3866235 -4257263 5802154\n", "16\n-3253484 -6513322 5617669\n-8870526 9976385 -7313669\n5682511 -1202928 -7057533\n47...
2CODEFORCES
586_F. Lizard Era: Beginning_1012
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initially, the attitude of each o...
#!/usr/bin/env python3 n = int(input()) a = [0] * n b = [0] * n c = [0] * n for i in range(n): a[i], b[i], c[i] = map(int, input().split()) middle = { } stack = [ ] result = (-1e10, ()) phase = 1 def search(pos, l, m, w): global result if (pos == n >> 1) if phase == 1 else (pos < n >> 1): if phas...
3Python3
{ "input": [ "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n", "2\n1 0 0\n1 1 0\n", "3\n1 0 0\n0 1 0\n0 0 1\n", "3\n7089544 9134148 -5332724\n368810 1638695 7889905\n-3866235 -4257263 5802154\n", "16\n-3253484 -6513322 5617669\n-8870526 9976385 -7313669\n5682511 -1202928 -7057533\n47...
2CODEFORCES
586_F. Lizard Era: Beginning_1013
In the game Lizard Era: Beginning the protagonist will travel with three companions: Lynn, Meliana and Worrigan. Overall the game has n mandatory quests. To perform each of them, you need to take exactly two companions. The attitude of each of the companions to the hero is an integer. Initially, the attitude of each o...
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class D { ...
4JAVA
{ "input": [ "7\n0 8 9\n5 9 -2\n6 -8 -7\n9 4 5\n-4 -9 9\n-4 5 2\n-6 8 -7\n", "2\n1 0 0\n1 1 0\n", "3\n1 0 0\n0 1 0\n0 0 1\n", "3\n7089544 9134148 -5332724\n368810 1638695 7889905\n-3866235 -4257263 5802154\n", "16\n-3253484 -6513322 5617669\n-8870526 9976385 -7313669\n5682511 -1202928 -7057533\n47...
2CODEFORCES
609_B. The Best Gift_1014
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the nu...
import fileinput from collections import Counter def get_input(): inp = fileinput.input() n, m = map(lambda a: int(a), inp.readline().strip().split()) c = Counter(inp.readline().strip().split()) return n, m, c def main(): n, m, c = get_input() vals = c.values() s = 0 for i in xrange(0...
1Python2
{ "input": [ "7 4\n4 2 3 1 2 4 3\n", "4 3\n2 1 3 1\n", "100 10\n7 4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 2 7 3 9 7 7 2 4 5 2 4 9 5 10 1 10 5 10 4 1 3 4 2 6 9 9 9 10 6 2 5 6 1 8 10 4 10 3 4 10 5 5 4 10 4 5 3 7 10 2 7 3 6 9 6 1 6 5 5 4 6 6 4 4 1 5 1 6 6 6 8 8 6 2 6\n", "3 2\n1 2 2\n", "...
2CODEFORCES
609_B. The Best Gift_1015
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the nu...
#include <bits/stdc++.h> using namespace std; int a[15]; int main() { int n, m; scanf("%d%d", &n, &m); memset(a, 0, sizeof a); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); a[x]++; } int ans = 0; for (int i = 1; i <= m; i++) { for (int j = i + 1; j <= m; j++) { ans = ans + a...
2C++
{ "input": [ "7 4\n4 2 3 1 2 4 3\n", "4 3\n2 1 3 1\n", "100 10\n7 4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 2 7 3 9 7 7 2 4 5 2 4 9 5 10 1 10 5 10 4 1 3 4 2 6 9 9 9 10 6 2 5 6 1 8 10 4 10 3 4 10 5 5 4 10 4 5 3 7 10 2 7 3 6 9 6 1 6 5 5 4 6 6 4 4 1 5 1 6 6 6 8 8 6 2 6\n", "3 2\n1 2 2\n", "...
2CODEFORCES
609_B. The Best Gift_1016
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the nu...
n, m = list(map(int, input().split())) a = list(map(int, input().split())) t = 0 for i in range(m): cnt = a.count(i + 1) t += cnt * (n - cnt) n -= cnt print(t)
3Python3
{ "input": [ "7 4\n4 2 3 1 2 4 3\n", "4 3\n2 1 3 1\n", "100 10\n7 4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 2 7 3 9 7 7 2 4 5 2 4 9 5 10 1 10 5 10 4 1 3 4 2 6 9 9 9 10 6 2 5 6 1 8 10 4 10 3 4 10 5 5 4 10 4 5 3 7 10 2 7 3 6 9 6 1 6 5 5 4 6 6 4 4 1 5 1 6 6 6 8 8 6 2 6\n", "3 2\n1 2 2\n", "...
2CODEFORCES
609_B. The Best Gift_1017
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books on sale from one of m genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the nu...
import java.io.*; import java.util.Scanner; import java.util.Arrays; public class MyCode { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] bookPerGenre = new int[m]; Arrays.fill(bookPerGenre, 0); f...
4JAVA
{ "input": [ "7 4\n4 2 3 1 2 4 3\n", "4 3\n2 1 3 1\n", "100 10\n7 4 5 5 10 10 5 8 5 7 4 5 4 6 8 8 2 6 3 3 10 7 10 8 6 2 7 3 9 7 7 2 4 5 2 4 9 5 10 1 10 5 10 4 1 3 4 2 6 9 9 9 10 6 2 5 6 1 8 10 4 10 3 4 10 5 5 4 10 4 5 3 7 10 2 7 3 6 9 6 1 6 5 5 4 6 6 4 4 1 5 1 6 6 6 8 8 6 2 6\n", "3 2\n1 2 2\n", "...
2CODEFORCES
630_A. Again Twenty Five!_1018
The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. "Do I give such a hard task?" — the HR manager thought. "Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using ...
#for fun #print int(pow(5,input(),100)) cin = input() print 25
1Python2
{ "input": [ "2\n", "7\n", "2000000000000000000\n", "1000000000000000000\n", "987654321012345678\n", "3\n", "316240294332142860\n", "1000001000000000000\n", "1440563777463828705\n", "5\n", "8\n", "178760633629979647\n", "1000001000000010000\n", "10\n", "6\n"...
2CODEFORCES
630_A. Again Twenty Five!_1019
The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. "Do I give such a hard task?" — the HR manager thought. "Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using ...
#include <bits/stdc++.h> using namespace std; int main() { cout << "25" << endl; return 0; }
2C++
{ "input": [ "2\n", "7\n", "2000000000000000000\n", "1000000000000000000\n", "987654321012345678\n", "3\n", "316240294332142860\n", "1000001000000000000\n", "1440563777463828705\n", "5\n", "8\n", "178760633629979647\n", "1000001000000010000\n", "10\n", "6\n"...
2CODEFORCES
630_A. Again Twenty Five!_1020
The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. "Do I give such a hard task?" — the HR manager thought. "Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using ...
n=int(input()) if(n==0): print(1) elif(n==1): print(5) else: print(25)
3Python3
{ "input": [ "2\n", "7\n", "2000000000000000000\n", "1000000000000000000\n", "987654321012345678\n", "3\n", "316240294332142860\n", "1000001000000000000\n", "1440563777463828705\n", "5\n", "8\n", "178760633629979647\n", "1000001000000010000\n", "10\n", "6\n"...
2CODEFORCES
630_A. Again Twenty Five!_1021
The HR manager was disappointed again. The last applicant failed the interview the same way as 24 previous ones. "Do I give such a hard task?" — the HR manager thought. "Just raise number 5 to the power of n and get last two digits of the number. Yes, of course, n can be rather big, and one cannot find the power using ...
import java.util.Scanner; public class FiveToTheN { public static void main(String[] args) { Scanner scan = new Scanner(System.in); scan.nextLine(); System.out.println(25); } }
4JAVA
{ "input": [ "2\n", "7\n", "2000000000000000000\n", "1000000000000000000\n", "987654321012345678\n", "3\n", "316240294332142860\n", "1000001000000000000\n", "1440563777463828705\n", "5\n", "8\n", "178760633629979647\n", "1000001000000010000\n", "10\n", "6\n"...
2CODEFORCES
656_E. Out of Controls_1022
You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. Input The first line of the input contains a single integer N (3 ≤ N ≤ 10). Th...
n = input() a = [] h = range(n) def read_a(i): rec = map(lambda _: lambda: read_a(i + 1), h) rec[-1] = lambda: 0 rec[i]() a.append(list(map(int, raw_input().split(' ')))) read_a(0) def iter_i(i): rec_i = map(lambda _: lambda: iter_i(i + 1), h) rec_i[-1] = lambda: 0 def iter_j(j): ...
1Python2
{ "input": [ "3\n0 1 1\n1 0 4\n1 4 0\n", "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n", "6\n0 41 48 86 94 14\n41 0 1 30 59 39\n48 1 0 9 31 49\n86 30 9 0 48 30\n94 59 31 48 0 33\n14 39 49 30 33 0\n", "6\n0 92 9 24 50 94\n92 0 70 73 57 87\n9 70 0 31 14 100\n24 73 31 0 66 25\n50 57 14 66 0 81\n94 87 100 25 8...
2CODEFORCES
656_E. Out of Controls_1023
You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. Input The first line of the input contains a single integer N (3 ≤ N ≤ 10). Th...
#include <bits/stdc++.h> using namespace std; int main() { int maxn = 999999999; int m[105][105]; int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) m[i][j] = maxn; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> m[i][j]; for (int k = 0; k < n; k++) for (in...
2C++
{ "input": [ "3\n0 1 1\n1 0 4\n1 4 0\n", "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n", "6\n0 41 48 86 94 14\n41 0 1 30 59 39\n48 1 0 9 31 49\n86 30 9 0 48 30\n94 59 31 48 0 33\n14 39 49 30 33 0\n", "6\n0 92 9 24 50 94\n92 0 70 73 57 87\n9 70 0 31 14 100\n24 73 31 0 66 25\n50 57 14 66 0 81\n94 87 100 25 8...
2CODEFORCES
656_E. Out of Controls_1024
You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. Input The first line of the input contains a single integer N (3 ≤ N ≤ 10). Th...
#!/usr/bin/python3 import sys sys.setrecursionlimit(10000) n = int(input()) a = [] def read(i): try: 1 / (n - i) a.append(list(map(int, input().split()))) read(i + 1); except: pass def f(i, j, k): try: 1 / (n - i) 1 / (n - j) 1 / (n - k) a...
3Python3
{ "input": [ "3\n0 1 1\n1 0 4\n1 4 0\n", "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n", "6\n0 41 48 86 94 14\n41 0 1 30 59 39\n48 1 0 9 31 49\n86 30 9 0 48 30\n94 59 31 48 0 33\n14 39 49 30 33 0\n", "6\n0 92 9 24 50 94\n92 0 70 73 57 87\n9 70 0 31 14 100\n24 73 31 0 66 25\n50 57 14 66 0 81\n94 87 100 25 8...
2CODEFORCES
656_E. Out of Controls_1025
You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. Input The first line of the input contains a single integer N (3 ≤ N ≤ 10). Th...
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class E { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; StringTokenizer tok = new StringTokenizer(""); void solve() throws IOException { solveTest(); } Scanner sc = new Scanner(...
4JAVA
{ "input": [ "3\n0 1 1\n1 0 4\n1 4 0\n", "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n", "6\n0 41 48 86 94 14\n41 0 1 30 59 39\n48 1 0 9 31 49\n86 30 9 0 48 30\n94 59 31 48 0 33\n14 39 49 30 33 0\n", "6\n0 92 9 24 50 94\n92 0 70 73 57 87\n9 70 0 31 14 100\n24 73 31 0 66 25\n50 57 14 66 0 81\n94 87 100 25 8...
2CODEFORCES
67_E. Save the City!_1026
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away fro...
from math import floor,ceil n = input() x,y = zip(*[map(int,raw_input().split()) for _ in xrange(n)]) nr,mr=min(x[:2]),max(x[:2]) for j in xrange(3,n): i = j-1 dx = x[j]-x[i] dy = y[j]-y[i] t = 1.*(y[0]-y[i])*dx; r = t/dy+x[i] if dy else 1e9 if t-dy*(mr-x[i])>0 and r<mr: mr=r; if t-dy*(nr-x[...
1Python2
{ "input": [ "5\n4 8\n8 8\n9 4\n4 0\n0 4\n", "5\n4 8\n5 8\n5 4\n7 4\n2 2\n", "4\n889308 0\n110692 0\n0 461939\n146447 815492\n", "5\n0 4\n3 4\n2 2\n2 0\n0 0\n", "5\n0 100\n50 100\n50 99\n149 0\n0 0\n", "10\n1000 0\n100 0\n0 25\n100 50\n100 51\n99 102\n1001 102\n1000 51\n1000 50\n1100 25\n", ...
2CODEFORCES
67_E. Save the City!_1027
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away fro...
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-6; struct pos { double x, y; }; struct vec { double x, y; }; struct seg { pos a, b; }; long long sign(double x) { return x < -eps ? -1 : x > eps ? 1 : 0; } double dot(vec a, vec b) { return a.x * b.x + a.y * b.y; } d...
2C++
{ "input": [ "5\n4 8\n8 8\n9 4\n4 0\n0 4\n", "5\n4 8\n5 8\n5 4\n7 4\n2 2\n", "4\n889308 0\n110692 0\n0 461939\n146447 815492\n", "5\n0 4\n3 4\n2 2\n2 0\n0 0\n", "5\n0 100\n50 100\n50 99\n149 0\n0 0\n", "10\n1000 0\n100 0\n0 25\n100 50\n100 51\n99 102\n1001 102\n1000 51\n1000 50\n1100 25\n", ...
2CODEFORCES
67_E. Save the City!_1028
In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away fro...
import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.fill; import static java.util.Arrays.binarySearch; import static java.util.Arrays.sort; public class Main { public static void main(String[] args) throws IOException { new Thread(null, new Runnable() { public vo...
4JAVA
{ "input": [ "5\n4 8\n8 8\n9 4\n4 0\n0 4\n", "5\n4 8\n5 8\n5 4\n7 4\n2 2\n", "4\n889308 0\n110692 0\n0 461939\n146447 815492\n", "5\n0 4\n3 4\n2 2\n2 0\n0 0\n", "5\n0 100\n50 100\n50 99\n149 0\n0 0\n", "10\n1000 0\n100 0\n0 25\n100 50\n100 51\n99 102\n1001 102\n1000 51\n1000 50\n1100 25\n", ...
2CODEFORCES
702_E. Analysis of Pathes in Functional Graph_1029
You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1. Graph is given as the array f0, f1, ..., fn - 1, where fi — the number of vertex to which goes the only arc from the vertex i. Besides you are given array with weights o...
#include <bits/stdc++.h> using namespace std; long long n, k; long long a[100010]; long long b[100010]; long long BZ[100010][40]; long long Min[100010][40]; long long Sum[100010][40]; int main() { scanf("%lld%lld", &n, &k); for (long long i = 0; i < n; ++i) scanf("%lld", &BZ[i][0]); for (long long i = 0; i < n; +...
2C++
{ "input": [ "5 3\n1 2 3 4 0\n4 1 2 14 3\n", "7 3\n1 2 3 4 3 2 6\n6 3 1 4 2 2 3\n", "4 4\n0 1 2 3\n0 1 2 3\n", "1 1\n0\n10000\n", "2 3\n1 0\n4 7\n", "1 2\n0\n10000\n", "3 10\n0 1 2\n9240 5331 6721\n", "4 10\n2 1 2 1\n960 2596 3752 8303\n", "6 10\n0 3 3 5 3 5\n4845 6494 579 5025 299...
2CODEFORCES
702_E. Analysis of Pathes in Functional Graph_1030
You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1. Graph is given as the array f0, f1, ..., fn - 1, where fi — the number of vertex to which goes the only arc from the vertex i. Besides you are given array with weights o...
import sys n, k = map(int, sys.stdin.buffer.readline().decode('utf-8').split()) a = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split())) b = list(map(int, sys.stdin.buffer.readline().decode('utf-8').split())) logk = len(bin(k)) - 2 sum_w, sum_w_p = b[:], b[:] min_w, min_w_p = b[:], b[:] dest, dest_p = a...
3Python3
{ "input": [ "5 3\n1 2 3 4 0\n4 1 2 14 3\n", "7 3\n1 2 3 4 3 2 6\n6 3 1 4 2 2 3\n", "4 4\n0 1 2 3\n0 1 2 3\n", "1 1\n0\n10000\n", "2 3\n1 0\n4 7\n", "1 2\n0\n10000\n", "3 10\n0 1 2\n9240 5331 6721\n", "4 10\n2 1 2 1\n960 2596 3752 8303\n", "6 10\n0 3 3 5 3 5\n4845 6494 579 5025 299...
2CODEFORCES
702_E. Analysis of Pathes in Functional Graph_1031
You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to n - 1. Graph is given as the array f0, f1, ..., fn - 1, where fi — the number of vertex to which goes the only arc from the vertex i. Besides you are given array with weights o...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.TreeMap; /* * Examples input 7 3 1 2 3 4 3 2 6 6 3 1 4 2 2 3 output 10 1 8 1 7 1 10 2 8 2 7 1 9 3 input 4 4 0 1 2 3 0 1 2 3 output 0 0 4 1 8 2 12 3 input 5 3 1 2 3 4 0 4 1 2 14 3 ...
4JAVA
{ "input": [ "5 3\n1 2 3 4 0\n4 1 2 14 3\n", "7 3\n1 2 3 4 3 2 6\n6 3 1 4 2 2 3\n", "4 4\n0 1 2 3\n0 1 2 3\n", "1 1\n0\n10000\n", "2 3\n1 0\n4 7\n", "1 2\n0\n10000\n", "3 10\n0 1 2\n9240 5331 6721\n", "4 10\n2 1 2 1\n960 2596 3752 8303\n", "6 10\n0 3 3 5 3 5\n4845 6494 579 5025 299...
2CODEFORCES
724_F. Uniformly Branched Trees_1032
A tree is a connected graph without cycles. Two trees, consisting of n vertices each, are called isomorphic if there exists a permutation p: {1, ..., n} → {1, ..., n} such that the edge (u, v) is present in the first tree if and only if the edge (pu, pv) is present in the second tree. Vertex of the tree is called int...
#include <bits/stdc++.h> using namespace std; int fac[1010], inv[1010], mod; int ksm(int a, int b = mod - 2) { int r = 1; for (; b; b >>= 1) { if (b & 1) r = 1ll * r * a % mod; a = 1ll * a * a % mod; } return r; } int C(int a, int b) { int r = inv[b]; for (b--; b >= 0; b--) r = 1ll * r * (a - b) % m...
2C++
{ "input": [ "10 3 409693891\n", "65 4 177545087\n", "5 2 433416647\n", "997 6 680633279\n", "989 8 990767311\n", "999 2 750160753\n", "1 4 904448911\n", "1000 3 750160753\n", "1000 6 970400047\n", "983 10 762763321\n", "106 9 434448163\n", "2 3 434448163\n", "102 4...
2CODEFORCES
724_F. Uniformly Branched Trees_1033
A tree is a connected graph without cycles. Two trees, consisting of n vertices each, are called isomorphic if there exists a permutation p: {1, ..., n} → {1, ..., n} such that the edge (u, v) is present in the first tree if and only if the edge (pu, pv) is present in the second tree. Vertex of the tree is called int...
import static java.lang.Double.parseDouble; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.System.exit; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; ...
4JAVA
{ "input": [ "10 3 409693891\n", "65 4 177545087\n", "5 2 433416647\n", "997 6 680633279\n", "989 8 990767311\n", "999 2 750160753\n", "1 4 904448911\n", "1000 3 750160753\n", "1000 6 970400047\n", "983 10 762763321\n", "106 9 434448163\n", "2 3 434448163\n", "102 4...
2CODEFORCES
746_F. Music in Car_1034
Sasha reaches the work by car. It takes exactly k minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the i-th song Sasha gets a pleasure which equals ai. The i-th song lasts for ti minutes. Before the beginning of his way Sasha turns on some song x and then he listens...
#include <bits/stdc++.h> using namespace std; const int N = 400000; int a[N], t[N], type[N], us[N]; int main() { ios::sync_with_stdio(0); int n, w, k; cin >> n >> w >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> t[i]; } int r = -1; int curr_time = 0; ...
2C++
{ "input": [ "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8\n", "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9\n", "1 1 3\n4\n7\n", "1 1 5\n6\n9\n", "1 1 2000000000\n1\n2\n", "28 3 2099\n768 115 416 934 926 65 802 980 551 213 335 202 784 914 46 609 34 492 985 740 521 894 648 155 925 436 428 25\n460 467 ...
2CODEFORCES
746_F. Music in Car_1035
Sasha reaches the work by car. It takes exactly k minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the i-th song Sasha gets a pleasure which equals ai. The i-th song lasts for ti minutes. Before the beginning of his way Sasha turns on some song x and then he listens...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Main { public static void main(String[] args) throws Exception { FastScanner scanner = new FastScanner(); ...
4JAVA
{ "input": [ "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8\n", "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9\n", "1 1 3\n4\n7\n", "1 1 5\n6\n9\n", "1 1 2000000000\n1\n2\n", "28 3 2099\n768 115 416 934 926 65 802 980 551 213 335 202 784 914 46 609 34 492 985 740 521 894 648 155 925 436 428 25\n460 467 ...
2CODEFORCES
76_B. Mice_1036
Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and ther...
n,m,x,y=map(int,raw_input().split()) a = map(int,raw_input().split()) b = map(int,raw_input().split()) v = [0]*m t = [0]*m i = 0 for x in a: while i<m-1 and abs(b[i]-x)>abs(b[i+1]-x): i+=1 tx = abs(b[i]-x) if i<m-1 and tx==abs(b[i+1]-x): if not v[i]: v[i]=1 t[i]=tx e...
1Python2
{ "input": [ "3 2 0 2\n0 1 3\n2 5\n", "20 18 1 2\n-9999944 -9999861 -9999850 -9999763 -9999656 -9999517 -9999375 -9999275 -9999203 -9999080 -9998988 -9998887 -9998714 -9998534 -9998475 -9998352 -9998164 -9998016 -9998002 -9997882\n-9999976 -9999912 -9999788 -9999738 -9999574 -9999460 -9999290 -9999260 -999914...
2CODEFORCES
76_B. Mice_1037
Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and ther...
#include <bits/stdc++.h> using namespace std; int n, m; int data[2][100010], t[100010], num[100010]; queue<pair<int, int> > que; int dis(int x, int y) { return abs(data[0][x] - data[1][y]); } int main() { scanf("%d%d%*d%*d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &data[0][i]); for (int i = 0; i < m; i++...
2C++
{ "input": [ "3 2 0 2\n0 1 3\n2 5\n", "20 18 1 2\n-9999944 -9999861 -9999850 -9999763 -9999656 -9999517 -9999375 -9999275 -9999203 -9999080 -9998988 -9998887 -9998714 -9998534 -9998475 -9998352 -9998164 -9998016 -9998002 -9997882\n-9999976 -9999912 -9999788 -9999738 -9999574 -9999460 -9999290 -9999260 -999914...
2CODEFORCES
76_B. Mice_1038
Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and ther...
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Locale; import java.util.StringTokenizer; import ...
4JAVA
{ "input": [ "3 2 0 2\n0 1 3\n2 5\n", "20 18 1 2\n-9999944 -9999861 -9999850 -9999763 -9999656 -9999517 -9999375 -9999275 -9999203 -9999080 -9998988 -9998887 -9998714 -9998534 -9998475 -9998352 -9998164 -9998016 -9998002 -9997882\n-9999976 -9999912 -9999788 -9999738 -9999574 -9999460 -9999290 -9999260 -999914...
2CODEFORCES
793_F. Julia the snail_1039
After hard work Igor decided to have some rest. He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium. Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the sna...
#include <bits/stdc++.h> using namespace std; const int MAXN = 111111; const int SQ = 200; int n, m; vector<int> go[MAXN]; vector<pair<int, int> > qq[MAXN], gg[MAXN]; int ans[MAXN], a[MAXN]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { int l, r; scanf("%d%d", &l, &r); --l; --r; ...
2C++
{ "input": [ "10\n10\n3 7\n1 4\n1 6\n5 5\n1 1\n3 9\n7 8\n1 2\n3 3\n7 10\n10\n2 4\n1 7\n3 4\n3 5\n2 8\n2 5\n5 5\n3 5\n7 7\n3 10\n", "8\n4\n1 2\n3 4\n2 5\n6 7\n5\n1 2\n1 4\n1 6\n2 7\n6 8\n", "10\n10\n8 10\n1 1\n1 6\n4 7\n4 9\n8 8\n1 4\n2 3\n4 5\n2 2\n10\n1 1\n1 2\n1 3\n1 4\n1 5\n6 10\n7 10\n8 10\n9 10\n10 1...
2CODEFORCES
793_F. Julia the snail_1040
After hard work Igor decided to have some rest. He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium. Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the sna...
import java.io.*; import java.util.*; public class E { static final int INF = Integer.MAX_VALUE / 3; static class Node { int l, r; Node left, right; int setMin; int max; public Node(int l, int r) { this.l = l; this.r = r; setMin = max = INF; if (r - l > 1) { int m = (l + r) >> 1; l...
4JAVA
{ "input": [ "10\n10\n3 7\n1 4\n1 6\n5 5\n1 1\n3 9\n7 8\n1 2\n3 3\n7 10\n10\n2 4\n1 7\n3 4\n3 5\n2 8\n2 5\n5 5\n3 5\n7 7\n3 10\n", "8\n4\n1 2\n3 4\n2 5\n6 7\n5\n1 2\n1 4\n1 6\n2 7\n6 8\n", "10\n10\n8 10\n1 1\n1 6\n4 7\n4 9\n8 8\n1 4\n2 3\n4 5\n2 2\n10\n1 1\n1 2\n1 3\n1 4\n1 5\n6 10\n7 10\n8 10\n9 10\n10 1...
2CODEFORCES
814_C. An impassioned circulation of affection_1041
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from le...
iLength = int(raw_input()) sStr = raw_input() #sStr = "ab"*750 questionList = [] iNum = int(raw_input()) for _ in xrange(iNum): iChange, sChar = raw_input().split() iChange = int(iChange) questionList.append((iChange, sChar)) hasCharDict = {} for sChar in sStr: hasCharDict[sChar] = 1 ansDict = {} for sChar in ...
1Python2
{ "input": [ "15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b\n", "6\nkoyomi\n3\n1 o\n4 o\n4 m\n", "10\naaaaaaaaaa\n2\n10 b\n10 z\n", "20\naaaaaaaaaaaaaaaaaaaa\n1\n11 a\n", "4\ncbcc\n12\n4 b\n4 c\n1 b\n2 a\n3 b\n2 c\n4 a\n1 a\n2 b\n3 a\n1 c\n3 c\n", "100\ndddddccccddd...
2CODEFORCES
814_C. An impassioned circulation of affection_1042
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from le...
#include <bits/stdc++.h> using namespace std; const int N = 1500 + 10; char s[N]; int n, q, a[N]; int memo[N][30]; int solve(int x, int c) { int j = 0, miss = 0, ret = 0; for (int i = 1; i <= n; i++) { if (a[i] != c && j < i) miss++, j = i; while (j + 1 <= n && (a[j + 1] == c || miss + 1 <= x)) { j++;...
2C++
{ "input": [ "15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b\n", "6\nkoyomi\n3\n1 o\n4 o\n4 m\n", "10\naaaaaaaaaa\n2\n10 b\n10 z\n", "20\naaaaaaaaaaaaaaaaaaaa\n1\n11 a\n", "4\ncbcc\n12\n4 b\n4 c\n1 b\n2 a\n3 b\n2 c\n4 a\n1 a\n2 b\n3 a\n1 c\n3 c\n", "100\ndddddccccddd...
2CODEFORCES
814_C. An impassioned circulation of affection_1043
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from le...
""" Python 3 compatibility tools. """ from __future__ import division, print_function import itertools import sys import os from io import BytesIO, IOBase if sys.version_info[0] < 3: input = raw_input range = xrange filter = itertools.ifilter map = itertools.imap zip = itertools.izip def is_it_local(): ...
3Python3
{ "input": [ "15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b\n", "6\nkoyomi\n3\n1 o\n4 o\n4 m\n", "10\naaaaaaaaaa\n2\n10 b\n10 z\n", "20\naaaaaaaaaaaaaaaaaaaa\n1\n11 a\n", "4\ncbcc\n12\n4 b\n4 c\n1 b\n2 a\n3 b\n2 c\n4 a\n1 a\n2 b\n3 a\n1 c\n3 c\n", "100\ndddddccccddd...
2CODEFORCES
814_C. An impassioned circulation of affection_1044
Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from le...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class CDFC { private static void solve(String s, int[][] q) { List<Integer>[] a = new ArrayList[26]; for (int i = 0; i < 26; i++) { a[i] = new ArrayList<>(); }...
4JAVA
{ "input": [ "15\nyamatonadeshiko\n10\n1 a\n2 a\n3 a\n4 a\n5 a\n1 b\n2 b\n3 b\n4 b\n5 b\n", "6\nkoyomi\n3\n1 o\n4 o\n4 m\n", "10\naaaaaaaaaa\n2\n10 b\n10 z\n", "20\naaaaaaaaaaaaaaaaaaaa\n1\n11 a\n", "4\ncbcc\n12\n4 b\n4 c\n1 b\n2 a\n3 b\n2 c\n4 a\n1 a\n2 b\n3 a\n1 c\n3 c\n", "100\ndddddccccddd...
2CODEFORCES
83_C. Track_1045
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest. The track's map is represented by a rectangle n × m in size divi...
#include <bits/stdc++.h> using namespace std; const int maxn = 55; int n, m, k; set<set<int> > ha[maxn][maxn]; string mat[maxn]; int stran[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; int br, bc; int er, ec; int dis(int r1, int c1, int r2, int c2) { return abs(r1 - r2) + abs(c1 - c2); } struct node { int r, c; string s; i...
2C++
{ "input": [ "5 3 2\nSba\nccc\naac\nccc\nabT\n", "3 4 1\nSxyy\nyxxx\nyyyT\n", "1 3 3\nTyS\n", "1 4 1\nSxyT\n", "20 10 3\nebebccacdb\neeebccddeT\neadebecaac\nadeeeaccbc\nbaccccdaed\ndeabceabba\ndadbecbaaa\neacbbcedcb\naeeScdbbab\nbabaecaead\nbacdbebeae\naacbadbeec\nacddceecca\nacaeaebaba\ncdddeaaea...
2CODEFORCES
83_C. Track_1046
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest. The track's map is represented by a rectangle n × m in size divi...
import sys from array import array # noqa: F401 from itertools import combinations from collections import deque def input(): return sys.stdin.buffer.readline().decode('utf-8') n, m, k = map(int, input().split()) chars = ( ['}' * (m + 2)] + ['}' + ''.join('{' if c == 'S' else '|' if c == 'T' else c for...
3Python3
{ "input": [ "5 3 2\nSba\nccc\naac\nccc\nabT\n", "3 4 1\nSxyy\nyxxx\nyyyT\n", "1 3 3\nTyS\n", "1 4 1\nSxyT\n", "20 10 3\nebebccacdb\neeebccddeT\neadebecaac\nadeeeaccbc\nbaccccdaed\ndeabceabba\ndadbecbaaa\neacbbcedcb\naeeScdbbab\nbabaecaead\nbacdbebeae\naacbadbeec\nacddceecca\nacaeaebaba\ncdddeaaea...
2CODEFORCES
83_C. Track_1047
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest. The track's map is represented by a rectangle n × m in size divi...
import java.util.*; public class Track{ static int[] dx = {-1, 1, 0, 0}; static int[] dy = {0, 0, -1, 1}; public static void main(String[] args){ Scanner in = new Scanner(System.in); int m = in.nextInt(); int n = in.nextInt(); int p = in.nextInt(); char[][] chs = new char[m][]; int[] S = null; int[]...
4JAVA
{ "input": [ "5 3 2\nSba\nccc\naac\nccc\nabT\n", "3 4 1\nSxyy\nyxxx\nyyyT\n", "1 3 3\nTyS\n", "1 4 1\nSxyT\n", "20 10 3\nebebccacdb\neeebccddeT\neadebecaac\nadeeeaccbc\nbaccccdaed\ndeabceabba\ndadbecbaaa\neacbbcedcb\naeeScdbbab\nbabaecaead\nbacdbebeae\naacbadbeec\nacddceecca\nacaeaebaba\ncdddeaaea...
2CODEFORCES
85_D. Sum of Medians_1048
In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance ...
#include <bits/stdc++.h> using namespace std; struct Node { int cnt = 1, key, prior; long long sum[5] = {}; Node *left = NULL, *right = NULL; }; typedef Node *PNode; Node nodes[111111]; int pri[111111]; int nodeCount = 0; inline int cnt(PNode &v) { return v ? v->cnt : 0; } inline void update(PNode &v) { if (v) ...
2C++
{ "input": [ "14\nadd 1\nadd 7\nadd 2\nadd 5\nsum\nadd 6\nadd 8\nadd 9\nadd 3\nadd 4\nadd 10\nsum\ndel 1\nsum\n", "6\nadd 4\nadd 5\nadd 1\nadd 2\nadd 3\nsum\n", "20\nadd 17\nadd 18\nadd 19\nsum\nadd 20\nadd 21\nadd 22\nsum\nadd 23\nadd 24\nadd 25\nsum\nadd 26\nadd 27\nadd 28\nsum\nadd 29\nadd 30\nadd 31\n...
2CODEFORCES
85_D. Sum of Medians_1049
In one well-known algorithm of finding the k-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance ...
import java.util.*; import java.io.*; import java.math.BigInteger; import com.sun.jmx.remote.internal.ArrayQueue; public class Main implements Runnable { // haha, you won't challenge my randomization with fixed seed!!! // static Random rm = new Random(31); static Random rm = new Random(); static class Tree { in...
4JAVA
{ "input": [ "14\nadd 1\nadd 7\nadd 2\nadd 5\nsum\nadd 6\nadd 8\nadd 9\nadd 3\nadd 4\nadd 10\nsum\ndel 1\nsum\n", "6\nadd 4\nadd 5\nadd 1\nadd 2\nadd 3\nsum\n", "20\nadd 17\nadd 18\nadd 19\nsum\nadd 20\nadd 21\nadd 22\nsum\nadd 23\nadd 24\nadd 25\nsum\nadd 26\nadd 27\nadd 28\nsum\nadd 29\nadd 30\nadd 31\n...
2CODEFORCES
886_D. Restoration of string_1050
A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Res...
'''input 4 mail ai lru cf ''' import sys pow2 = pow # for modular expo pow2(base, n, mod) from math import * from time import time from collections import defaultdict from bisect import bisect_right, bisect_left from string import ascii_lowercase as lcs from string import ascii_uppercase as ucs from fract...
1Python2
{ "input": [ "3\nkek\npreceq\ncheburek\n", "4\nmail\nai\nlru\ncf\n", "2\nab\nac\n", "2\nca\ncb\n", "2\ndc\nec\n", "2\naz\nzb\n", "2\naa\nb\n", "25\nsw\nwt\nc\nl\nyo\nag\nz\nof\np\nmz\nnm\nui\nzs\nj\nq\nk\ngd\nb\nen\nx\ndv\nty\nh\nr\nvu\n", "51\np\nsu\nbpxh\nx\nxhvacdy\nqosuf\ncdy\n...
2CODEFORCES
886_D. Restoration of string_1051
A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Res...
#include <bits/stdc++.h> using namespace std; char str[100010]; bool line[27][27]; bool p[27], vis[27]; int in[27], out[27]; string ans; bool dfs(int value); int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", str); int len = strlen(str); for (int i = 0; i < len - 1; i++) { ...
2C++
{ "input": [ "3\nkek\npreceq\ncheburek\n", "4\nmail\nai\nlru\ncf\n", "2\nab\nac\n", "2\nca\ncb\n", "2\ndc\nec\n", "2\naz\nzb\n", "2\naa\nb\n", "25\nsw\nwt\nc\nl\nyo\nag\nz\nof\np\nmz\nnm\nui\nzs\nj\nq\nk\ngd\nb\nen\nx\ndv\nty\nh\nr\nvu\n", "51\np\nsu\nbpxh\nx\nxhvacdy\nqosuf\ncdy\n...
2CODEFORCES
886_D. Restoration of string_1052
A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Res...
StringsNumber = int(input()) FinalStrings = [] Strings = [] for i in range(StringsNumber): Strings.append(input()) LetterGraph = {} # Генерим граф for i in range(len(Strings)): if len(Strings[i]) == 1: if Strings[i] not in LetterGraph: LetterGraph[Strings[i]] = "" #print("заа...
3Python3
{ "input": [ "3\nkek\npreceq\ncheburek\n", "4\nmail\nai\nlru\ncf\n", "2\nab\nac\n", "2\nca\ncb\n", "2\ndc\nec\n", "2\naz\nzb\n", "2\naa\nb\n", "25\nsw\nwt\nc\nl\nyo\nag\nz\nof\np\nmz\nnm\nui\nzs\nj\nq\nk\ngd\nb\nen\nx\ndv\nty\nh\nr\nvu\n", "51\np\nsu\nbpxh\nx\nxhvacdy\nqosuf\ncdy\n...
2CODEFORCES
886_D. Restoration of string_1053
A substring of some string is called the most frequent, if the number of its occurrences is not less than number of occurrences of any other substring. You are given a set of strings. A string (not necessarily from this set) is called good if all elements of the set are the most frequent substrings of this string. Res...
import java.io.*; import java.util.*; public class __Solution { static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; public static void main(String[] args) { new __Solution().run(); } BufferedReader in; PrintWriter out; StringTokenizer tok; void init()...
4JAVA
{ "input": [ "3\nkek\npreceq\ncheburek\n", "4\nmail\nai\nlru\ncf\n", "2\nab\nac\n", "2\nca\ncb\n", "2\ndc\nec\n", "2\naz\nzb\n", "2\naa\nb\n", "25\nsw\nwt\nc\nl\nyo\nag\nz\nof\np\nmz\nnm\nui\nzs\nj\nq\nk\ngd\nb\nen\nx\ndv\nty\nh\nr\nvu\n", "51\np\nsu\nbpxh\nx\nxhvacdy\nqosuf\ncdy\n...
2CODEFORCES
909_D. Colorful Points_1054
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of p...
s=raw_input() a=[[s[0],1]] for i in s[1:]: if(a[-1][0]==i): a[-1][1]+=1 else: a.append([i,1]) turns=0 while((len(a)>1)): turns+=1 temp=[] if(a[0][1]>1): temp.append([a[0][0],a[0][1]-1]) for i in a[1:-1]: if(i[1]>2): temp.append([i[0],i[1]-2]) if(a[...
1Python2
{ "input": [ "aabb\n", "aabcaa\n", "bbbbbbbbaaaaaaaaaaaccccccaaaaaaaaaaaaaaccccccccaaaaaaaaabbbbbbccbbbaaaaaabccccccaaaacaaacccccccccccb\n", "ccccccccccccccccccccccccccccccccaaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccc\n", "aaaaabbbbbaaaaabbbbaaabbbbbbbaaabbbbbabbbbbbbaabbb...
2CODEFORCES
909_D. Colorful Points_1055
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of p...
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; list<pair<char, int>> L; int solve() { int res = 0; while (L.size() > 1) { int mini = INF; for (auto it = L.begin(); it != L.end(); ++it) { int x = it->second; if (it == L.begin() || next(it) == L.end()) { mini = min(mini...
2C++
{ "input": [ "aabb\n", "aabcaa\n", "bbbbbbbbaaaaaaaaaaaccccccaaaaaaaaaaaaaaccccccccaaaaaaaaabbbbbbccbbbaaaaaabccccccaaaacaaacccccccccccb\n", "ccccccccccccccccccccccccccccccccaaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccc\n", "aaaaabbbbbaaaaabbbbaaabbbbbbbaaabbbbbabbbbbbbaabbb...
2CODEFORCES
909_D. Colorful Points_1056
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of p...
name = input() blocks = [] now = name[0] counter = 1 for x in range(1, len(name)): if name[x] != now: blocks.append((now, counter)) now = name[x] counter = 1 else: counter += 1 blocks.append((now, counter)) counter = 0 temp = [] while len(blocks) > 1: counter += 1 temp ...
3Python3
{ "input": [ "aabb\n", "aabcaa\n", "bbbbbbbbaaaaaaaaaaaccccccaaaaaaaaaaaaaaccccccccaaaaaaaaabbbbbbccbbbaaaaaabccccccaaaacaaacccccccccccb\n", "ccccccccccccccccccccccccccccccccaaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccc\n", "aaaaabbbbbaaaaabbbbaaabbbbbbbaaabbbbbabbbbbbbaabbb...
2CODEFORCES
909_D. Colorful Points_1057
You are given a set of points on a straight line. Each point has a color assigned to it. For point a, its neighbors are the points which don't have any other points between them and a. Each point has at most two neighbors - one from the left and one from the right. You perform a sequence of operations on this set of p...
import java.io.* ; import java.util.* ; import java.text.* ; import java.math.* ; import static java.lang.Math.min ; import static java.lang.Math.max ; public class Codeshefcode{ public static void main(String[] args) throws IOException{ Solver Machine = new Solver() ; Machine.Solve() ; Machine.Finish() ; // n...
4JAVA
{ "input": [ "aabb\n", "aabcaa\n", "bbbbbbbbaaaaaaaaaaaccccccaaaaaaaaaaaaaaccccccccaaaaaaaaabbbbbbccbbbaaaaaabccccccaaaacaaacccccccccccb\n", "ccccccccccccccccccccccccccccccccaaaaaaaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccc\n", "aaaaabbbbbaaaaabbbbaaabbbbbbbaaabbbbbabbbbbbbaabbb...
2CODEFORCES
931_A. Friends Meeting_1058
Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the f...
from sys import stdin a = int(stdin.readline()) b = int(stdin.readline()) s = abs(a-b) x = s/2 y = s - x print (x*(x+1))/2 + (y * (y+1) )/2
1Python2
{ "input": [ "3\n4\n", "5\n10\n", "101\n99\n", "188\n762\n", "352\n445\n", "596\n777\n", "1000\n999\n", "1000\n2\n", "773\n70\n", "1\n1000\n", "285\n153\n", "892\n520\n", "1000\n1\n", "2\n1000\n", "138\n370\n", "1\n999\n", "967\n487\n", "999\n2\n...
2CODEFORCES
931_A. Friends Meeting_1059
Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the f...
#include <bits/stdc++.h> using namespace std; int a, b, temp, ans; int main() { cin >> a >> b; temp = abs(b - a); if (temp % 2 == 0) { for (int i = 1; i <= temp / 2; i++) ans += i; cout << ans * 2; } else { for (int i = 1; i <= temp / 2 + 1; i++) { ans += i; } ans *= 2; ans -= temp...
2C++
{ "input": [ "3\n4\n", "5\n10\n", "101\n99\n", "188\n762\n", "352\n445\n", "596\n777\n", "1000\n999\n", "1000\n2\n", "773\n70\n", "1\n1000\n", "285\n153\n", "892\n520\n", "1000\n1\n", "2\n1000\n", "138\n370\n", "1\n999\n", "967\n487\n", "999\n2\n...
2CODEFORCES
931_A. Friends Meeting_1060
Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the f...
a=int(input()) b=int(input()) def fact(a): ans=0 for i in range(a,0,-1): ans=ans+i return ans d=abs(a-b) if d==1: print("1") elif d%2==0: a=fact(d//2) a=a*2 print(a) else: a=fact(d//2) b=fact((d+1)//2) print(a+b)
3Python3
{ "input": [ "3\n4\n", "5\n10\n", "101\n99\n", "188\n762\n", "352\n445\n", "596\n777\n", "1000\n999\n", "1000\n2\n", "773\n70\n", "1\n1000\n", "285\n153\n", "892\n520\n", "1000\n1\n", "2\n1000\n", "138\n370\n", "1\n999\n", "967\n487\n", "999\n2\n...
2CODEFORCES
931_A. Friends Meeting_1061
Two friends are on the coordinate axis Ox in points with integer coordinates. One of them is in the point x1 = a, another one is in the point x2 = b. Each of the friends can move by one along the line in any direction unlimited number of times. When a friend moves, the tiredness of a friend changes according to the f...
import java.io.*; import java.util.*; public class q1 { public static void main(String args[])throws IOException { Scanner in=new Scanner(System.in); PrintWriter pw=new PrintWriter(System.out, true); int a=in.nextInt(); int b=in.nextInt(); int dis=Math.abs(a-b); int ma=dis/2; int mb=dis/...
4JAVA
{ "input": [ "3\n4\n", "5\n10\n", "101\n99\n", "188\n762\n", "352\n445\n", "596\n777\n", "1000\n999\n", "1000\n2\n", "773\n70\n", "1\n1000\n", "285\n153\n", "892\n520\n", "1000\n1\n", "2\n1000\n", "138\n370\n", "1\n999\n", "967\n487\n", "999\n2\n...
2CODEFORCES
958_E2. Guard Duty (medium)_1062
Princess Heidi decided to give orders to all her K Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at N specific moments t1, t2, ..., tN. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board he...
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; const int maxk = 5010; const long long oo = 1e13; int k, n, a[maxn]; long long f[3][maxk][3]; vector<pair<long long, int> > ti; int main() { cin >> k >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); for (int i = 2; i <...
2C++
{ "input": [ "2 5\n1 4 6 7 12\n", "4 12\n15 7 4 19 3 30 14 1 5 23 17 25\n", "3 6\n6 3 4 2 5 1\n", "1 10\n700282491 332230980 954401907 59481241 188256336 995466811 463183048 725322957 89294440 697458143\n", "10 23\n411970360 640040178 804073268 984486113 986486863 148005705 76298857 42029651 53222...
2CODEFORCES
958_E2. Guard Duty (medium)_1063
Princess Heidi decided to give orders to all her K Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at N specific moments t1, t2, ..., tN. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board he...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; i...
4JAVA
{ "input": [ "2 5\n1 4 6 7 12\n", "4 12\n15 7 4 19 3 30 14 1 5 23 17 25\n", "3 6\n6 3 4 2 5 1\n", "1 10\n700282491 332230980 954401907 59481241 188256336 995466811 463183048 725322957 89294440 697458143\n", "10 23\n411970360 640040178 804073268 984486113 986486863 148005705 76298857 42029651 53222...
2CODEFORCES
985_A. Chess Placing_1064
You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW". Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to <image>. In one step you can mov...
n = input() l = map(int, raw_input().split()) l = [i-1 for i in l] l.sort() white, black = 0, 0 for i in range(0, n, 2): white += abs(i-l[i/2]) for i in range(1, n, 2): black += abs(i-l[i/2]) print min(white,black)
1Python2
{ "input": [ "10\n1 2 3 4 5\n", "6\n1 2 6\n", "10\n9 8 7 6 5\n", "100\n41 13 29 11 25 15 6 23 28 50 48 17 3 9 44 24 5 19 34 22 33 32 20 16 35 37 4 10 46 2 39 40 47 49 36 42 1 30 43 21 14 7 18 45 31 8 12 26 27 38\n", "10\n10 9 8 1 5\n", "6\n3 5 6\n", "6\n1 4 5\n", "100\n84 10 26 79 58 9...
2CODEFORCES
985_A. Chess Placing_1065
You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW". Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to <image>. In one step you can mov...
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int n; cin >> n; int g1 = 0, g2 = 0; int a[n / 2]; for (int i = 0; i < n / 2; i++) { cin >> a[i]; } sort(a, a + n / 2); for (int i = 1; i <= n / 2; i++) { g1 += abs(a[i - 1] - (i * 2 - 1)...
2C++
{ "input": [ "10\n1 2 3 4 5\n", "6\n1 2 6\n", "10\n9 8 7 6 5\n", "100\n41 13 29 11 25 15 6 23 28 50 48 17 3 9 44 24 5 19 34 22 33 32 20 16 35 37 4 10 46 2 39 40 47 49 36 42 1 30 43 21 14 7 18 45 31 8 12 26 27 38\n", "10\n10 9 8 1 5\n", "6\n3 5 6\n", "6\n1 4 5\n", "100\n84 10 26 79 58 9...
2CODEFORCES
985_A. Chess Placing_1066
You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW". Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to <image>. In one step you can mov...
import re import math import decimal import bisect def read(): return input().strip() n = int(read()) ps = [0 for i in range(1, n+1)] nadd = 10 for x in sorted([int(_) for _ in read().split()]): ps[x-1] = nadd nadd += 10 nadd = 15 for i, p in enumerate(ps): if p == 0: ps[i] = nadd nadd += 10 # print(ps) swap...
3Python3
{ "input": [ "10\n1 2 3 4 5\n", "6\n1 2 6\n", "10\n9 8 7 6 5\n", "100\n41 13 29 11 25 15 6 23 28 50 48 17 3 9 44 24 5 19 34 22 33 32 20 16 35 37 4 10 46 2 39 40 47 49 36 42 1 30 43 21 14 7 18 45 31 8 12 26 27 38\n", "10\n10 9 8 1 5\n", "6\n3 5 6\n", "6\n1 4 5\n", "100\n84 10 26 79 58 9...
2CODEFORCES
985_A. Chess Placing_1067
You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW". Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to <image>. In one step you can mov...
import java.io.*; import java.util.*; public class A{ public static void main(String[] args)throws Throwable { MyScanner sc=new MyScanner(); PrintWriter pw=new PrintWriter(System.out); int x=0,y=0; int n=sc.nextInt(); int [] a=new int [n/2]; for(int i=0;i<n/2;i++) a[i]=sc.nextInt()-1; Arrays.sort(a...
4JAVA
{ "input": [ "10\n1 2 3 4 5\n", "6\n1 2 6\n", "10\n9 8 7 6 5\n", "100\n41 13 29 11 25 15 6 23 28 50 48 17 3 9 44 24 5 19 34 22 33 32 20 16 35 37 4 10 46 2 39 40 47 49 36 42 1 30 43 21 14 7 18 45 31 8 12 26 27 38\n", "10\n10 9 8 1 5\n", "6\n3 5 6\n", "6\n1 4 5\n", "100\n84 10 26 79 58 9...
2CODEFORCES
abc-garfield_1068
Garfield the cat likes candies A LOT. He always keeps a huge stock of it at his home. Today John, his owner, brought home three types of candies. He brought A pieces of Red candy, B pieces of Green candy and C pieces of Blue candy. Garfield is really happy. But the problem is that John won’t allow him to eat all of it ...
t = int(raw_input()) for _ in range(t): n, a, b, c = map(int, raw_input().split()) ways = 0 for i in range(a + 1): for j in range(min(n - i, b) + 1): if(n - i - j < 0): break else: ways += min(n - i - j, c) + 1 print(ways)
1Python2
{ "input": [ "3\n2 1 2 3\n1 1 1 1\n2 1 0 1\n\nSAMPLE" ], "output": [ "9\n4\n4" ] }
3HACKEREARTH
bobs-journey-5_1069
Bob is travelling from one city to another. In his way, he sees many other cities pass by. What he does instead of learning the full names of the cities, he learns just the first character of the cities. For example, if he passes by "bhopal", he will just remember the 'b'. Given the list of N cities that come in h...
def fun(): n=input() l=[] for i in range(n): z=raw_input() l.append(z[0]) if len(l)==len(set(l)): return("YES") else: return("NO") a=input() for i in range(a): print(fun())
1Python2
{ "input": [ "2\n2\nbhopal\ndelhi\n3\nbhopal\ndelhi\ndehradun\n\nSAMPLE", "100\n4\nlrbbmqb\ncd\nr\nowkk\n7\nid\nqscdxrjmow\nrxsjybldbe\nsarcbyne\ndyggxxp\nlorel\nnmpa\n6\nfwkho\nkmcoqhnw\nkuewhsqmgb\nuqcljj\nvsw\ndkqtbxi\n10\nv\nrr\nlj\ntnsnfwzqfj\nafadr\nwsofsbcnuv\nhffbsaq\nwp\nc\ncehch\n2\nfrkmlnoz\nkpqpxr...
3HACKEREARTH
cube-change-qualifier2_1070
Chandan gave his son a cube with side N. The N X N X N cube is made up of small 1 X 1 X 1 cubes. Chandan's son is extremely notorious just like him. So he dropped the cube inside a tank filled with Coke. The cube got totally immersed in that tank. His son was somehow able to take out the cube from the tank. But soone...
''' # Read input from stdin and provide input before running code name = raw_input('What is your name?\n') print 'Hi, %s.' % name ''' t=input(); while t > 0 : c=input() if c!=1: print (c*c*c)-(c-2)*(c-2)*(c-2) t=t-1 else: print 1 t=t-1
1Python2
{ "input": [ "2\n1\n3\n\nSAMPLE", "61\n17357774\n248290406\n171408490\n20054376\n118774144\n110228788\n8827234\n85304880\n158466588\n168421585\n174586544\n51487597\n19100400\n4459509\n85309162\n445813368\n115311040\n646041788\n22514822\n141558016\n677295564\n42909504\n20244456\n10294157\n412622298\n147040179\...
3HACKEREARTH
friendless-dr-sheldon-cooper-14_1071
Leonard has decided to quit living with Dr. Sheldon Cooper and has started to live with Penny. Yes, you read it right. (And you read it here for the first time!) He is fed up of Sheldon, after all. Since, Sheldon no more has Leonard to drive him all around the city for various things, he's feeling a lot uneasy so he de...
t = int(raw_input()) for i in range(t): a, b = map(int, raw_input().split()) for j in range(b): m, n = map(int, raw_input().split()) print(a-1)
1Python2
{ "input": [ "1\n3 3\n1 2\n2 3\n1 3\n\nSAMPLE", "1\n3 3\n1 2\n2 3\n1 3", "1\n3 3\n1 2\n3 3\n1 3", "1\n2 3\n0 0\n2 -1\n0 1\n\nSANPLE", "1\n3 3\n1 2\n2 3\n1 3\n\nELPMAS", "1\n3 3\n1 2\n2 3\n2 3\n\nELPMAS", "1\n3 3\n1 0\n2 3\n2 3\n\nELPMAS", "1\n3 3\n1 2\n2 3\n1 1\n\nSAMPLE", "1\n3 3\...
3HACKEREARTH
jumping-frog_1072
There is a frog known as "CHAMELEON" because he has a special feature to change its body's color similar to stone's color on which he sits. There are N colorful stones lying in a row, but of only 1 to 100 different colors. Frog can hopp on another stone if the stone has same color as its body or (i-1)th stone if it is ...
from collections import deque class Frog(): def __init__(self,n,stones): self.colors=[[] for _ in xrange(101)] self.N = n self.stones = stones for i,c in enumerate(stones): self.colors[c].append(i) self.l=[0]*self.N def bfsThrough(self,S,M,E): s1 = self.bfs(S,M) if s1==-1: return s1 ...
1Python2
{ "input": [ "6\n2 3 1 3 2 4\n2\n1 0 4\n2 3 5\n\nSAMPLE", "541\n54 98 68 63 83 94 55 35 12 63 30 17 97 62 96 26 63 76 91 19 52 42 55 95 8 97 6 18 96 3 46 21 55 88 14 27 65 8 94 93 52 39 40 52 12 94 89 39 38 6 24 92 88 40 89 12 40 8 86 41 66 15 61 91 11 32 33 59 77 24 46 51 97 17 6 58 16 40 84 28 51 28 56 46 6...
3HACKEREARTH
mind-palaces-3_1073
Sherlock Holmes loves mind palaces! We all know that. A mind palace, according to Mr. Holmes is something that lets him retrieve a given memory in the least time posible. For this, he structures his mind palace in a very special way. Let a NxM Matrix denote the mind palace of Mr. Holmes. For fast retrieval he keeps ea...
N, M = map(int, raw_input().split()) memory = {} for i in range(N): x = map(int, raw_input().split()) for index, elem in enumerate(x): memory[elem] = str(i) + ' ' + str(index) Q = int(raw_input()) for k in range(Q): q = int(raw_input()) if q in memory: print memory[q] else: print '-1 -1'
1Python2
{ "input": [ "5 5\n-993655555 -758584352 -725954642 -696391700 -649643547\n-591473088 -568010221 -432112275 -421496588 -351507172\n-323741602 -232192004 -30134637 -369573 100246476\n156824549 174266331 392354039 601294716 763826005\n768378344 802829330 818988557 992012759 999272829\n10\n156824549\n-758584352\n-99...
3HACKEREARTH
palin-pairs_1074
Panda has a thing for palindromes. Hence he was a given a problem by his master. The master will give Panda an array of strings S having N strings. Now Panda has to select the Palin Pairs from the given strings . A Palin Pair is defined as : (i,j) is a Palin Pair if Si = reverse(Sj) and i < j Panda wants to k...
from sys import stdin t = stdin.readline() di = {} a = stdin.readlines() ans = 0 for i in a: cur = i.strip() x=cur cur = list(cur) cur.reverse() cur = ''.join(cur) ans+=di.get(cur,0) di[x] = di.get(x,0)+1 print ans
1Python2
{ "input": [ "3\nbba\nabb\nabb\n\nSAMPLE", "3\nbba\nabb\nabc\n\nSAMPLE", "3\nbba\nabb\nabb\n\nSAMPLF", "3\nbba\nbba\nabc\n\nSAMPLE", "3\nbba\nabb\nabc\n\nSAPMLE", "3\nbba\nabb\nbac\n\nSAPMLE", "3\nbba\nabb\nbba\n\nSAMPLF", "3\nbba\nabb\ncab\n\nSAPMLE", "3\nbba\nabb\ncba\n\nSAMPLF",...
3HACKEREARTH
reversemerge-shuffle-reverse_1075
Given a string, S, we define some operations on the string as follows: a. reverse(S) denotes the string obtained by reversing string S. E.g.: reverse("abc") = "cba" b. shuffle(S) denotes any string that's a permutation of string S. E.g.: shuffle("god") ∈ ['god', 'gdo', 'ogd', 'odg', 'dgo', 'dog'] c. merge(S1,S2) den...
from collections import defaultdict def solve(S): # Reverse S S = S[::-1] # Count each character in S. count = defaultdict(int) for c in S: count[c] += 1 need = {} for c in count: need[c] = count[c] / 2 solution = [] i = 0 while len(solution) < len(S) / 2: ...
1Python2
{ "input": [ "eggegg\n\nSAMPLE", "bbcbccbabcbabcbaaccccaaabcbcaacacbabbbbcabcbbbbacbcaccccbcccbccaaabcabacccbaccccbbababccbccacbaccacbcccacbaaacacbbcbaaabbabbaaccbbbabccbccacacacabaababbcbcccbbcacabcabbbccababbcccacccccabbcbcbbaabbaabacabaabbbbcccccccacabaacbbbbbcbbabccbacbaabccbabccbbabccbacccaaaababccbbbaca...
3HACKEREARTH
special-matrix-1_1076
You are given a square matrix of size n (it will be an odd integer). Rows are indexed 0 to n-1 from top to bottom and columns are indexed 0 to n-1 form left to right. Matrix consists of only '*' and '.'. '*' appears only once in the matrix while all other positions are occupied by '.' Your task is to convert this matr...
t=int(input()) while t: t-=1 o=n=int(input()) i=i1=j=-1 while n: n-=1 i1+=1 s=raw_input() if j==-1 and '*' in s: j=s.index('*') i=i1 print abs((o/2)-i)+abs((o/2)-j)
1Python2
{ "input": [ "1\n7\n.......\n.*.....\n.......\n.......\n.......\n.......\n.......\n\nSAMPLE", "1\n7\n.......\n.*.....\n.......\n.......\n.......\n.......\n.......\n\nSAMQLE", "1\n5\n.......\n.*.....\n.......\n..-..-.\n.......\n.......\n....../\n\nSALPLE", "1\n7\n....../\n..*..//\n../....\n./-.--.\n......
3HACKEREARTH
trailing-zero-problem-1_1077
Given integer n, find length of n! (which is factorial of n) excluding trailing zeros. Input The first line of the standard input contains one integer t (t<10001) which is the number of test cases. In each of the next t lines there is number n (0 ≤ n ≤ 5*10^9). Output For each test, print the length of n! (which i...
import math for _ in xrange(int(raw_input())): n = int(raw_input()) #m = int(math.log10((n/math.exp(1.0))**n*math.sqrt(2*math.pi*n)*math.exp(1/12.0/n))) m = int(n*math.log10(n/math.e) + 0.5*(math.log10(2*math.pi*n))) a = n/5 z = n//5 i=2 while a > 1: a = n/(5**i) z += n//(5**i) i += 1 print m-z+1
1Python2
{ "input": [ "3\n5\n7\n10\n\nSAMPLE", "4\n4568\n4545992\n9265642\n4592886", "4\n4568\n4545992\n9265642\n1073637", "4\n4568\n3988846\n9265642\n1073637", "4\n9136\n3988846\n9265642\n1073637", "4\n9136\n5594790\n9265642\n1073637", "4\n11948\n5594790\n9265642\n1073637", "4\n11948\n5594790\...
3HACKEREARTH
p02574 AtCoder Beginner Contest 177 - Coprime_1078
We have N integers. The i-th number is A_i. \\{A_i\\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N. \\{A_i\\} is said to be setwise coprime when \\{A_i\\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1. Determine if \\{A_i\\} is pairwise coprime, setw...
FAST_IO = 0 if FAST_IO: import io, sys, atexit rr = iter(sys.stdin.read().splitlines()).next sys.stdout = _OUTPUT_BUFFER = io.BytesIO() @atexit.register def write(): sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) else: rr = raw_input rri = lambda: int(rr()) rrm = lambda: map(int, rr()....
1Python2
{ "input": [ "3\n6 10 16", "3\n6 10 15", "3\n3 4 5", "3\n6 5 15", "3\n4 4 2", "3\n4 3 1", "3\n4 4 5", "3\n4 8 2", "3\n8 8 2", "3\n4 4 1", "3\n4 4 4", "3\n6 8 2", "3\n7 4 4", "3\n6 8 3", "3\n2 4 4", "3\n1 4 4", "3\n2 8 4", "3\n4 8 4", "3\n6 10...
5ATCODER
p02574 AtCoder Beginner Contest 177 - Coprime_1079
We have N integers. The i-th number is A_i. \\{A_i\\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N. \\{A_i\\} is said to be setwise coprime when \\{A_i\\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1. Determine if \\{A_i\\} is pairwise coprime, setw...
#include<bits/stdc++.h> using namespace std; int n,f[1202020],siz[202020]; const int mod = 1e9+7; long long sum = 0; inline int getf(int x) { if (x == f[x]) return x; return f[x] = getf(f[x]); } int m,aa,bb; int main() { scanf("%d",&n); for (int i = 1;i <= n; i++) { scanf("%d",&aa); f[aa]++; } int fl = 0; for (...
2C++
{ "input": [ "3\n6 10 16", "3\n6 10 15", "3\n3 4 5", "3\n6 5 15", "3\n4 4 2", "3\n4 3 1", "3\n4 4 5", "3\n4 8 2", "3\n8 8 2", "3\n4 4 1", "3\n4 4 4", "3\n6 8 2", "3\n7 4 4", "3\n6 8 3", "3\n2 4 4", "3\n1 4 4", "3\n2 8 4", "3\n4 8 4", "3\n6 10...
5ATCODER
p02574 AtCoder Beginner Contest 177 - Coprime_1080
We have N integers. The i-th number is A_i. \\{A_i\\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N. \\{A_i\\} is said to be setwise coprime when \\{A_i\\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1. Determine if \\{A_i\\} is pairwise coprime, setw...
N = int(input()) A = list(map(int,input().split())) A = sorted(A,reverse = True) prime = [0] * (10**6+1) eratos = [True] * (A[0] + 1) D = [0] * (A[0]+1) D[1] = 1 for i in range(2,A[0] + 1): if not eratos[i]: continue else: for j in range(i,A[0] + 1,i): if not D[j]: D[j] = i if j!=i: ...
3Python3
{ "input": [ "3\n6 10 16", "3\n6 10 15", "3\n3 4 5", "3\n6 5 15", "3\n4 4 2", "3\n4 3 1", "3\n4 4 5", "3\n4 8 2", "3\n8 8 2", "3\n4 4 1", "3\n4 4 4", "3\n6 8 2", "3\n7 4 4", "3\n6 8 3", "3\n2 4 4", "3\n1 4 4", "3\n2 8 4", "3\n4 8 4", "3\n6 10...
5ATCODER
p02574 AtCoder Beginner Contest 177 - Coprime_1081
We have N integers. The i-th number is A_i. \\{A_i\\} is said to be pairwise coprime when GCD(A_i,A_j)=1 holds for every pair (i, j) such that 1\leq i < j \leq N. \\{A_i\\} is said to be setwise coprime when \\{A_i\\} is not pairwise coprime but GCD(A_1,\ldots,A_N)=1. Determine if \\{A_i\\} is pairwise coprime, setw...
import java.io.*; import java.util.*; import static java.lang.System.out; public class Main { static MyReader in = new MyReader(); public static void main(String[] args) { int N = in.i(); int[] A = in.ii(N); String ans; if (gcd(A) > 1) { ans = "not coprime"; ...
4JAVA
{ "input": [ "3\n6 10 16", "3\n6 10 15", "3\n3 4 5", "3\n6 5 15", "3\n4 4 2", "3\n4 3 1", "3\n4 4 5", "3\n4 8 2", "3\n8 8 2", "3\n4 4 1", "3\n4 4 4", "3\n6 8 2", "3\n7 4 4", "3\n6 8 3", "3\n2 4 4", "3\n1 4 4", "3\n2 8 4", "3\n4 8 4", "3\n6 10...
5ATCODER
p02705 AtCoder Beginner Contest 163 - Circle Pond_1082
Print the circumference of a circle of radius R. Constraints * 1 \leq R \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: R Output Print the circumference of the circle. Your output is considered correct if and only if its absolute or relative error ...
import math print math.pi * 2 * float(raw_input())
1Python2
{ "input": [ "73", "1", "8", "0", "4", "-1", "2", "-2", "3", "-4", "6", "-3", "-6", "-9", "-8", "-12", "-5", "7", "-10", "10", "-17", "9", "-7", "15", "-11", "17", "-18", "33", "-31", "66", "-16...
5ATCODER
p02705 AtCoder Beginner Contest 163 - Circle Pond_1083
Print the circumference of a circle of radius R. Constraints * 1 \leq R \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: R Output Print the circumference of the circle. Your output is considered correct if and only if its absolute or relative error ...
#include<bits/stdc++.h> using namespace std; int main(){ int r; cin>>r; cout<<3.14159265*2*r<<endl; }
2C++
{ "input": [ "73", "1", "8", "0", "4", "-1", "2", "-2", "3", "-4", "6", "-3", "-6", "-9", "-8", "-12", "-5", "7", "-10", "10", "-17", "9", "-7", "15", "-11", "17", "-18", "33", "-31", "66", "-16...
5ATCODER
p02705 AtCoder Beginner Contest 163 - Circle Pond_1084
Print the circumference of a circle of radius R. Constraints * 1 \leq R \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: R Output Print the circumference of the circle. Your output is considered correct if and only if its absolute or relative error ...
R=int(input()) print(R*6.2831853)
3Python3
{ "input": [ "73", "1", "8", "0", "4", "-1", "2", "-2", "3", "-4", "6", "-3", "-6", "-9", "-8", "-12", "-5", "7", "-10", "10", "-17", "9", "-7", "15", "-11", "17", "-18", "33", "-31", "66", "-16...
5ATCODER
p02705 AtCoder Beginner Contest 163 - Circle Pond_1085
Print the circumference of a circle of radius R. Constraints * 1 \leq R \leq 100 * All values in input are integers. Input Input is given from Standard Input in the following format: R Output Print the circumference of the circle. Your output is considered correct if and only if its absolute or relative error ...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s=new Scanner(System.in); double r=s.nextDouble(); System.out.println(2*Math.PI*r); } }
4JAVA
{ "input": [ "73", "1", "8", "0", "4", "-1", "2", "-2", "3", "-4", "6", "-3", "-6", "-9", "-8", "-12", "-5", "7", "-10", "10", "-17", "9", "-7", "15", "-11", "17", "-18", "33", "-31", "66", "-16...
5ATCODER
p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree_1086
We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u, and Aoki is standing at Vertex v. Now, they will play a game of tag as follows: * 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vert...
def main(): n, u, v = map(int, raw_input().split()) u -= 1 v -= 1 tree = {i: [] for i in xrange(n)} for _ in xrange(n - 1): a, b = map(int, raw_input().split()) a -= 1 b -= 1 tree[a].append(b) tree[b].append(a) dist = [None] * n dist[v] = 0 stack =...
1Python2
{ "input": [ "2 1 2\n1 2", "5 4 5\n1 2\n1 3\n1 4\n1 5", "5 4 1\n1 2\n2 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n7 8\n8 9", "5 4 5\n1 2\n1 3\n2 4\n1 5", "5 4 1\n1 2\n1 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n4 8\n8 9", "5 4 1\n1 2\n2 3\n1 4\n3 5", "9 6 1\n1 2\...
5ATCODER
p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree_1087
We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u, and Aoki is standing at Vertex v. Now, they will play a game of tag as follows: * 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vert...
#include <bits/stdc++.h> using namespace std; int n, u, v; int dist[2][100000]; vector<vector<int> > g; void dfs(int v, int prev, int d, int f) { dist[f][v] = d; for (auto e : g[v]) if (e != prev) dfs(e, v, d + 1, f); } int main() { cin >> n >> u >> v; --u, --v; g.resize(n); for (int i = 1; i < n; ++i) { ...
2C++
{ "input": [ "2 1 2\n1 2", "5 4 5\n1 2\n1 3\n1 4\n1 5", "5 4 1\n1 2\n2 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n7 8\n8 9", "5 4 5\n1 2\n1 3\n2 4\n1 5", "5 4 1\n1 2\n1 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n4 8\n8 9", "5 4 1\n1 2\n2 3\n1 4\n3 5", "9 6 1\n1 2\...
5ATCODER
p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree_1088
We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u, and Aoki is standing at Vertex v. Now, they will play a game of tag as follows: * 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vert...
import sys sys.setrecursionlimit(10**8) input = sys.stdin.readline n, u, v = map(int, input().split()) u, v = u - 1, v - 1 graph = [[] for _ in range(n)] for _ in range(n - 1): a, b = map(int, input().split()) a, b = a - 1, b - 1 graph[a].append(b) graph[b].append(a) def dfs(v, d, count): count[v...
3Python3
{ "input": [ "2 1 2\n1 2", "5 4 5\n1 2\n1 3\n1 4\n1 5", "5 4 1\n1 2\n2 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n7 8\n8 9", "5 4 5\n1 2\n1 3\n2 4\n1 5", "5 4 1\n1 2\n1 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n4 8\n8 9", "5 4 1\n1 2\n2 3\n1 4\n3 5", "9 6 1\n1 2\...
5ATCODER
p02834 AtCoder Beginner Contest 148 - Playing Tag on Tree_1089
We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally. Takahashi is standing at Vertex u, and Aoki is standing at Vertex v. Now, they will play a game of tag as follows: * 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vert...
import java.util.*; import java.io.*; class Graph { public ArrayList<Integer>[] adjacency; Graph(int n){ adjacency = new ArrayList[n]; for(int i = 0; i < n; i++){ adjacency[i] = new ArrayList<Integer>(); } } public void add_edge(int from, int to, boolean is_directe...
4JAVA
{ "input": [ "2 1 2\n1 2", "5 4 5\n1 2\n1 3\n1 4\n1 5", "5 4 1\n1 2\n2 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n7 8\n8 9", "5 4 5\n1 2\n1 3\n2 4\n1 5", "5 4 1\n1 2\n1 3\n3 4\n3 5", "9 6 1\n1 2\n2 3\n3 4\n4 5\n5 6\n4 7\n4 8\n8 9", "5 4 1\n1 2\n2 3\n1 4\n3 5", "9 6 1\n1 2\...
5ATCODER
p02971 AtCoder Beginner Contest 134 - Exception Handling_1090
You are given a sequence of length N: A_1, A_2, ..., A_N. For each integer i between 1 and N (inclusive), answer the following question: * Find the maximum value among the N-1 elements other than A_i in the sequence. Constraints * 2 \leq N \leq 200000 * 1 \leq A_i \leq 200000 * All values in input are integers. Inp...
N = int(raw_input()) A = [(int(raw_input()), i) for i in range(N)] AS = sorted(A, key=lambda x:-x[0]) for i in range(N): if i != AS[0][1]: print AS[0][0] else: print AS[1][0]
1Python2
{ "input": [ "2\n5\n5", "3\n1\n4\n3", "2\n5\n4", "3\n1\n0\n3", "2\n3\n4", "3\n1\n0\n2", "2\n4\n4", "3\n1\n0\n1", "2\n4\n0", "3\n1\n2\n1", "3\n1\n3\n1", "3\n1\n3\n2", "3\n1\n2\n2", "3\n1\n4\n2", "3\n3\n4\n2", "3\n2\n4\n4", "3\n2\n3\n4", "3\n2\n3\n...
5ATCODER
p02971 AtCoder Beginner Contest 134 - Exception Handling_1091
You are given a sequence of length N: A_1, A_2, ..., A_N. For each integer i between 1 and N (inclusive), answer the following question: * Find the maximum value among the N-1 elements other than A_i in the sequence. Constraints * 2 \leq N \leq 200000 * 1 \leq A_i \leq 200000 * All values in input are integers. Inp...
#include<bits/stdc++.h> using namespace std; int main(){ int n,max=0,b=0; cin >> n; int a[n]; for(int i=0;i<n;i++){ cin >> a[i]; if(max<a[i]){ max = a[i]; b = i; } } sort(a,a+n); for(int i=0;i<n;i++){ if(i!=b) cout << max << endl; else cout << a[n-2] << endl; } }
2C++
{ "input": [ "2\n5\n5", "3\n1\n4\n3", "2\n5\n4", "3\n1\n0\n3", "2\n3\n4", "3\n1\n0\n2", "2\n4\n4", "3\n1\n0\n1", "2\n4\n0", "3\n1\n2\n1", "3\n1\n3\n1", "3\n1\n3\n2", "3\n1\n2\n2", "3\n1\n4\n2", "3\n3\n4\n2", "3\n2\n4\n4", "3\n2\n3\n4", "3\n2\n3\n...
5ATCODER
p02971 AtCoder Beginner Contest 134 - Exception Handling_1092
You are given a sequence of length N: A_1, A_2, ..., A_N. For each integer i between 1 and N (inclusive), answer the following question: * Find the maximum value among the N-1 elements other than A_i in the sequence. Constraints * 2 \leq N \leq 200000 * 1 \leq A_i \leq 200000 * All values in input are integers. Inp...
n=int(input()) a=[int(input()) for _ in [0]*n] b,c=sorted(a,reverse=True)[0:2] for A in a: if A==b: print(c) else: print(b)
3Python3
{ "input": [ "2\n5\n5", "3\n1\n4\n3", "2\n5\n4", "3\n1\n0\n3", "2\n3\n4", "3\n1\n0\n2", "2\n4\n4", "3\n1\n0\n1", "2\n4\n0", "3\n1\n2\n1", "3\n1\n3\n1", "3\n1\n3\n2", "3\n1\n2\n2", "3\n1\n4\n2", "3\n3\n4\n2", "3\n2\n4\n4", "3\n2\n3\n4", "3\n2\n3\n...
5ATCODER
p02971 AtCoder Beginner Contest 134 - Exception Handling_1093
You are given a sequence of length N: A_1, A_2, ..., A_N. For each integer i between 1 and N (inclusive), answer the following question: * Find the maximum value among the N-1 elements other than A_i in the sequence. Constraints * 2 \leq N \leq 200000 * 1 \leq A_i \leq 200000 * All values in input are integers. Inp...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int max = 0; int max_num =0; int max2 = 0; int n = sc.nextInt(); for(int i=0; i<n; i++) { int a = sc.nextInt(); if(a>max) { max = a; ...
4JAVA
{ "input": [ "2\n5\n5", "3\n1\n4\n3", "2\n5\n4", "3\n1\n0\n3", "2\n3\n4", "3\n1\n0\n2", "2\n4\n4", "3\n1\n0\n1", "2\n4\n0", "3\n1\n2\n1", "3\n1\n3\n1", "3\n1\n3\n2", "3\n1\n2\n2", "3\n1\n4\n2", "3\n3\n4\n2", "3\n2\n4\n4", "3\n2\n3\n4", "3\n2\n3\n...
5ATCODER
p03107 AtCoder Beginner Contest 120 - Unification_1094
There are N cubes stacked vertically on a desk. You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is `0`, and blue if that character is `1`. You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent...
def smaller(x, y): if x < y: return x else: return y S = raw_input() count = [0, 0] for i in range(0, len(S)): count[int(S[i])] += 1 print smaller(count[0], count[1])*2
1Python2
{ "input": [ "0", "0011", "11011010001011", "1", "0010", "11011010001001", "11001010001001", "11001000001001", "1010", "01001000101000", "11011011111101", "0000", "1000", "11001000101001", "1011", "11001010101001", "1100", "01001010101001", "...
5ATCODER
p03107 AtCoder Beginner Contest 120 - Unification_1095
There are N cubes stacked vertically on a desk. You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is `0`, and blue if that character is `1`. You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent...
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; int c0,c1 =0; for(char i: S) { if(i == '0') ++c0; else ++c1; } cout << 2 * min(c0,c1) << endl; }
2C++
{ "input": [ "0", "0011", "11011010001011", "1", "0010", "11011010001001", "11001010001001", "11001000001001", "1010", "01001000101000", "11011011111101", "0000", "1000", "11001000101001", "1011", "11001010101001", "1100", "01001010101001", "...
5ATCODER
p03107 AtCoder Beginner Contest 120 - Unification_1096
There are N cubes stacked vertically on a desk. You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is `0`, and blue if that character is `1`. You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent...
S = input() print(min(S.count('1'),S.count('0'))*2)
3Python3
{ "input": [ "0", "0011", "11011010001011", "1", "0010", "11011010001001", "11001010001001", "11001000001001", "1010", "01001000101000", "11011011111101", "0000", "1000", "11001000101001", "1011", "11001010101001", "1100", "01001010101001", "...
5ATCODER
p03107 AtCoder Beginner Contest 120 - Unification_1097
There are N cubes stacked vertically on a desk. You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is `0`, and blue if that character is `1`. You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent...
import java.util.Scanner; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); String input = sc.next(); String[] inputSplit = input.split(""); int num0 = 0; for (int i = 0; i < inputSplit.length; i++) { if (inputSplit[i].equals("0")) { nu...
4JAVA
{ "input": [ "0", "0011", "11011010001011", "1", "0010", "11011010001001", "11001010001001", "11001000001001", "1010", "01001000101000", "11011011111101", "0000", "1000", "11001000101001", "1011", "11001010101001", "1100", "01001010101001", "...
5ATCODER
p03254 AtCoder Grand Contest 027 - Candy Distribution Again_1098
There are N children, numbered 1, 2, ..., N. Snuke has decided to distribute x sweets among them. He needs to give out all the x sweets, but some of the children may get zero sweets. For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets. Snuke is trying to maximize the number of happy ...
INF = 999999999999999999L EPS = 1e-12 def read(): return raw_input().strip() def read_ints(): return map(int,read().split()) n,x = read_ints() a = read_ints() a.sort() ans = 0 for i in range(len(a)): if i == len(a)-1 and x == a[i]: ans += 1 elif i != len(a)-1 and a[i] <= x: ans += 1 ...
1Python2
{ "input": [ "4 1111\n1 10 100 1000", "3 70\n20 30 10", "3 10\n20 30 10", "2 10\n20 20", "3 70\n20 30 5", "3 10\n20 18 10", "2 10\n29 20", "4 1111\n1 5 100 1000", "4 1111\n-1 2 110 1000", "3 70\n20 49 5", "3 10\n20 9 10", "2 10\n30 20", "3 70\n4 49 5", "3 10\n20...
5ATCODER
p03254 AtCoder Grand Contest 027 - Candy Distribution Again_1099
There are N children, numbered 1, 2, ..., N. Snuke has decided to distribute x sweets among them. He needs to give out all the x sweets, but some of the children may get zero sweets. For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets. Snuke is trying to maximize the number of happy ...
#include<bits/stdc++.h> using namespace std; int main() { int N, x; cin>>N>>x; vector<int> a(N); for(int i=0; i<N; i++) cin>>a[i]; sort(a.begin(), a.end()); int n; for(n=0; n<N && x>0; n++) x-=a[n]; if(x!=0) n-=1; cout<<n<<endl; return 0; }
2C++
{ "input": [ "4 1111\n1 10 100 1000", "3 70\n20 30 10", "3 10\n20 30 10", "2 10\n20 20", "3 70\n20 30 5", "3 10\n20 18 10", "2 10\n29 20", "4 1111\n1 5 100 1000", "4 1111\n-1 2 110 1000", "3 70\n20 49 5", "3 10\n20 9 10", "2 10\n30 20", "3 70\n4 49 5", "3 10\n20...
5ATCODER