anchor
stringlengths
10
14.3k
positive
stringlengths
10
14.9k
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int sum = 0; for (int i = 0; i < n; i++) { sum = sum + a[i]; } int m; cin >> m; int i = 0; for (i; i < m; i++) { int l, r; cin >> l >> r; ...
##n = int(input()) ##a = list(map(int, input().split())) ##print(' '.join(map(str, res))) n = int(input()) a = list(map(int, input().split())) tot = 0 for e in a: tot += e m = int(input()) for i in range(m): [l , r] = list(map(int, input().split())) if l <= tot and tot <= r: print(tot) ex...
from fractions import Fraction def higher(x1, y1, x2, y2): if x1 == 0: if x2 == 0: return def min_days(p, q, pr): ma = max(a for a, b in pr) mb = max(b for a, b in pr) pr.sort(key=lambda t: (t[0], -t[1])) ch = [(0, mb)] for a, b in pr: if a == ch[-1][0]: ...
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-1; bool is_zero(long double a) { if (a > eps) return false; if (a < -eps) return false; return true; } bool zgodne(pair<long double, long double> a, pair<long double, long double> b, pair<long double, long double> c) { if (is_z...
#include<bits/stdc++.h> #define ll long long using namespace std; ll n,m,i,j,k,now,num,ans,v[1010],a[1010],f[2][310][310],p=1e9+7; int main(){ scanf("%lld",&n); m=n*2; for(i=1;i<=m;i++)scanf("%lld",&a[i]); for(i=1;i<=m;i+=2)if(a[i]==-1||a[i+1]==-1){ num+=(a[i]==-1)&&(a[i+1]==-1); if(a[i]!=-1)v[a[i]]=1; else ...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper...
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using ull = unsigned long long; using pii = pair<int, int>; using pll = pair<ll, ll>; using vi = vector<int>; using vll = vector<ll>; using db = double; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, -1, 0, 1}; ll gcd(ll a, ll b) { ...
import math def maxSubArraySum(a, size): max_so_far = int(-1e-19) max_ending_here = 0 st = 0 ef = 0 et = 0 sf = 0 for i in range(0, size): max_ending_here = max_ending_here + a[i] if (max_so_far < max_ending_here): max_so_far = max_ending_here ef = i...
n=input() x=0 y=0 def dis(x,y,a,b): return (x-a)**2+(y-b)**2 lst=[] ans=[0]*n lst2=[] lst4=[] ans2=[0]*n for i in range(0,n): a,b=map(int,raw_input().split()) lst4.append((a,b)) lst.append((a*a,a,b,i)) lst2.append((b*b,a,b,i)) lst.sort(reverse=True) lst2.sort(reverse=True) for i in range(0,n): a=lst[i][1] ...
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; long long X, Y; int ans[maxn]; pair<pair<int, int>, int> s[maxn]; int main() { int N, x, y, i; scanf("%d", &N); for (i = 1; i <= N; i++) { scanf("%d%d", &x, &y); s[i] = make_pair(make_pair(x, y), i); } while (true) { random_shu...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual soluti...
#include <bits/stdc++.h> using namespace std; const int maxn = 252; const int inf = 0x3f3f3f3f; const double eps = 1e-2; int a[maxn]; int main() { int n; while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) { int sum = 0; int mmax = -inf; for (int k = 0; k < 250; k++) { scanf("%d", &...
#include<bits/stdc++.h> using namespace std; int main(){ string S; cin>>S; int cnt=0,res=0; for(int i=0;i<S.length();++i){ char ch=S[i]; if(ch=='A'||ch=='T'||ch=='C'||ch=='G'){ cnt++; res=max(res,cnt); } else cnt=0; } cout<<res; return 0; }
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Pattern p = Pattern.compile("[ACGT]+"); Scanner s = new Scanner(System.in); int result = 0; String inS = s.next(); Matcher m = p.matcher(inS); for(...
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; struct Info { int a, b; Info operator+(const Info& B) const { return (Info){a + B.a, max(b + B.a, B.b)}; } }; struct Segment_Tree { const static int NODE = N << 2; bool setv[NODE]; Info A[NODE]; void pushdown(int o, int L, int R) { ...
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class DivCmb_502G implements Runnable { public static void ma...
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class Main { private static StreamTokenizer inputReader = new Stre...
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double eps = 1e-8; const int mod = 1e9 + 7; const int inf = 1061109567; const int dir[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; map<int, int> mp; int main() { int n, x; long long ans = 0; cin >> n; for (int i = 0; i < n; i++) { ...
import static java.lang.Math.*; import java.io.*; import java.util.*; import java.math.*; public class Main { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st==null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) {} ...
#include <bits/stdc++.h> using namespace std; int main() { long long T, S, q, ans = 0; cin >> T >> S >> q; while (1) { if (S >= T) break; ans++; S = S + S * (q - 1); } cout << ans << endl; }
// Test .. import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in ...
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 * 1000 + 100; long long d[maxn]; int main() { ios::sync_with_stdio(false); long long n, m; cin >> n >> m; long long p = 0; long long Cn3 = (n * (n - 1) * (n - 2)) / 6; for (int i = 0; i < m; ++i) { int t1, t2; cin >> t1 >> t2; t...
''' codeforces: Chemical using disjoint sets ''' n,m=map(int,raw_input().split()) if m==0: print 1 exit(0) global store store=[-1 for _ in range(n)] def find_root(x): if store[x]>=0: store[x]=find_root(store[x]) return store[x] else: return x def merge(x,y): root1=fi...
import java.util.* ; import java.io.* ; import java.util.regex.* ; import java.text.* ; import java.math.* ; public class Main { public static void main( String[] args ) { InputStream isObj ; OutputStream osObj ; InputReader irObj ; PrintWriter pwObj ; String inputFileName , outputFileName ; File fObj1 , ...
import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public cla...
#include <bits/stdc++.h> using namespace std; const int p = 1e9 + 7; struct Matrix { int n, m; long long a[105][105]; Matrix(int x, int y) { n = x, m = y; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) a[i][j] = 0; } } void init() { for (int i = 1; i <= min(n, m); i++) a[i][...
#include <bits/stdc++.h> using namespace std; int i, j, k, n, m; int id[300010], f[300010], g[300010]; double b[300010], an; inline bool cc1(const int &A, const int &B) { return b[A] < b[B]; } int getg(int x) { return g[x] == x ? x : g[x] = getg(g[x]); } int getf(int x) { return f[x] == x ? x : f[x] = getf(f[x]); } int...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; public class Main223E { private FastScanner in; private PrintWriter out; ...
from heapq import heappush, heappop h, w = map(int, input().split()) def get_area(): mp = ["#" * (w + 2)] for _ in range(h): mp.append("#" + input() + "#") mp.append("#" * (w + 2)) return mp areas = [get_area()] times = {} n = int(input()) for i in range(n): times[int(input())] = i + 1 ...
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Loc{ void set(int arg_row,int arg_col){ row = arg_row; col = arg_col; } int row,col; };...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class Main { private void solve() throws IOException { int N = nextInt(); long a[] = new long[N]; for (int i = 0; i < N; ...
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const int INF = 1000 * 1000 * 1000 + 7; const int MOD = 1000 * 1000 * 1000 + 7; const int MAX = 1000 * 100 + 47; struct node { long long ss[6 + 1][12]; }; long long a[MAX]; node t[MAX]; vector<long long> ss[6 + 4]; int n; void calcs() { for...
count = 0 def merge(a, left, mid, right): global count l = a[left:mid] r = a[mid:right] l.append(10**9+1) r.append(10**9+1) i = 0 j = 0 for k in range(left, right): count += 1 if l[i] <= r[j]: a[k] = l[i] i += 1 else: a[k] = ...
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; public class Main { private static FastScanner sc = new FastScanner(); static int cnt = 0; public static void main(String[] args) { int n = sc.nextInt(); int[] A = new int[n]; for(int i=0; i<n; i++) { A[i] ...
import java.io.*; import java.util.Arrays; import java.util.InputMismatchException; public class Cf2802D { private static InputReader in = new InputReader(System.in); private static OutputWriter out = new OutputWriter(System.out); private static void solve() throws Exception{ int n = in.readInt...
#include <bits/stdc++.h> using namespace std; struct seg { int long long sum, sum2; seg(int long long sum = 0, int long long sum2 = 0) : sum(sum), sum2(sum2) {} }; int long long x, y; vector<seg> t; void update(int long long l, int long long r, int long long pos, int long long v, int long long w) { if...
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class D { static final int MAXV = 500_000; public static void main(String... args) throws Exception { FastScan sc = new FastS...
# Your circuit's dead, there's something wrong! # Can you hear me, Major Tom?... m, k, n, s = map(int, input().split()) fl = [int(x) for x in input().split()] seq = [int(x) for x in input().split()] d = {} p = {} for i in seq: d[i] = d.get(i, 0) + 1 p[i] = d[i] f = False collect = s keys = set(d.keys()) ...
while 1: s = raw_input() if s == "#": break l = len(s) ps = [(int(c)-1) % 3 for c in s] dpl = [1e9]*l; dpr = [1e9]*l dpl[0] = dpr[0] = 0 for i in xrange(1, l): if ps[i-1] <= ps[i]: dpl[i] = dpr[i-1] if ps[i] <= ps[i-1]: dpr[i] = dpl[i-1] dpl[i]...
import java.io.*; import java.util.*; import java.lang.ArrayIndexOutOfBoundsException; import java.math.BigInteger; /** * @author yoshikyoto */ class Main { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while(true){ char[] s...
#include <bits/stdc++.h> using namespace std; int const MAXN = 2e5 + 10; int n, m, T; int a[MAXN]; bool cmp(int a, int b) { return a > b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <=...
#------------------------template--------------------------# import os import sys from math import * from collections import * from fractions import * from bisect import * from heapq import* from io import BytesIO, IOBase def vsInput(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') BUF...
from sys import stdin n, m = [int(i) for i in stdin.readline().split()] ori = [int(i) for i in stdin.readline().split()] pre = [-1] * n ans = list() for i in range(n-1): if ori[i] != ori[i+1]: pre[i+1] = i else: pre[i+1] = pre[i] for i in range(m): l, r, x = [int(i) for i in stdin.readline().split()] tem = r -...
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; import java.lang.*; public class P622C{ static long mod=1000000007; public static void main(String[] args) throws Exception{ InputReader in = new InputReader(System.in); Print...
import java.util.Scanner; public class Main { public static void main (String[] args) throws java.lang.Exception { Scanner myScanner = new Scanner(System.in); int n = myScanner.nextInt(); myScanner.nextLine(); for(int i=0;i<n;i++){ String line = m...
n = int(input()) while n!=0: l = input() if len(l)<=10: print(l) else: newl = [] newl.append(l[0]) newl.append(len(l[1:-1])) newl.append(l[-1]) nl = "".join([str(ele) for ele in newl]) print(nl) n-=1
#include <iostream> #include <cstdio> #define REP(i,n) for(int i=0; i<(int)(n); i++) using namespace std; char buff[512]; int main(){ while(scanf("%s", buff), buff[0] != '#'){ int cnt = 0; int var[3]; int neg = 1; bool ans = false; for(int i = 0; buff[i]; i++){ if(buff[i] == '~'){ neg =...
import java.util.Scanner; class Main { private static char[] expr; private static int ptr; private static void consume(char c) { assert expr[ptr] == c : "expected " + c + ", but got " + expr[ptr]; ptr++; } private static boolean expression() { do { consume('(')...
import java.util.Arrays; import java.util.Scanner; import java.util.stream.IntStream; public class Main{ static IntStream REPS(int v){return IntStream.range(0,v);} static IntStream REPS(int l,int r){return IntStream.rangeClosed(l,r);} static IntStream INS(int n) {return REPS(n).map(i->getInt());} static Scanner s=...
#include<iostream> #include<algorithm> using namespace std; int main(){ int N; cin>>N; int jump[N]; for(int i=0;i<N;i++){ cin>>jump[i]; } int jum_dis=0; for(int i=0;i<N-1;i++){ jum_dis=max(jum_dis,jump[i]); if(jum_dis<10){cout<<"no\n";return 0;} jum_dis -= 10; } jum_dis=0; for(int ...
// これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました import java.io.*; import java.math.BigDecimal; import java.math.BigInteger; import java.util.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Solution { static PrintWriter fop = new PrintWriter(System.ou...
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, l, t, r, s, m, n, a, b, c, d, e, f, g, h, u, v; scanf("%d %d", &c, &d); scanf("%d %d", &n, &m); scanf("%d", &k); a = n * m; a = a - k; if (a > 0) { e = a / n; h = 100000000; g = 0; if (a % n > 0) ++e; for (i = 0; i...
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) #define repb(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl #define int long long #define fi first #define se second using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; ...
def ans(N): # N:foat n=int(N) d=N-n ans_l=bin(n)[2:] ans_r='' for _ in range(4): ans_r+=str(int(d*2)) d=d*2-int(d*2) if n>=256 or d!=0: return 'NA' else: return '0'*(8-len(ans_l))+ans_l+'.'+ans_r while True: INP=float(input()) if INP<0: break prin...
#!/usr/bin/env python3 # http://codeforces.com/contest/633/problem/C from collections import deque # import sys # sys.setrecursionlimit(10001) n = int(input()) s = input() m = int(input()) words = [] for i in range(0, m): words.append(input()) dict_words = {} for word in words: dict_words[word.lower()[::-1...
#include <bits/stdc++.h> using namespace std; int n, m, cur, ch; int cntNodes = 1; int trie[1001000][26]; int DP[1001000]; int isWord[1001000]; string str, auxStr; vector<string> words; void insert(string str, int idx) { cur = 1; for (int i = 0; i < str.length(); i++) { ch = (str[i] >= 'a') ? str[i] - 'a' : str...
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main ( String args[] ) throws Exception { (new P7()).solve(); } } class P7 { int n, wantedTime; char a[]; int getTime ( int startCnt ) { int cnt = -startCnt, lastH = -1; ArrayList<Integer> first = new ArrayLi...
#include <bits/stdc++.h> int n, t, len; char s[500005]; bool check(int x) { int num = x, End = 0; for (int i = 0; i < n; i++) { if (s[i] == 'H') num--, End = i; else if (s[i] == 'S') { num++; if (!num) End = i; } } if (num < 0) return 0; int last = -1, ans = 0; num = x; for (...
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; public class Main{ Scanner sc=new Scanner(System.in); int INF=1<<28; double EPS=1e-12; int n, m, nb, k; int[] us, vs, ws; int s,...
// #include<iostream> // #include<map> // #include<algorithm> // #include<queue> // #include<vector> // using namespace std; // typedef pair<int,int> P; // typedef pair<int,P> P2; // int n,m,l,k; // int u,v,c; // int s,g,t; // int b[7]; // int dis[51][1<<7][10],vis[51][1<<7]; // vector<P> G[51]; // const int INF...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.List; import java.util.Arrays; import java.util.Scanner; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public s...
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): t = int(input()) for _ in range(t):...
import java.io.*; import java.util.*; public class E { BufferedReader br; StringTokenizer in; PrintWriter out; public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken()...
#include <bits/stdc++.h> inline int gd(int x) { int s = 0; for (; !(x & 1); x >>= 1) s++; return s; } double s[201][201]; int main() { int x, k, p_i; scanf("%d %d %d", &x, &k, &p_i); double p = (double)p_i / 100, q = 1 - p; for (register int i = 0; i <= k; i++) s[0][i] = gd(x + i); for (register int j =...
import java.io.*; import java.math.*; import java.security.KeyStore.Entry; import java.util.*; public class CODEFORCES { private InputStream is; private PrintWriter out; void solve() { int n = ni(), p = ni(), k = ni(); int a1[] = new int[n + 1], a2[] = new int[n + 1]; int a3[] = new int[n + 1]; int tem = n...
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 10; int dp[2][MAXN][55][55]; bool quest[2][MAXN]; int n, p, k; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> p >> k; int m, val; cin >> m; for (int i = 0; i < m; i++) { cin >> val; quest[0][val] = ...
#include <bits/stdc++.h> using namespace std; int main() { int n, x; cin >> n >> x; int sum = 0; for (int i = 0; i < n; i++) { int x; cin >> x; sum += x; } if (sum + n - 1 == x) cout << "YES"; else cout << "NO"; }
import java.util.Scanner; public class JpCross { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int x = input.nextInt(); int[] arr = new int[n]; int k = 0; for(int i = 0; i < n; i++){ arr[i] = i...
from fractions import Fraction q, p = list(map(int, input().split())) is_equal_to = Fraction(q, p) n = int(input()) a = list(map(int, input().split())) pos = -1 def continued_fraction(n, a): if n == 1: return a[0] score = a[-1] for i in range(n - 2, 0, -1): score = a[i] + Fraction(1, scor...
#include <bits/stdc++.h> using namespace std; int n; long long a, b; long long v[1000]; bool verif(int pos, long long a, long long b) { if (pos == n + 1) { return (b == 0); } else { if (b == 0 || a / b < v[pos]) { return false; } return verif(pos + 1, b, a - v[pos] * b); } } int main() { i...
#include<bits/stdc++.h> using namespace std; int main() { double a,b; cin>>a>>b; cout<<a/b<<"\n"; }
N,A=map(int,input().split()) print(float(N/A))
import sys from array import array # noqa: F401 def input(): return sys.stdin.buffer.readline().decode('utf-8') n, m, k = map(int, input().split()) adj = [[] for _ in range(n)] dp = [[0] * (1 << n) for _ in range(1 << n)] for u, v in (map(int, input().split()) for _ in range(m)): u, v = u - 1, v - 1 a...
import java.util.*; public class E { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(), m = s.nextInt(), k = s.nextInt(); int[][] a = new int[m][]; int[][] count = new int[1 << n][1 << n]; for (int i = 0; i < m; ++i) { int u = s.nextInt() - 1, v = s.nextInt(...
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; import java.math.BigInteger; import java.util.HashMap; /** * Actual solution is at the top * @author Erasy...
#include <bits/stdc++.h> using namespace std; bool check(long long int mid, long long int a[], long long int b[], long long int k, long long int n) { for (int i = 0; i < n; i++) { long long int req = a[i] * mid; long long int need = max((long long int)0, req - b[i]); if (k >= need) { k -=...
#include <bits/stdc++.h> using namespace std; const int MAXSIZE = 30000020; int bufpos; char buf[MAXSIZE]; void init() { buf[fread(buf, 1, MAXSIZE, stdin)] = '\0'; bufpos = 0; } int readint() { int val = 0; for (; !isdigit(buf[bufpos]); bufpos++) ; for (; isdigit(buf[bufpos]); bufpos++) val = val * 10 + b...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.Collec...
#include <bits/stdc++.h> using namespace std; const long long mod[5] = {99998363, 99998377, 99998413, 99998417, 1000000007}; const int MAXN = 200005; long long hashed[MAXN][5], pow2[MAXN][5]; int sum[MAXN][2]; bool cksame(int l1, int l2, int len) { if (len == 0) return true; for (int it = 0; it < 5; ++it) { lon...
import sys input = sys.stdin.readline n=int(input()) t=input().strip() q=int(input()) ZEROS=[0]*n ZERO_ONE=[] ONECOUNT=[0]*n ind=0 count=0 for i in range(n): ZEROS[i]=ind if t[i]=="0": ind+=1 ONECOUNT[i]=count%2 ZERO_ONE.append(count%2) count=0 else: count+=1 ...
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<vector<int> > arr(n, vector<int>(m)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> arr[i][j]; vector<pair<int, int> > ans; vector<...
def solve(Amat, n, m): actions = [] Bmat = [[0 for _ in range(m)] for _ in range(n)] for i in range(n - 1): for j in range(m - 1): if Amat[i][j] and Amat[i + 1][j] and Amat[i][j + 1] and Amat[i + 1][j + 1]: Bmat[i][j] = 1 Bmat[i + 1][j] = 1 ...
n =int(input()) a = list(map(int,input().split())) b = list(map(int,input().split())) dp = [0] * (n//2+1) dp2 = [0] * (n//2+1) fi = 0 se = 0 for i in range(n): fi +=a[i] * i fi +=b[-i-1] * (i+n) dp[0] = fi for i in range(n): if i != 0: se +=a[-i] * (i+n) se +=b[i] * (i+1) dp2[0] = se gou = sum(a)+ sum(b) -...
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 300000; int n; int g[2][MAXN]; long long suffsum[2][MAXN + 1]; long long wsuffsum[2][MAXN + 1]; long long dp[2][MAXN + 1]; void run() { scanf("%d", &n); for (int x = (0); x < (2); ++x) for...
#include <bits/stdc++.h> using namespace std; int main() { int m, n; cin >> n >> m; long long a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } long long w1 = 0; long long w3 = 0; long long w4 = 0; for (long long aa = 2; aa <= 2 * m - 1; aa++) { long long s; if (a[0] % aa <= (aa - 1) / 2)...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOExcept...
/* ID: tommatt1 LANG: JAVA TASK: */ import java.util.*; import java.io.*; public class cf1313d{ public static void main(String[] args)throws IOException { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); BufferedReader bf=new BufferedReader(new InputStreamReader(Syste...
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 50; int n, m, k, a[MAXN]; struct Node { int l, r; } z[MAXN]; void readInput() { int t; scanf("%d%d%d", &m, &t, &k); vector<int> v; for (int i = 1; i <= m; i++) { scanf("%d%d", &z[i].l, &z[i].r); z[i].r++; v.push_back(z[i].l); ...
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; while (T--) { int n, m; cin >> n >> m; vector<int> t(n); for (int i = 0; i < n; i++) cin >> t[i]; long long sum = 0; int ans = 0; for (int i = 0; i < n; ...
a = [] total = 0 def binary_search(n, l): first = 0 last = len(l)-1 found = False mid = (first+last)//2 while(first<=last and not found): mid = (first+last)//2 if l[mid] == n: found = True elif n <l[mid]: last = mid - 1 else: first ...
import java.io.*; import java.util.*; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { char[] s = ns().toCharArray(); int n = s.length; int mod = 998244353; int als = 1; for(int i = 0;i < n;i++){ if(s[i] != s[0])als = 0; } i...
#include <bits/stdc++.h> using namespace std; #define int long long #define maxn 200050 #define mod 998244353 int n, ans, f[maxn][3][3][2], sum; char a[maxn]; int read() { int x = 0, k = 1; char c; c = getchar(); while(c < '0' || c > '9') { if(c == '-') k = -1; } while(c >= '0' && c <= '9') x = x * 10 ...
#include <bits/stdc++.h> using namespace std; int n; int m; int k; int q; struct st { int x1; int y1; int x2; int y2; int id; }; vector<st> ev[200002]; vector<int> v[200002]; int seg[200002 * 4]; inline void add(int b, int l, int r, int ll, int x) { if (l <= ll && ll < r) { if (l + 1 == r) { seg[b...
import java.io.FileNotFoundException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.File; import java.io.Writer; import java.util.List; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileReader; import java.util.Arrays; import java.u...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Iterator; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.Writer; import java.io.OutputStreamW...
from fractions import gcd def gcdL(L): g=L[0] for i in range(1,len(L)): if g==1: return(g) g=gcd(g,L[i]) return g def play(A,i): A[i]-=1 g=gcdL(A) return [x//g for x in A] def cnt(A): odd,even=0,0 for a in A: if a%2: odd+=1 else: ...
#include <bits/stdc++.h> using namespace std; int cmp(pair<int, int> a, pair<int, int> b) { return a.first == b.first ? a.second < b.second : a.first < b.first; } int main() { ios::sync_with_stdio(false); int T; cin >> T; while (T--) { int n; cin >> n; vector<pair<int, int>> all(n); priority_q...
import java.io.*; import java.util.*; public class Main { private static void execute(ContestReader reader, PrintWriter out) { int t = reader.nextInt(); for (int i = 0; i < t; i++) { int n = reader.nextInt(); int[] ms = new int[n]; int[] ps = new int[n]; for (int j = 0; j < n; j++) { ...
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class TaskD { /** * @param args */ public static void main(String[] args) { TaskD task = new TaskD(); task.read(); } int m, n, p; ArrayList<Map<Integer,...
In=lambda:map(int,raw_input().split()) n,m,p=In() a=In() c=In() b={};ans=[];s=[];d=m for x in a:b[x]=0 for x in c:b[x]=0 for x in c:b[x]+=1 for q in range(p): for i in range(q,n,p): s.append([a[i],i+1]) for i in range(min(m,n)): d+=-1 if b[s[i][0]]>0 else 1 b[s[i][0]]-=1 for i in range(m,n): if ...
from collections import deque R,C,ay,ax,by,bx = map(int,input().split()) MOD = INF = 10**9+7 dists = [[INF]*C for i in range(R)] dists[ay][ax] = 0 ptns = [[0]*C for i in range(R)] ptns[ay][ax] = 1 q = deque([(0,ax,ay)]) dxs = [1,0,-1,0] dys = [0,1,0,-1] ans_d = None while q: d,x,y = q.popleft() if ans_d is not...
#include <bits/stdc++.h> using namespace std; using lint = long long int; struct Pos { int x, y, c; }; int R, C, ax, ay, bx, by, cost[555][555]; lint card[555][555], MOD = (lint)1e9 + 7; const int dx[4] = {0,1,0,-1}; const int dy[4] = {1,0,-1,0}; void update(int x, int y, int nx, int ny, int nc, queue< Pos >& que)...
from math import * n,m,k=map(int,raw_input().split()) def c(n,k): if k>n: return 0 return factorial(n)/(factorial(k)*factorial(n-k)) print (c(n-1,2*k)*c(m-1,2*k))%(10**9+7)
import java.io.*; import java.util.*; public class CF129E { static int M = 1000000007; static int add(int a, int b) { return (a + b) % M; } static int mul(int a, int b) { return (int) ((long) a * b % M); } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(ne...
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.ConnectException; import java.rmi.dgc.Lease; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.u...
#include<iostream> #include<cstring> using namespace std; char board[105][105]; int dp[105][105]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>board[i]+1; memset(dp,127,sizeof(dp)); dp[1][1] = (board[1][1]=='#'); for(int i=1;i<=n;i++)...
#include <bits/stdc++.h> using namespace std; int n, a[100], b, mx; int main() { cin >> n >> b; for (int i = 0; i < n; i++) { cin >> a[i]; mx = max(mx, a[i]); } int v = 0; for (int i = 0; i < n; i++) { v += mx - a[i]; } if (v > b) { cout << -1 << endl; return 0; } b -= v; for (in...
import java.util.*; public class Main{ public static void main(String[] args){ Scanner s = new Scanner(System.in); int n = s.nextInt(); double k = s.nextDouble(); double[] a = new double[n]; double max = Double.MIN_VALUE; for(int i = 0; i < n; i++...
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public stat...
#include <bits/stdc++.h> using namespace std; struct Y_X { int cy, cx; } counter; Y_X yazn(string s) { counter.cy = 0; counter.cx = 0; int x = s.size(); for (int i = 0; i < x; i++) { if (s[i] == 'y') counter.cy++; else counter.cx++; } return counter; } void Y(int x) { for (int i = 0;...
import java.io.IOException; import java.util.ArrayDeque; import java.util.Deque; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { new Main().run(); } private void run() throws IOException { Scanner scanner = new Scanner(...
# from line_profiler import LineProfiler from collections import deque from sys import stdin def main(): while(True): W, H = map(int, stdin.readline().split()) if not W: break tx,ty = map(int, stdin.readline().split()) kx,ky = map(int, stdin.readline().split()) ma = [[False]...
n = int(input()) if n == 1: print(1) else: adj = [[] for i in range(n+10)] s = input().split() for i in range(2,n+1): pi = int(s[i-2]) adj[i].append(pi) adj[pi].append(i) num = 1 curr = [1] nextcurr = [] disco = [1] visited = {1:True} while num < ...
#include <bits/stdc++.h> using namespace std; int dp[100009]; vector<int> adj[100009]; int vis[100009]; int totleaf; int calc(int s) { vis[s] = 1; int lf = 0, loc = 0; for (int i = 0; i < adj[s].size(); i++) { if (!vis[adj[s][i]]) { loc = 1; lf += calc(adj[s][i]); } } if (!loc) lf += 1; ...
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int times = 1; for (int i = 0; i < times; i++) { solve(); cout << '\n'; } return 0; } int a, b, c, d, e, f, g, h; int arr[500000 + 50], arr1[500000 + 50], arr2[500000 + 5...
n = int(input()) l = 0 r = 0 books = {} z = input().split() books[z[1]] = 0 for i in range(n-1): z = input().split() if z[0]=='L': l -= 1 books[z[1]] = l elif z[0]=='R': r += 1 books[z[1]] = r else: print(min(r-books[z[1]], books[z[1]]-l))
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; @SuppressWarnings("unchecked") class Main{ static List<Integer> edges[]; static long[] m; static long[] w; static long[] v; static long dfs(int p){ w[p] += m[...
#include <algorithm> #include <cstring> #include <cstdio> #include <vector> typedef long long ll; struct data { ll val, w, lim; } arr[55]; ll sz[55], sum[55], dp[200005]; std::vector<int> son[55]; void dfs(int u) { sz[u] = 1; for (int v : son[u]) { dfs(v); sz[u] += sz[v]; sum[u] += sum[v]; } } int ma...
#include <bits/stdc++.h> using namespace std; long long rdtsc() { long long tmp; asm("rdtsc" : "=A"(tmp)); return tmp; } inline int myrand() { return abs((rand() << 15) ^ rand()); } inline int rnd(int x) { return myrand() % x; } const int INF = (int)1e9 + 1; const long double EPS = 1e-9; int gcd(int a, int b) { ...
import static java.lang.Math.PI; import static java.lang.Math.abs; import static java.lang.Math.acos; import static java.lang.Math.cos; import static java.lang.Math.min; import static java.lang.Math.sin; import static java.lang.Math.sqrt; import static java.lang.System.in; import static java.lang.System.out; import j...
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; const int mod = 998244353; long long qpow(long long a, long long b) { long long ans = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } long long gcd(long long a, ...
import java.util.Scanner; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Andy Phan */ public class p1173e { static int MOD = 998244353; static int n; ...
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int A; vector<int> tail_vec(N, -1); auto comp = greater<int>(); for (int i = 0; i < N; ++i) { cin >> A; *upper_bound(tail_vec.begin(), tail_vec.end(), A, comp) = A; } auto end = lower_bound(tail_vec.begin(), tail_vec.end(), -1, com...
from bisect import bisect_right n = int(input()) color = [0] * (n+1) for i in range(n): a = int(input()) j = bisect_right(color, a) color[j-1] = a+1 ans = 0 for t in color: if t != 0: ans += 1 print(ans)
#include <bits/stdc++.h> using namespace std; typedef long long signed int LL; #define incID(i, l, r) for(LL i = (l) ; i < (r); ++i) #define inc(i, n) incID(i, 0, n) #define FI first #define SE second // ---- ---- const int MOD = 998244353; int n, BIT[200001]; pair<int, int> p[200000]; LL e2[200001]; int su...
import java.io.*; import java.nio.CharBuffer; import java.util.*; public class Main { private static final int MOD = 998244353; public static void main(String[] args) { SimpleScanner scanner = new SimpleScanner(System.in); PrintWriter writer = new PrintWriter(System.out); int n = sca...
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <map> #include <queue> #include <deque> #include <stack> #include <algorithm> #include <cstring> #include <functional> #include <cmath> #include <complex> using namespace std; #define rep(i,n) for(int i=0;i<(n);++i) #define rep1(i,n) for(in...
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { new Main().run(); } void run() throws FileNotFoundException { solver(); } @Supp...
import java.io.*; import java.util.*; public class E { public static void main (String[] args) { new E(); } E() { Scanner s = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); System.err.println(""); int n = s.nextInt(), m = s.nextInt(), k = s.nextInt(); if(k == n || m == n*(n-1)/2)...
#include <bits/stdc++.h> using namespace std; int n, m, k; int a[303]; int b[303]; int c[303]; int g[303][303]; inline void dfs(int v) { c[v] = 1; for (int i = 0; i < n; ++i) if (!c[i] && g[v][i]) { printf("%d %d\n", v + 1, i + 1); g[v][i] = g[i][v] = 0; --m; dfs(i); } } int main() {...
for e in iter(input,'0'): n=int(e);m,f=[[0]*n for _ in[0]*n],'{:4}'*n s=lambda x:-~x%n j=n//2;i=j+1 m[i][j]=1 for k in range(2,n*n+1): i,j,t=s(i),s(j),j if m[i][j]:i,j=s(i),t m[i][j]=k for r in m:print(f.format(*r))
import static java.lang.Math.atan2; import static java.lang.Math.sqrt; import static java.util.Arrays.deepToString; import java.util.Scanner; public class Main { public static void main(String[] args) { new Main().run(); } void tr(Object... os) { System.err.println(deepToString(os)); } Scanner sc = new Scan...
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int a[n][n]; int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { scanf("%d", &a[i][j]); if (i == j) { ans = ans + a[i][j]; } } } int q; int x; scanf("%d", &q); int t; while (q--) { ...
import java.util.*; import java.io.IOException; import java.io.StreamTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { static int [][] mat=null; static int n; public static void main(String[] args) throws IOException { StreamTokenizer st = new StreamTokenizer(...
#include <bits/stdc++.h> using namespace std; const long long maxN = 1e18; const long long minN = -1e18; const int INF = 2e9; const long long MOD = 1e9 + 7; const long long MOD1 = 998244353; const int baseHash = 331; const int bigNumLength = 5000; const long double PI = acos(-1); const long long limit = 2e5 + 5; const ...
import java.io.*; import java.util.*; public class C { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; static class Pair { int r, b; public Pair(int r, int b) { this.r = r; this.b = b; } } static int max = 0; static class Options { List<Pair> lst = new ArrayList<>(60)...
#include <bits/stdc++.h> using namespace std; inline int rd() { int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return x * f; } const int MAXN = 300; const double eps = 1e-9; int N, l[MAXN], r[MAX...
import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Locale; import java.util.Scanner; import static java.lang.Math.max; import static java.lang.Math.min; public class E { static int n, from, to; static int[] left, right; static double[][][] res; stati...
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n; vector<int> e[maxn]; int ans[maxn]; int have[maxn][27]; void dfs(int cur, int from) { for (int i = 0; i < e[cur].size(); i++) { int t = e[cur][i]; if (t == from) continue; dfs(t, cur); for (int j = 0; j < 26; j++) have[cur][...
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.u...
#include <bits/stdc++.h> using namespace std; int main() { long int n; long int qtde_i, qtde_a; char ch; cin >> n; qtde_i = 0; qtde_a = 0; for (long int i = 0; i < n; i++) { cin >> ch; if (ch == 'I') { qtde_i += 1; } else if (ch == 'A') { qtde_a += 1; } } if (0) { cout ...
n=int(input()) arr=list(input()) if arr.count("I")==0: print(arr.count("A")) elif arr.count("I")==1: print(1) else: print(0)
n,a=int(input()),list(map(int,input().split()));dp=[[None]*n for _ in range(n)] for i in range(n):dp[i][i]=a[i] for j in range(1,n): for i in range(n-j):dp[i][j+i]=max(a[i]-dp[i+1][j+i],a[j+i]-dp[i][j+i-1]) print(dp[0][-1])
import java.util.*; import java.io.*; class Main{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { thi...
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.OutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.io.Writer; import j...
from sys import stdin from itertools import repeat from bisect import bisect def main(): n, m = map(int, stdin.readline().split()) dat = map(int, stdin.read().split(), repeat(10, 2 * n)) s = [0] * (m + 1) for i, x in enumerate(dat): if i & 1: s[x] -= 1 else: s[x-1...
##################################### import atexit, io, sys, collections, math, heapq buffer = io.BytesIO() sys.stdout = buffer @atexit.register def write(): sys.__stdout__.write(buffer.getvalue()) ##################################### def f(ais): m = min(ais) for j in range(len(ais)): if ais[j] == m: bre...
#include <bits/stdc++.h> using namespace std; #define max3(a,b,c) max(max(a,b),c) #define min3(a,b,c) min(min(a,b),c) #define mod 1000000007 #define mod2 998244353 #define pb push_back #define all(v) v.begin(),v.end() #define en "\n" #define MN INT_MIN #define MX 100000001 #define fr(i,a,b) for(long long int i=a;i<b;i...
import math import sys read = sys.stdin.readline n = int(read()) sub = [[] for _ in range(n)] for i in range(n): data = read().strip() for j in data.split('-'): tree = list(map(int, j.split(':')[1].split(','))) for k in range(len(tree)): tree[k] -= 1 sub[i].append(sorted(tree...
#include <bits/stdc++.h> using namespace std; int n, ja[3030], grau[3030]; vector<set<int> > ramos[3030]; set<set<int> > ori[3030]; vector<pair<int, int> > ans; vector<int> adj[3030]; void dfs(int v, int p, set<int>& pr) { pr.insert(v); for (int(i) = (0); (i) < (((int)(adj[v]).size())); (i)++) if (p != adj[v][i...
n = input() lr1 = [map(int, raw_input().split()) for i in range(n)] m = input() lr2 = [map(int, raw_input().split()) for i in range(m)] min_r1 = min(map(lambda (l, r): r, lr1)) min_r2 = min(map(lambda (l, r): r, lr2)) max_l1 = max(map(lambda (l, r): l, lr1)) max_l2 = max(map(lambda (l, r): l, lr2)) print max(0, max...
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } long long int m, cnt = 0, maxi = 0; cin >> m; long long int c[m], d[m]; for (int i = 0; i < m; i++) { cin >> c[i] >> d[i]; } ...
#!/usr/bin/env python3 from collections import defaultdict DEBUG = False def main(): if DEBUG: test() n = int(input()) paths = cycles(n) print(len(paths)) for p in paths: print('%d %s' % (len(p), ' '.join([str(v) for v in p]))) def cycles(n): """Builds a set of cycles fo...
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.StringTokenizer; public class abcd { public static void main(String[] args) { FS in = new FS(); ...
import java.util.Scanner; public class alarm { public static void main(String[] args) { Scanner input = new Scanner(System.in); int x = input.nextInt(); int h= input.nextInt(); int m = input.nextInt(); int count= 0; while(h%10!=7 && m%10!=7){ m-=x; ...
#include <bits/stdc++.h> int counter = 0; void check(int hh, int mm, int snooze) { while (mm % 10 != 7 && hh % 10 != 7) { mm -= snooze; if (mm < 0) { mm = mm + 60; hh--; if (hh < 0) hh = hh + 24; } counter++; } } int main(void) { int snooze; int hh, mm; scanf("%d", &snooze); ...
#include <iostream> using namespace std; #define MAX 100000 #define INF 1 << 31 struct Node { int parent; int cost; }; int parent[MAX]; int depth[MAX]; int cost[MAX]; Node findParent(int child) { Node tmp; if (child != parent[child]) { tmp = findParent(parent[child]); parent[child] = tmp.parent; ...
import sys input = sys.stdin.readline class UnionFindWeighted: """ポテンシャル付きUnionFind""" def __init__(self, n): self.parent = [-1] * n self.weight = [0] * n self.cnt = n self.INF = 10 ** 18 def root(self, x): """頂点xの根を求める""" if self.parent[x] < 0: ...
import java.util.*; import java.io.*; public class Candy { public static void main(String [] args) { Solver s = new Solver () ; s.solve(); } static class Solver { int min_value(ArrayList <Pair> a) { int mn = Integer.MAX_VALUE; for(Pair i : a) { ...
#include <bits/stdc++.h> using namespace std; int n, cnt, ans[1000000][2]; struct data { int val, id; } a[1010]; bool cmp(const data x, const data y) { return x.val == y.val ? x.id < y.id : x.val > y.val; } void Remove() { int t = a[2].val / a[3].val; while (t) { if (t & 1) { a[2].val -= a[3].val; ...
#include <bits/stdc++.h> using namespace std; int f[1000005], a, b, n; int main() { scanf("%d", &n); for (int x, i = 1; i <= n; ++i) { scanf("%d", &x); if (x == 1) { ++a; } else { ++b; } } f[0] = 1; f[1] = 1; f[2] = 2; for (int i = 3; i <= a; ++i) { f[i] = (f[i - 1] + 1ll *...
import java.util.*; import java.io.*; public class PE { final static long mod = 1000000007; public static void main(String[] args) throws Exception { Parser in = new Parser(System.in); int n = in.nextInt(); int count2s = 0; for(int i = 0; i < n; i++) { int checker = in.nextInt(); if(checker == 2) { ...
#include <bits/stdc++.h> #define MAXN 3007 #define MOD 998244353 using namespace std; long long dp[MAXN][MAXN]; int main() { int a,b,c,d; cin>>a>>b>>c>>d; dp[a][b]=1; for(int i=a;i<=c;i++) for(int j=b;j<=d;j++) if(a!=i || b!=j) dp[i][j]=(i*dp[i][j-1]+j*dp[i-1][j]-((i-1LL)*(j-1LL)*dp[i-1][j-1])%MOD+MOD)%MOD; cout<<d...
mod=998244353 a,b,c,d=map(int,input().split()) if a==c and b==d: print(1) exit() dp=[[0]*d for _ in range(c)] tmp=1 for i in range(a,c): tmp*=b tmp%=mod dp[i][b-1]=tmp tmp=1 for i in range(b,d): tmp*=a tmp%=mod dp[a-1][i]=tmp for i in range(b,d): tmp=0 for j in range(a,c): tmp*=i tmp+=dp[j-1...
import itertools def get(permutation): ans = 0 for i in range(len(permutation)): for j in range(i + 1, len(permutation)): if permutation[j] < permutation[i]: ans += 1 return ans def check(s, pat): for i in range(len(pat)): if pa...
#include <bits/stdc++.h> using namespace std; int n, m; vector<string> a, b[20]; bool p[20] = {}; int ans[20] = {}; void rec(int tnum, int wnum, int c, int st, int pnum) { if (st >= b[tnum].size()) return; while (st < b[tnum].size() && b[tnum][st] != a[wnum]) st++; if (st >= b[tnum].size()) return; p[wnum] = tr...
#include <bits/stdc++.h> using namespace std; vector<int> l[100010], r[100010], chosenl[100010], chosenr[100010]; vector<bool> badl[100010], badr[100010]; bool monst[100010]; int ans[100010]; vector<pair<int, int> > edges[100010]; int cnt[100010], toFather[100010], fromFather[100010]; void longPathChild(int v, int f) {...
//https://github.com/EgorKulikov/yaal/tree/master/lib/main/net/egork import java.util.*; import java.io.*; public class E{ static PrintWriter out; static InputReader in; public static void main(String args[]){ out = new PrintWriter(System.out); in = new InputReader(); new E(...
t = int(input()) for i in range(t): s = input() n = 0 a = 0 b = 0 for c in s: if c == '(': a += 1 if c == '[': b += 1 if c == ')' and a > 0: n += 1 a -= 1 if c == ']' and b > 0: n += 1 b -= 1 print(n)
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define X first #define Y second #define y0 y12 #define y1 y22 #define INF 987654321987654321 #define PI 3.141592653589793238462643383279502884 #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c)) #define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c...
import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; //指定された頭文字からはじまる文字列とそれの出現回数を //扱う、クラス。sort用に比較も用意。 class Moji implements Comparable<Moji>{ String str=""; int count=0; Moji(String a,int b){ str=a; count=b; } @Override public int compareTo(Moji o) { // TODO 自動生成されたメソッド・スタブ if(t...
from collections import defaultdict while True: n = int(input()) if n==0: break initial = defaultdict(set) cnt = defaultdict(int) for _ in range(n): l = list(input().split()) for li in l: initial[li[0]].add(li) cnt[li] += 1 ...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.TreeSet; public class _526E { public static void main(String[] args) { FastScanner fs=new FastScanner(); int n=fs.n...
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; struct matrix { int x, y; long long v; } a[N]; int n, convex_hull[N]; long long f[N]; double slope(int i, int j) { return (double)(f[i] - f[j]) / (a[i].x - a[j].x); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%...
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Task242A { public static void main(String... args) throws NumberFormatException, IOException { Solution.main(System.in, System.out);...
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > V; int main(void) { int x, y, a, b; scanf("%d %d %d %d", &x, &y, &a, &b); int cnt = 0; for (int i = a; i <= x; i++) { for (int j = b; j <= y; j++) { if (i > j) cnt++; } } printf("%d\n", cnt); for (int i = a; i <= x; i++) {...
n = int(input()) masks = [1 << x for x in range(n)] for i in range(1 << n): sub = [idx for idx, mask in enumerate(masks) if i & mask != 0b00] print('{}: {}'.format(i, ' '.join(map(str, sub)))) if len(sub) != 0 else print(f'{i}:')
#include<iostream> #include<cmath> using namespace std; int main() { int n; double n2; cin >> n; n2 = n; int bit[n]; int selection[n]; for( int i = 0; i < n; i++ ) { bit[i] = i; selection[i] = 0; } for( int i = 0; i < pow( 2, n2 ); i++ ) { int...
#include <bits/stdc++.h> using namespace std; long long x, c, arr[1000]; bool b; string num_to_string(int num) { stringstream ss; ss << num; return ss.str(); } void O_o() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); } int main() { O_o(); priority_queue<int> q; int ...
import java.util.Arrays; import java.util.Scanner; public class mo { public static void main(String[] args) { Scanner input = new Scanner(System.in); int size = input.nextInt(); int lima = input.nextInt(); int[] arr = new int [size - 1]; for (int i = 0; i < arr.length; i++) { arr[i] = input.nextInt()...
s = raw_input().strip() keys = [ None for i in range(int(raw_input())) ] for i in range(len(keys)): keys[i] = raw_input().strip() best_length, best_pos = 0, 0 def kosher(left, right): pos_range = range(right - 1, left - 1, -1) #print(left, right) for key in keys: for pos in pos_range: ...
import java.io.*; import java.util.*; import static java.lang.Math.min; public class Code implements Runnable { public static void main(String[] args) throws IOException { new Thread(new Code()).start(); } private void solve() throws IOException { String mess = nextToken(); int n...
import java.util.*; public class SuperAgent { public static void main(String[]args){ String[] s = new String[3]; Scanner input = new Scanner(System.in); for(int i = 0;i < 3;i++){ s[i] = input.next(); } char[] arrmid = s[1].toCharArray(); int j = arrmid.length-1; for(int i = 0;i < j;i...
#include <bits/stdc++.h> int main() { char passKey[4][4]; char revKey[4][4]; for (int i = 0; i < 3; i++) { scanf("%s", &passKey[i][0]); } for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { revKey[2 - i][2 - j] = passKey[i][j]; } } bool isOk = true; for (int i = 0; i < 3; i++) ...
#include <bits/stdc++.h> using namespace std; int n; vector<int> tree[2010]; int dfs(int pos) { int ret = 0; for (int i = 0; i < tree[pos].size(); i++) ret = max(ret, 1 + dfs(tree[pos][i])); return ret; } int main() { scanf("%d", &n); vector<int> allah; for (int i = 0; i < n; i++) { int x; scanf...
import java.util.*; import java.io.*; public class Main { static class Scan { private byte[] buf=new byte[1024]; private int index; private InputStream in; private int total; public Scan() { in=System.in; } public int scan()throws IOExcepti...
import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class A155 { FastScanner in; PrintWriter out; public void solve() throws IOException { int ...
n = int(input()) a = [int(s) for s in input().split()] maxi, mini, k = a[0],a[0],0 for i in range(1,len(a)): if maxi < a[i]: maxi = a[i] k += 1 elif mini > a[i]: mini = a[i] k += 1 print(k)
import java.util.*; import java.math.*; public class Main { public static void main(String[] args) throws Exception{ Scanner scan = new Scanner(System.in); int r1 = scan.nextInt(); int r2 = scan.nextInt(); int c1 = scan.nextInt(); int c2 = scan.nextInt(); int d1 = sc...
#include <bits/stdc++.h> using namespace std; int main() { int r1, r2, c1, c2, d1, d2; cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; int a11 = (r1 + c1 - d2) / 2; int a12 = r1 - a11; int a21 = c1 - a11; int a22 = d1 - a11; if (a11 != a12 && a11 != a21 && a11 != a22 && a12 != a21 && a12 != a22 && a21 != a...
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), m = sc.nextInt(), x = sc.nextInt(); x = x*x; ...
from math import sqrt n, m, x = map(int, input().split()) arr=[] for i in range(n): arr.append([k for k in input()]) r=input() s = input() di={} present=set() for p in range(65,91): di[chr(p).lower()] = -1 shift=[] for a in range(n): for b in range(m): if arr[a][b] == "S": shift.append([...
import java.io.*; import java.util.*; public class Main{ static class base implements Comparable<base>{ int d,g; base(int dd,int gg){ d=dd;g=gg; } @Override public int compareTo(base o) { return d-o.d; } } static long inf=(long)1e15+7; static long[][]adj; static int V, s, t; static long ...
#include <bits/stdc++.h> using namespace std; const int N = 105; const int INF = 1e9 + 5; const long long INF_LL = 1e15 + 5; int g[N][N], dist[N][N]; struct Ship { int loc, attack, fuel, price; void read() { cin >> loc >> attack >> fuel >> price; --loc; } }; vector<pair<int, int>> reward[N]; struct Base {...
#include<bits/stdc++.h> using namespace std; const int N=200005; int a[N],n; long long ans; int main(){ scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%d",&a[i]); a[0]=-1; for (int i=1;i<=n;i++) if (a[i-1]<a[i]-1){ puts("-1"); return 0; } for (int i=n;i;i--){ if (a[i]==a[i+1]-1)continue; ans+=a[i]; }...
n = int(input()) prev = ans = -1 a = [int(input()) for _ in range(n)] for i in a: if i - prev > 1: print(-1) exit() elif i - prev == 1: ans += 1 else: ans += i prev = i print(ans)
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } templat...
import java.util.*; public class TakeMetro { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int M = scanner.nextInt(); //saves mod annoyances int S = scanner.nextInt()-1; long T = scanner.nextLong(); ...
def positive_problem(): pos_num, neg_num = 0, 0 n = eval(input()) a = input().split(' ') for i in range(len(a)): a[i] = eval(a[i]) for i in range(len(a)): if a[i] > 0: pos_num += 1 elif a[i] < 0: neg_num += 1 if n % 2 == 0: p = n // 2 e...
#include <bits/stdc++.h> using namespace std; double a[200]; int n; int main() { cin >> n; int po = 0, ni = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > 0) po++; else if (a[i] < 0) ni++; } double x = 2; if (po >= ceil((double)n / 2)) { cout << 2; return 0; } els...