description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() p = "" p += s[0] j = 1 for i in range(1, len(s)): if s[i] > s[i % j]: break if s[i] < s[i % j]: j = i + 1 ans = "" for i in range(0, k): ans += s[i % j] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = [int(d) for d in input().split()] s = input() if len(s) == 1: print(s * k) else: x = s[0] done = True i = 1 get = False idx = 0 curr = 0 while i < len(s) and done == True: if s[i] < s[curr]: x = x + s[i] if curr != 0: curr = 0 get = False i = i + 1 elif s[i] == s[curr]: x = x + s[i] curr = curr + 1 if get == False: idx = i get = True i = i + 1 else: break if get == True: x = x[:idx] full = k // len(x) rem = k - full * len(x) res = x * full + x[:rem] print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def bestPref(s): pref = s[0] for i in range(1, len(s)): ind = i % len(pref) if pref[ind] < s[i]: break elif pref[ind] > s[i]: while len(pref) < i + 1: pref += s[len(pref)] return pref def solve(n, k, s): pref = bestPref(s) cur = "" while len(cur) + len(pref) <= k: cur += pref cur += pref[: k - len(cur)] return cur n, k = list(map(int, input().split())) ans = solve(n, k, input()) print(ans)
FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def cpy_tll(s, k): s = s * (k // len(s) + 1) s = s[:k] return s n, k = map(int, input().split()) s = input() pref = s[0] ans = s[0] * k for i in s[1:]: if i <= s[0]: pref += i else: break ans = min(ans, cpy_tll(pref, k)) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() ans = n m = s[0] for i in range(1, n): if s[i] > m: ans = i break elif s[i] == m: a = s[i:] b = s[: len(a)] if a >= b: ans = i break ns = s[:ans] while len(ns) < k: ns = ns + ns print(ns[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() mi = "z" * k for i in range(1, n + 1): news = s[0:i] mi = min(mi, news * (k // i) + news[0 : k % i]) print(mi)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
import itertools def validate_prefix(s, prefix): n = len(s) source = 0 for i in range(prefix, n): if s[i] > s[source]: return prefix elif s[i] == s[source]: source += 1 if source == prefix: source = 0 continue assert s[i] < s[source] prefix = i + 1 source = 0 return prefix def determine_initial_prefix(s): prefix = 1 while True: new_prefix = validate_prefix(s, prefix) if new_prefix == prefix: break prefix = new_prefix return prefix def solve(): n, k = [int(x) for x in input().split()] s = list(input()) pref = determine_initial_prefix(s) return "".join(itertools.islice(itertools.cycle(s[:pref]), k)) print(solve())
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() ans = "" for i in range(1, len(s) + 1): g = s[:i] while len(g) < k: g += g g = g[:k] if ans == "": ans = g if g < ans: ans = g print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() ans = "z" * (k + 1) for i in range(n): ref = str() ref = s[: i + 1] while len(ref) < k: ref = ref + ref ref = ref[:k] ans = min(ans, ref) print() print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split(" ")) s = input() all_strings = [] for i in range(1, len(s) + 1): temp = s[0:i] while len(temp) < k: temp = temp * 2 all_strings.append(temp[0:k]) print(min(all_strings))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = list(map(int, input().split())) st = input() f = st[0] for i in range(1, n): if st[i] >= f: ind = i rep = k // ind temp = st[:ind] * (rep + 1) temp2 = st * (rep + 1) if temp[:k] < temp2[:k]: print(temp[:k]) break else: l = len(st) rep = k // l st = st * (rep + 1) print(st[:k])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = input().split(" ") n = int(n) k = int(k) string_a = input() strings = [] for i in range(n): strings.append(string_a[: i + 1]) for i in range(n): aux = strings[i] while len(aux) < k: aux += aux aux = aux[:k] strings[i] = aux strings.sort() print(strings[0])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def sol(A, k): ans = "z" * k for i in range(1, len(A) + 1): a = A[:i] ans = min(ans, a * (k // i) + a[: k % i]) return ans A = list(map(int, input().split())) B = input() print(sol(B, A[1]))
FUNC_DEF ASSIGN VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() l = 0 pos = 0 n = len(s) while l < n: temp = s[: pos + 1] * ((l + 1) // (pos + 1) + 1) if s[: l + 1] < temp[: l + 1]: pos = l l += 1 res = s[: pos + 1] * (k // (pos + 1) + 1) print(res[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() start = s[0] save = -1 build = "" pref = [] z = list(range(1, n + 1)) for i in range(n): build += s[i] pref.append(build * (n // len(build) + 1)) res = sorted(zip(pref, z)) save = res[0][1] s = s[:save] while len(s) < k: s = s + s s = s[:k] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() l = "" l = l + s[0] j = 0 for i in range(1, n): if s[i] > l[j]: break elif s[i] < l[j]: l = l + s[i] j = 0 elif s[i] == l[j]: l = l + s[i] j = j + 1 p = len(l) r = l[: p - j] m = len(r) s = r * (k // m) for i in range(k % m): s = s + r[i] print(s)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def sec(init, i, e, flag): i1 = i while i < n and ord(s[init]) == ord(s[i]) and init < i1: init += 1 i += 1 e += 1 if i == n or i != n and ord(s[init]) < ord(s[i]): flag = 1 return e, flag, i n, k = map(int, input().split()) s = input() init = 0 e = 1 i = 1 flag = 0 while i < n: if ord(s[i]) > ord(s[init]): break elif s[i] == s[init]: e1 = e e, flag, i = sec(init, i, e, flag) if flag == 1: e = e1 break else: init = 0 continue e += 1 i += 1 t = k // e + 1 s = s[:e] x = s for i in range(t): s += x print(*s[:k], sep="")
FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = list(map(int, input().split())) s = input() ans = s[0] * k for i in range(1, n): x = s[: i + 1] * (k // (i + 1)) + s[: k % (i + 1)] ans = min(x, ans) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def string(c: str, k: int) -> str: while len(c) < k: c = c + c if len(c) > k: sub = len(c) - k c = c[: len(c) - sub] return c def solution(n: int, k: int, s: str) -> None: first = s[0] lex = string(first, k) for i in range(1, n): if s[i] > s[0]: break first += s[i] lex = min(lex, string(first, k)) print(lex) n, k = map(int, input().strip().split()) s = input() solution(n, k, s)
FUNC_DEF VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, desired = map(int, input().split()) s = list(input()) stack = [s[0]] ptr = 0 for i in range(1, n): if ptr == len(stack): ptr = 0 if s[i] < stack[ptr]: stack.append(s[i]) ptr = 0 elif s[i] == stack[ptr]: stack.append(s[i]) ptr += 1 else: stack = stack[: len(stack) - ptr] break while stack[-1] == stack[0] and len(stack) > 1: stack.pop() ans = [] for i in range(desired): ptr = i % len(stack) ans.append(stack[ptr]) print(*ans, sep="")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, m = map(int, input().split()) s = input() i = 1 ans = s[0] * m while i < n: if s[i] > s[0]: break i += 1 t = s[:i] a = m // len(t) b = m % len(t) ans = min(ans, t * a + t[:b]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
def solve(st, n, k): pref = "" ans = None for it in st: pref += it cur = "" ln = 0 while len(pref) + len(cur) <= k: cur += pref if len(cur) < k: cur += pref[: k - len(cur)] if ans == None: ans = cur elif ans > cur: ans = cur return ans n, k = list(map(int, input().split())) st = input() ans = solve(st, n, k) print(ans)
FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NONE FOR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() for i in range(n): if s[i] > s[0]: s = s[:i] break while True: prevlen = len(s) for i in range(1, prevlen): if s[i] == s[0]: tmp = s[i:] + s tmp2 = s if tmp[:prevlen] > tmp2: s = s[:i] break if len(s) == prevlen: break ans = "" while len(ans) < k: ans += s while len(ans) > k: ans = ans[:-1] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR STRING WHILE FUNC_CALL VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, m = list(map(int, input().split())) s = input() s = list(s) ans = "" for i in range(m): ans += "z" t = "" for i in range(n): t += s[i] p = t while len(t) + len(p) < m: p += t p += p[: m - len(p)] if ans > p: ans = p print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) s = input() f = s[0] idx = n for i in range(1, n): if ord(s[i]) > ord(f): idx = i break elif s[i] == f: temp = s + s q = 2 * n // i temp1 = (q + 1) * s[:i] if temp1 < temp: idx = i break sn = s[0:idx] n = len(sn) q = k // n ans = "" ans += (q + 1) * sn ans = ans[0:k] print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
import sys n, k = map(int, sys.stdin.readline().split()) data = sys.stdin.readline().rstrip() left, right = 0, 0 for i in range(1, len(data)): if data[left] > data[i]: left = 0 right = i elif data[left] == data[i]: left += 1 else: break ans = data[: right + 1] while len(ans) < k: ans += ans print(ans[:k])
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split()) def get_pref(pref, k): while len(pref) < k: pref += pref return pref[:k] string = input() mn = get_pref(string[0], k) for i in range(1, len(string)): if string[0] < string[i]: break mn = min(mn, get_pref(string[: i + 1], k)) print(mn)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF WHILE FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = map(int, input().split(" ")) s = input() best_ss = s + s for r in range(n - 1, 0, -1): ss = s[:r] * (2 * n // r + 1) if ss <= best_ss: best_ss = ss while len(best_ss) < k: best_ss += best_ss print(best_ss[:k])
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is the constraints on $n$ and $k$. You can make hacks only if all versions of the problem are solved. You have a string $s$, and you can do two types of operations on it: Delete the last character of the string. Duplicate the string: $s:=s+s$, where $+$ denotes concatenation. You can use each operation any number of times (possibly none). Your task is to find the lexicographically smallest string of length exactly $k$ that can be obtained by doing these operations on string $s$. A string $a$ is lexicographically smaller than a string $b$ if and only if one of the following holds: $a$ is a prefix of $b$, but $a\ne b$; In the first position where $a$ and $b$ differ, the string $a$ has a letter that appears earlier in the alphabet than the corresponding letter in $b$. -----Input----- The first line contains two integers $n$, $k$ ($1 \leq n, k \leq 5000$) β€” the length of the original string $s$ and the length of the desired string. The second line contains the string $s$, consisting of $n$ lowercase English letters. -----Output----- Print the lexicographically smallest string of length $k$ that can be obtained by doing the operations on string $s$. -----Examples----- Input 8 16 dbcadabc Output dbcadabcdbcadabc Input 4 5 abcd Output aaaaa -----Note----- In the first test, it is optimal to make one duplication: "dbcadabc" $\to$ "dbcadabcdbcadabc". In the second test it is optimal to delete the last $3$ characters, then duplicate the string $3$ times, then delete the last $3$ characters to make the string have length $k$. "abcd" $\to$ "abc" $\to$ "ab" $\to$ "a" $\to$ "aa" $\to$ "aaaa" $\to$ "aaaaaaaa" $\to$ "aaaaaaa" $\to$ "aaaaaa" $\to$ "aaaaa".
n, k = list(map(int, input().split())) s = input() pos = [] for i in range(1, n + 1): pos.append((s[:i] * (k // i + 100))[:k]) print(sorted(pos)[0])
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
This is a harder version of the problem with bigger constraints. Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all integers x β‰₯ 0 such that there exists an increasing subsequence of the array a, in which the bitwise XOR of numbers is equal to x. It didn't take a long time for Korney Korneevich to find all such x, and he wants to check his result. That's why he asked you to solve this problem! A sequence s is a subsequence of a sequence b if s can be obtained from b by deletion of several (possibly, zero or all) elements. A sequence s_1, s_2, … , s_m is called increasing if s_1 < s_2 < … < s_m. Input The first line contains a single integer n (1 ≀ n ≀ 10^6). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 5000) β€” the elements of the array a. Output In the first line print a single integer k β€” the number of found x values. In the second line print k integers in increasing order x_1, x_2, … x_k (0 ≀ x_1 < … < x_k) β€” found x values. Examples Input 4 4 2 2 4 Output 4 0 2 4 6 Input 8 1 0 1 7 12 5 3 2 Output 12 0 1 2 3 4 5 6 7 10 11 12 13 Note In the first test case: * To get value x = 0 it is possible to choose and empty subsequence * To get value x = 2 it is possible to choose a subsequence [2] * To get value x = 4 it is possible to choose a subsequence [4] * To get value x = 6 it is possible to choose a subsequence [2, 4]
MAX = 1 << 13 n = int(input()) d = [MAX] * MAX d[0] = 0 flag = [0] * (MAX + 1) for _ in input().split(): a = int(_) if flag[a]: continue flag[a] = 1 for i in range(MAX): if d[i] < a: if d[i ^ a] > a: for j in range(a + 1, d[i ^ a] + 1): flag[j] = 0 d[i ^ a] = a r = [str(i) for i in range(MAX) if d[i] < MAX] print(len(r)) print(" ".join(r))
ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is a harder version of the problem with bigger constraints. Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all integers x β‰₯ 0 such that there exists an increasing subsequence of the array a, in which the bitwise XOR of numbers is equal to x. It didn't take a long time for Korney Korneevich to find all such x, and he wants to check his result. That's why he asked you to solve this problem! A sequence s is a subsequence of a sequence b if s can be obtained from b by deletion of several (possibly, zero or all) elements. A sequence s_1, s_2, … , s_m is called increasing if s_1 < s_2 < … < s_m. Input The first line contains a single integer n (1 ≀ n ≀ 10^6). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 5000) β€” the elements of the array a. Output In the first line print a single integer k β€” the number of found x values. In the second line print k integers in increasing order x_1, x_2, … x_k (0 ≀ x_1 < … < x_k) β€” found x values. Examples Input 4 4 2 2 4 Output 4 0 2 4 6 Input 8 1 0 1 7 12 5 3 2 Output 12 0 1 2 3 4 5 6 7 10 11 12 13 Note In the first test case: * To get value x = 0 it is possible to choose and empty subsequence * To get value x = 2 it is possible to choose a subsequence [2] * To get value x = 4 it is possible to choose a subsequence [4] * To get value x = 6 it is possible to choose a subsequence [2, 4]
import sys input = sys.stdin.readline mx = 1 << 13 n = int(input()) a = list(map(int, input().split())) g = [list() for _ in range(5001)] r = [5001] * mx r[0] = 0 ans = [False] * mx ans[0] = True for x in range(1, 5001): g[x].append(0) for i in range(n): for y in g[a[i]]: x = a[i] ^ y ans[x] = True if r[x] > a[i]: for val in range(a[i] + 1, r[x]): g[val].append(x) r[x] = min(r[x], a[i] + 1) g[a[i]].clear() ans = [x for x in range(mx) if ans[x]] print(len(ans)) print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer a_{i}Β β€” the current skill level. All skills have the same maximum level A. Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values: The number of skills that a character has perfected (i.e., such that a_{i} = A), multiplied by coefficient c_{f}. The minimum skill level among all skills (min a_{i}), multiplied by coefficient c_{m}. Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force. -----Input----- The first line of the input contains five space-separated integers n, A, c_{f}, c_{m} and m (1 ≀ n ≀ 100 000, 1 ≀ A ≀ 10^9, 0 ≀ c_{f}, c_{m} ≀ 1000, 0 ≀ m ≀ 10^15). The second line contains exactly n integers a_{i} (0 ≀ a_{i} ≀ A), separated by spaces,Β β€” the current levels of skills. -----Output----- On the first line print the maximum value of the Force that the character can achieve using no more than m currency units. On the second line print n integers a'_{i} (a_{i} ≀ a'_{i} ≀ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces. -----Examples----- Input 3 5 10 1 5 1 3 1 Output 12 2 5 2 Input 3 5 10 1 339 1 3 1 Output 35 5 5 5 -----Note----- In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1. In the second test one should increase all skills to maximum.
f = lambda: list(map(int, input().split())) g = lambda: m - l * p[l - 1] + s[l] n, A, x, y, m = f() t = sorted((q, i) for i, q in enumerate(f())) p = [q for q, i in t] s = [0] * (n + 1) for j in range(n): s[j + 1] = p[j] + s[j] l = r = n F = L = R = B = -1 while 1: if p: while l > r or g() < 0: l -= 1 b = min(p[l - 1] + g() // l, A) else: b, l = A, 0 f = x * (n - r) + y * b if F < f: F, L, R, B = f, l, r, b if not p: break m += p.pop() - A r -= 1 if m < 0: break print(F) p = [(i, B if j < L else q if j < R else A) for j, (q, i) in enumerate(t)] for i, q in sorted(p): print(q)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE NUMBER IF VAR WHILE VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer a_{i}Β β€” the current skill level. All skills have the same maximum level A. Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values: The number of skills that a character has perfected (i.e., such that a_{i} = A), multiplied by coefficient c_{f}. The minimum skill level among all skills (min a_{i}), multiplied by coefficient c_{m}. Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force. -----Input----- The first line of the input contains five space-separated integers n, A, c_{f}, c_{m} and m (1 ≀ n ≀ 100 000, 1 ≀ A ≀ 10^9, 0 ≀ c_{f}, c_{m} ≀ 1000, 0 ≀ m ≀ 10^15). The second line contains exactly n integers a_{i} (0 ≀ a_{i} ≀ A), separated by spaces,Β β€” the current levels of skills. -----Output----- On the first line print the maximum value of the Force that the character can achieve using no more than m currency units. On the second line print n integers a'_{i} (a_{i} ≀ a'_{i} ≀ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces. -----Examples----- Input 3 5 10 1 5 1 3 1 Output 12 2 5 2 Input 3 5 10 1 339 1 3 1 Output 35 5 5 5 -----Note----- In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1. In the second test one should increase all skills to maximum.
n, A, cf, cm, mN = map(int, input().split()) a = list(map(int, input().split())) aCOPY = [] for elem in a: aCOPY.append(elem) a.sort() aPartialSum = [0] for elem in a: aPartialSum.append(aPartialSum[-1] + elem) maxScore = 0 ansMAXIBound = 0 ansMAXI = 0 ansMIN = 0 for MAXI in range(n + 1): currentScore = cf * MAXI if MAXI >= 1: mN -= A - a[-MAXI] if mN < 0: break if MAXI == n: maxScore = currentScore + A * cm ansMAXIBound = 0 ansMAXI = 10**10 ansMIN = 0 l = a[0] r = A - 1 while l < r: m = (l + r + 1) // 2 lA = 0 rA = n - MAXI - 1 while lA < rA: mA = (lA + rA) // 2 if a[mA] > m: rA = mA - 1 if a[mA] < m: lA = mA + 1 if a[mA] == m: lA = mA rA = mA break lA = min(lA, n - MAXI - 1) if a[lA] > m: lA -= 1 expenditure = (lA + 1) * m - aPartialSum[lA + 1] if expenditure > mN: r = m - 1 else: l = m currentScore += l * cm if currentScore > maxScore: maxScore = currentScore ansMAXIBound = a[-MAXI] ansMAXI = MAXI ansMIN = l print(maxScore) inclCount = 0 for i in range(n): if aCOPY[i] > ansMAXIBound and inclCount < ansMAXI: aCOPY[i] = A inclCount += 1 for i in range(n): if aCOPY[i] == ansMAXIBound and inclCount < ansMAXI: aCOPY[i] = A inclCount += 1 if aCOPY[i] < ansMIN: aCOPY[i] = ansMIN print(" ".join(map(str, aCOPY)))
ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Lesha plays the recently published new version of the legendary game hacknet. In this version character skill mechanism was introduced. Now, each player character has exactly n skills. Each skill is represented by a non-negative integer ai β€” the current skill level. All skills have the same maximum level A. Along with the skills, global ranking of all players was added. Players are ranked according to the so-called Force. The Force of a player is the sum of the following values: * The number of skills that a character has perfected (i.e., such that ai = A), multiplied by coefficient cf. * The minimum skill level among all skills (min ai), multiplied by coefficient cm. Now Lesha has m hacknetian currency units, which he is willing to spend. Each currency unit can increase the current level of any skill by 1 (if it's not equal to A yet). Help him spend his money in order to achieve the maximum possible value of the Force. Input The first line of the input contains five space-separated integers n, A, cf, cm and m (1 ≀ n ≀ 100 000, 1 ≀ A ≀ 109, 0 ≀ cf, cm ≀ 1000, 0 ≀ m ≀ 1015). The second line contains exactly n integers ai (0 ≀ ai ≀ A), separated by spaces, β€” the current levels of skills. Output On the first line print the maximum value of the Force that the character can achieve using no more than m currency units. On the second line print n integers a'i (ai ≀ a'i ≀ A), skill levels which one must achieve in order to reach the specified value of the Force, while using no more than m currency units. Numbers should be separated by spaces. Examples Input 3 5 10 1 5 1 3 1 Output 12 2 5 2 Input 3 5 10 1 339 1 3 1 Output 35 5 5 5 Note In the first test the optimal strategy is to increase the second skill to its maximum, and increase the two others by 1. In the second test one should increase all skills to maximum.
f = lambda: map(int, input().split()) g = lambda: m - l * p[l - 1] + s[l] n, A, x, y, m = f() t = sorted((q, i) for i, q in enumerate(f())) p = [q for q, i in t] s = [0] * (n + 1) for j in range(n): s[j + 1] = p[j] + s[j] l = r = n F = L = R = B = -1 while 1: if p: while l > r or g() < 0: l -= 1 b = min(p[l - 1] + g() // l, A) else: b, l = A, 0 f = x * (n - r) + y * b if F < f: F, L, R, B = f, l, r, b if not p: break m += p.pop() - A r -= 1 if m < 0: break print(F) p = [(i, B if j < L else q if j < R else A) for j, (q, i) in enumerate(t)] for i, q in sorted(p): print(q)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE NUMBER IF VAR WHILE VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fretting the fret number j on the i-th string produces the note a_{i} + j. Tayuya wants to play a melody of n notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if a = [1, 1, 2, 2, 3, 3], and the sequence of notes is 4, 11, 11, 12, 12, 13, 13 (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be 10, the minimal one will be 3, and the answer is 10 - 3 = 7, as shown on the picture. <image> Input The first line contains 6 space-separated numbers a_{1}, a_{2}, ..., a_{6} (1 ≀ a_{i} ≀ 10^{9}) which describe the Tayuya's strings. The second line contains the only integer n (1 ≀ n ≀ 100 000) standing for the number of notes in the melody. The third line consists of n integers b_{1}, b_{2}, ..., b_{n} (1 ≀ b_{i} ≀ 10^{9}), separated by space. They describe the notes to be played. It's guaranteed that b_i > a_j for all 1≀ i≀ n and 1≀ j≀ 6, in other words, you can play each note on any string. Output Print the minimal possible difference of the maximal and the minimal indices of used frets. Examples Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 Note In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the 100-th fret is used each time, so the difference is 100 - 100 = 0. <image> In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be 10, the minimal one will be 3, and the answer is 10 - 3 = 7. <image>
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline A = list(map(int, input().split())) N = int(input()) B = list(map(int, input().split())) A.sort() B.sort() cnt = {i: (0) for i in range(N)} C = [] for i in range(N): for j in range(6): C.append((B[i] - A[j], i)) C.sort(key=lambda x: x[0]) r = -1 zero_num = N ans = 10**10 for l in range(6 * N): if l > 0: cnt[C[l - 1][1]] -= 1 if cnt[C[l - 1][1]] == 0: zero_num += 1 while zero_num != 0: r += 1 if r == 6 * N: print(ans) exit() _, i = C[r] cnt[i] += 1 if cnt[i] == 1: zero_num -= 1 ans = min(ans, C[r][0] - C[l][0]) print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fretting the fret number j on the i-th string produces the note a_{i} + j. Tayuya wants to play a melody of n notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if a = [1, 1, 2, 2, 3, 3], and the sequence of notes is 4, 11, 11, 12, 12, 13, 13 (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be 10, the minimal one will be 3, and the answer is 10 - 3 = 7, as shown on the picture. <image> Input The first line contains 6 space-separated numbers a_{1}, a_{2}, ..., a_{6} (1 ≀ a_{i} ≀ 10^{9}) which describe the Tayuya's strings. The second line contains the only integer n (1 ≀ n ≀ 100 000) standing for the number of notes in the melody. The third line consists of n integers b_{1}, b_{2}, ..., b_{n} (1 ≀ b_{i} ≀ 10^{9}), separated by space. They describe the notes to be played. It's guaranteed that b_i > a_j for all 1≀ i≀ n and 1≀ j≀ 6, in other words, you can play each note on any string. Output Print the minimal possible difference of the maximal and the minimal indices of used frets. Examples Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 Note In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the 100-th fret is used each time, so the difference is 100 - 100 = 0. <image> In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be 10, the minimal one will be 3, and the answer is 10 - 3 = 7. <image>
ss = sorted(set(map(int, input().split()))) input() nn = sorted(set(map(int, input().split()))) if len(nn) == 1: print(0) elif len(ss) == 1: print(nn[-1] - nn[0]) else: n0 = nn[0] ans = int(1000000000.0) for s in ss: n0f = n0 - s lrs = [] for n in nn[1:]: l = r = int(1000000000.0) for s in ss: f = n - s if f == n0f: break if f > n0f: r = min(r, f - n0f) else: l = min(l, n0f - f) else: lrs.append([l, r]) if not lrs: ans = 0 break lrs.sort() rls = sorted(lrs, key=lambda x: x[1], reverse=True) ansc = rls[0][1] nrls = len(rls) ir = 0 for lr in lrs: lr[1] = 0 while ir < nrls and rls[ir][1] == 0: ir += 1 r = rls[ir][1] if ir < nrls else 0 ansc = min(ansc, lr[0] + r) ans = min(ans, ansc) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has 6 strings and an infinite number of frets numbered from 1. Fretting the fret number j on the i-th string produces the note a_{i} + j. Tayuya wants to play a melody of n notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if a = [1, 1, 2, 2, 3, 3], and the sequence of notes is 4, 11, 11, 12, 12, 13, 13 (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be 10, the minimal one will be 3, and the answer is 10 - 3 = 7, as shown on the picture. <image> Input The first line contains 6 space-separated numbers a_{1}, a_{2}, ..., a_{6} (1 ≀ a_{i} ≀ 10^{9}) which describe the Tayuya's strings. The second line contains the only integer n (1 ≀ n ≀ 100 000) standing for the number of notes in the melody. The third line consists of n integers b_{1}, b_{2}, ..., b_{n} (1 ≀ b_{i} ≀ 10^{9}), separated by space. They describe the notes to be played. It's guaranteed that b_i > a_j for all 1≀ i≀ n and 1≀ j≀ 6, in other words, you can play each note on any string. Output Print the minimal possible difference of the maximal and the minimal indices of used frets. Examples Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 Note In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the 100-th fret is used each time, so the difference is 100 - 100 = 0. <image> In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be 10, the minimal one will be 3, and the answer is 10 - 3 = 7. <image>
import sys readline = sys.stdin.readline A = list(map(int, readline().split())) N = int(readline()) B = list(map(int, readline().split())) b0 = B[0] B = B[1:] INF = 3 * 10**9 if N == 1: print(0) else: ans = INF for a in A: S = b0 - a ev = [None] * N ev[-1] = S, S for i in range(N - 1): b = B[i] l, r = -INF, INF for a in A: if b - a == S: l, r = S, S break if b - a < S: l = max(l, b - a) else: r = min(r, b - a) ev[i] = l, r ev.sort() mr = S for l, r in ev: ans = min(ans, mr - l) mr = max(mr, r) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
import sys def main(): a = [int(x) for x in input().split()] n = int(input()) b = [int(x) for x in input().split()] fretsAndNoteNo = [] for base in a: for noteNo, note in enumerate(b): fret = note - base fretsAndNoteNo.append((fret, noteNo)) fretsAndNoteNo.sort(key=lambda x: x[0]) ans = float("inf") noteNoCnts = [(0) for _ in range(n)] totalUniqueNotesInSegment = 0 left = 0 for right, (fretNo, noteNo) in enumerate(fretsAndNoteNo): noteNoCnts[noteNo] += 1 if noteNoCnts[noteNo] == 1: totalUniqueNotesInSegment += 1 while totalUniqueNotesInSegment == n: diff = fretNo - fretsAndNoteNo[left][0] ans = min(ans, diff) noteNoCnts[fretsAndNoteNo[left][1]] -= 1 if noteNoCnts[fretsAndNoteNo[left][1]] == 0: totalUniqueNotesInSegment -= 1 left += 1 print(ans) return input = sys.stdin.buffer.readline def oneLineArrayPrint(arr): print(" ".join([str(x) for x in arr])) def multiLineArrayPrint(arr): print("\n".join([str(x) for x in arr])) def multiLineArrayOfArraysPrint(arr): print("\n".join([" ".join([str(x) for x in y]) for y in arr])) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
a = list(map(int, input().split())) n = int(input()) s = list(map(int, input().split())) b = [] i = 0 j = 0 ans = 10**18 cs = [0] * n nz = 1 z = n * 6 for y in range(n): for x in a: b.append((s[y] - x) * n + y) b.sort() cs[b[0] % n] += 1 while j + 1 < z: while j + 1 < z and nz < n: j += 1 nz += cs[b[j] % n] < 1 cs[b[j] % n] += 1 while nz == n: ans = min(ans, b[j] // n - b[i] // n) cs[b[i] % n] -= 1 nz -= cs[b[i] % n] == 0 i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
from sys import stdin a = list(map(int, stdin.readline().split())) a.sort(reverse=True) n = int(stdin.readline()) res = [] b = list(map(int, stdin.readline().split())) for i in range(n): bi = b[i] for ai in a: res.append((bi - ai, i)) res.sort() cnt = [0] * n r = 0 c = 0 ans = int(1e18) for l in range(n * 6): while c != n and r < n * 6: cnt[res[r][1]] += 1 if cnt[res[r][1]] == 1: c += 1 r += 1 if c != n: break ans = min(ans, res[r - 1][0] - res[l][0]) cnt[res[l][1]] -= 1 if cnt[res[l][1]] == 0: c -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
la = input().split() la = list(map(int, la)) t = input() lb = input().split() lb = list(map(int, lb)) lenb = len(lb) l = [(lb[j] - i, j) for i in la for j in range(lenb)] l.sort() last = 0 first = 0 l0 = [0] * lenb l1 = [i for i in range(lenb)] dic = dict(zip(l1, l0)) dic[l[first][1]] += 1 length = 1 lenl = len(l) while length < lenb: last += 1 if dic[l[last][1]] == 0: length += 1 dic[l[last][1]] += 1 minnum = l[last][0] - l[first][0] t = 0 while 1: dic[l[first][1]] -= 1 while dic[l[first][1]] != 0: first += 1 dic[l[first][1]] -= 1 if minnum > l[last][0] - l[first][0]: minnum = l[last][0] - l[first][0] if first == last: break first += 1 last += 1 if last >= lenl: break while dic[l[last][1]] != 0: dic[l[last][1]] += 1 last = last + 1 if last >= lenl: break if last >= lenl: break dic[l[last][1]] += 1 if minnum > l[last][0] - l[first][0]: minnum = l[last][0] - l[first][0] if minnum == 0: break print(minnum)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
import sys input = sys.stdin.readline mod = 10**9 + 7 INF = float("inf") def getlist(): return list(map(int, input().split())) def main(): A = getlist() N = int(input()) B = getlist() T = [] check = [0] * N cnt = 0 for i in A: for j, b in enumerate(B): T.append((b - i, j)) T.sort() ans = INF i = 0 j = 0 while True: if cnt < N: if j >= 6 * N: break val, ind = T[j] if check[ind] == 0: cnt += 1 check[ind] += 1 j += 1 else: if i >= 6 * N: break val, ind = T[i] valj = T[j - 1][0] ans = min(ans, valj - val) if check[ind] == 1: cnt -= 1 check[ind] -= 1 i += 1 print(ans) main()
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
import sys def techno_c(): a = list(set(map(int, sys.stdin.readline().split()))) n = int(sys.stdin.readline()) b = list(set(map(int, sys.stdin.readline().split()))) n = len(b) m = len(a) raz = [] for j in range(m): for i in range(n): raz.append([b[i] - a[j], i]) raz.sort() if m == 1: return raz[-1][0] - raz[0][0] v = [-1] * n s = set() for i in range(n * m): if v[raz[i][1]] >= 0: raz[v[raz[i][1]]][0] = -1 v[raz[i][1]] = i s.add(raz[i][1]) if len(s) == n: break j = i i = min(v) ans = raz[j][0] - raz[i][0] while j < n * m - 1: if len(s) == n: ans = min(ans, raz[j][0] - raz[i][0]) if ans == 0: return 0 k = raz[i][1] raz[v[k]][0] = -1 v[k] = -1 s.remove(k) i += 1 while raz[i][0] == -1: i += 1 else: j += 1 if v[raz[j][1]] >= 0: if v[raz[j][1]] == i: i += 1 while raz[i][0] == -1: i += 1 raz[v[raz[j][1]]][0] = -1 v[raz[j][1]] = j s.add(raz[j][1]) return ans rez = techno_c() sys.stdout.write(str(rez) + "\n")
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
import sys as _sys from itertools import chain as _chain def main(): a_seq = tuple(_read_ints()) (n,) = _read_ints() notes = tuple(_read_ints()) result = find_minimal_melody_difficulty(a_seq, notes) print(result) def find_minimal_melody_difficulty(strings_values, notes): strings_values = set(strings_values) strings_values = sorted(strings_values, reverse=True) if len(strings_values) == 1: return max(notes) - min(notes) frets_by_notes_indices = [[(note - x) for x in strings_values] for note in notes] del strings_values del notes max_initially_selected_fret = max(frets[0] for frets in frets_by_notes_indices) for frets in frets_by_notes_indices: i_first_interesting_fret = 0 while ( i_first_interesting_fret + 1 < len(frets) and frets[i_first_interesting_fret + 1] <= max_initially_selected_fret ): i_first_interesting_fret += 1 frets[:] = frets[i_first_interesting_fret:] sorted_frets = sorted(_chain.from_iterable(frets_by_notes_indices)) initially_selected_frets = [frets[0] for frets in frets_by_notes_indices] min_initially_selected = min(initially_selected_frets) i_first_selected = sorted_frets.index(min_initially_selected) while ( i_first_selected + 1 < len(sorted_frets) and sorted_frets[i_first_selected + 1] == min_initially_selected ): i_first_selected += 1 i_first_selected -= initially_selected_frets.count(min_initially_selected) - 1 i_last_selected = i_first_selected + len(initially_selected_frets) - 1 available_selections = _chain.from_iterable( zip(frets[1:], frets[0:]) for frets in frets_by_notes_indices ) available_selections = sorted(available_selections) frets_to_unselect = dict() for frets in frets_by_notes_indices: for fret in frets: frets_to_unselect[fret] = 0 result = sorted_frets[i_last_selected] - sorted_frets[i_first_selected] for new_fret, fret_to_unselect in available_selections: frets_to_unselect[fret_to_unselect] += 1 i_first_selected = _shift_index_skipping( sorted_frets, i_first_selected, frets_to_unselect ) i_last_selected += 1 current_maxmin_difference = ( sorted_frets[i_last_selected] - sorted_frets[i_first_selected] ) if current_maxmin_difference < result: result = current_maxmin_difference return result def _shift_index_skipping(sorted_seq, index, deletions_ns): while index < len(sorted_seq) and deletions_ns[sorted_seq[index]] > 0: deleted_element = sorted_seq[index] index += 1 deletions_ns[deleted_element] -= 1 return index def _read_line(): result = _sys.stdin.readline() assert result[-1] == "\n" return result[:-1] def _read_ints(): return map(int, _read_line().split()) main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER STRING RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
import sys input = sys.stdin.readline def getInts(): return [int(s) for s in input().strip().split()] def getInt(): return int(input().strip()) def solve(): A = getInts() M = getInt() B = getInts() X = [] P = [] for i, b in enumerate(B): for a in A: P.append((b - a, i)) P.sort() i = 0 j = -1 counts = [0] * M sset = set() ans = 2 * 10**9 MAX = M * 6 set_len = 0 while i < MAX: while set_len < M and j < MAX: j += 1 try: if not counts[P[j][1]]: set_len += 1 counts[P[j][1]] += 1 except: break if set_len < M: break z = P[i][1] ans = min(ans, P[j][0] - P[i][0]) counts[z] -= 1 if not counts[z]: set_len -= 1 i += 1 return ans print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
a = list(map(int, input().split())) n = int(input()) s = list(map(int, input().split())) b = [] for i in range(n): for j in a: b.append((s[i] - j) * n + i) b = sorted(b) cs = {} i = j = 0 ans = 10**18 cs[b[0] % n] = cs.get(b[0] % n, 0) + 1 z = len(b) while j < z: while j + 1 < z and len(cs) < n: j += 1 cs[b[j] % n] = cs.get(b[j] % n, 0) + 1 while len(cs) == n: ans = min(ans, b[j] // n - b[i] // n) cs[b[i] % n] -= 1 if cs[b[i] % n] == 0: cs.pop(b[i] % n) i += 1 if j + 1 == z: break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR WHILE BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
a = list(map(int, input().split())) n = int(input()) s = list(map(int, input().split())) b = [] for i in range(n): for j in a: b.append((s[i] - j) * n + i) b.sort() cs = {} i = j = 0 ans = 10**18 def dd(w): cs[w] = cs.get(w, 0) + 1 def em(w): cs[w] -= 1 if cs[w] == 0: cs.pop(w) dd(b[0] % n) while j < len(b): while j + 1 < len(b) and len(cs) < n: j += 1 dd(b[j] % n) while len(cs) == n: ans = min(ans, b[j] // n - b[i] // n) em(b[i] % n) i += 1 if j + 1 == len(b): break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
arr = [int(x) for x in input().split()] arr = list(set(arr)) n = int(input()) b = [int(x) for x in input().split()] temp = [] b = list(set(b)) n = len(b) for i in range(n): for j in arr: temp.append((b[i] - j, i)) temp.sort() start, end = 0, 0 l = len(temp) curr = 0 ans = 10000000000000 d = [(0) for i in range(n)] while end < l or end == l and curr >= n: if curr < n: if d[temp[end][1]] == 0: curr += 1 d[temp[end][1]] += 1 end += 1 else: ans = min(ans, temp[end - 1][0] - temp[start][0]) d[temp[start][1]] -= 1 if d[temp[start][1]] == 0: curr -= 1 start += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
from sys import stdin input = stdin.readline a = sorted([int(i) for i in input().split()]) n = int(input()) b = sorted([int(i) for i in input().split()]) c = [] for i in range(n): c += [[b[i] - a[j], i] for j in range(6)] c.sort() d = [0] * n e = 0 ans = 10**10 u = 0 for i in range(len(c)): while u < len(c) and e < n: x = c[u][1] if d[x] == 0: e += 1 d[x] += 1 u += 1 if e == n: ans = min(ans, c[u - 1][0] - c[i][0]) x = c[i][1] d[x] -= 1 if d[x] == 0: e -= 1 print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def MI(): return map(int, sys.stdin.buffer.readline().split()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() inf = 10**16 aa = LI() n = II() bb = LI() di = [] for a in aa: for i, b in enumerate(bb): di.append((b - a, i)) di.sort() cnt = [0] * n now = 0 for r, (d, i) in enumerate(di): if cnt[i] == 0: now += 1 cnt[i] += 1 if now == n: break ans = inf for d0, i0 in di: while r + 1 < n * 6 and now < n: r += 1 i1 = di[r][1] if cnt[i1] == 0: now += 1 cnt[i1] += 1 if now < n: break ans = min(ans, di[r][0] - d0) cnt[i0] -= 1 if cnt[i0] == 0: now -= 1 print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR VAR WHILE BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
After battling Shikamaru, Tayuya decided that her flute is too predictable, and replaced it with a guitar. The guitar has $6$ strings and an infinite number of frets numbered from $1$. Fretting the fret number $j$ on the $i$-th string produces the note $a_{i} + j$. Tayuya wants to play a melody of $n$ notes. Each note can be played on different string-fret combination. The easiness of performance depends on the difference between the maximal and the minimal indices of used frets. The less this difference is, the easier it is to perform the technique. Please determine the minimal possible difference. For example, if $a = [1, 1, 2, 2, 3, 3]$, and the sequence of notes is $4, 11, 11, 12, 12, 13, 13$ (corresponding to the second example), we can play the first note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$, as shown on the picture. [Image] -----Input----- The first line contains $6$ space-separated numbers $a_{1}$, $a_{2}$, ..., $a_{6}$ ($1 \leq a_{i} \leq 10^{9}$) which describe the Tayuya's strings. The second line contains the only integer $n$ ($1 \leq n \leq 100\,000$) standing for the number of notes in the melody. The third line consists of $n$ integers $b_{1}$, $b_{2}$, ..., $b_{n}$ ($1 \leq b_{i} \leq 10^{9}$), separated by space. They describe the notes to be played. It's guaranteed that $b_i > a_j$ for all $1\leq i\leq n$ and $1\leq j\leq 6$, in other words, you can play each note on any string. -----Output----- Print the minimal possible difference of the maximal and the minimal indices of used frets. -----Examples----- Input 1 4 100 10 30 5 6 101 104 105 110 130 200 Output 0 Input 1 1 2 2 3 3 7 13 4 11 12 11 13 12 Output 7 -----Note----- In the first sample test it is optimal to play the first note on the first string, the second note on the second string, the third note on the sixth string, the fourth note on the fourth string, the fifth note on the fifth string, and the sixth note on the third string. In this case the $100$-th fret is used each time, so the difference is $100 - 100 = 0$. [Image] In the second test it's optimal, for example, to play the second note on the first string, and all the other notes on the sixth string. Then the maximal fret will be $10$, the minimal one will be $3$, and the answer is $10 - 3 = 7$. [Image]
a = list(map(int, input().split())) n = int(input()) notes = list(map(int, input().split())) s, used = [], [(0) for i in range(n * 6)] for i in range(n): for j in a: s.append((notes[i] - j, i)) l, res, cnt = 0, 10**20, 0 s.sort() for r in range(len(s)): used[s[r][1]] += 1 if used[s[r][1]] == 1: cnt += 1 while cnt == n: res = min(res, s[r][0] - s[l][0]) used[s[l][1]] -= 1 if used[s[l][1]] == 0: cnt -= 1 l += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β€” horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo. Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during T seconds. Input The first line of the input contains 4 integers n, a, b, T (1 ≀ n ≀ 5Β·105, 1 ≀ a, b ≀ 1000, 1 ≀ T ≀ 109) β€” the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation. If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation. Output Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds. Examples Input 4 2 3 10 wwhw Output 2 Input 5 2 4 13 hhwhh Output 4 Input 5 2 4 1000 hhwhh Output 5 Input 3 1 100 10 whw Output 0 Note In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
def main(): n, a, b, t = map(int, input().split()) oris = input() def get_time(front, rear, count_rot): span = front - rear offset = min(front, -rear) return span, span * a + (span + 1) + offset * a + count_rot * b front = rear = span = count_rot = new_count_rot = time = 0 has_one = False for i in range(0, -n, -1): if oris[i] == "w": new_count_rot += 1 new_span, new_time = get_time(front, i, new_count_rot) if new_time > t: break has_one = True span, time, rear, count_rot = new_span, new_time, i, new_count_rot if not has_one: return 0 maxi = max_span = n - 1 while front < maxi and rear <= 0 and span != max_span: front += 1 if oris[front] == "w": count_rot += 1 while True: new_span, time = get_time(front, rear, count_rot) if time <= t: break if oris[rear] == "w": count_rot -= 1 rear += 1 if rear > 0: return span + 1 span = max(new_span, span) return span + 1 print(main())
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER WHILE NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def solve(k, s): c = 0 zeros = map(len, s.split("1")) if k > 0: zeros = list(zeros) l = len(zeros) for i in range(l - k + 1): if i + k >= l: continue head, tail = zeros[i], zeros[i + k] c += (head + 1) * (tail + 1) return c else: return sum(e * (e + 1) // 2 for e in zeros) k = int(input()) s = input() print(solve(k, s))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) a = [int(i) for i in input()] n = len(a) if k: b = [0] * n c = 0 for i in range(1, n): if a[i - 1] == 0: b[i] = i - c elif a[i] == 0: c = i i, j, d, e = 0, -1, 0, 0 while i < n and j < n: if d <= k: if d == k: while i < n and a[i] == 0: i += 1 e += b[i] + 1 j += 1 if j == n: break d += a[j] else: d -= a[i] i += 1 print(e) else: b = c = 0 for i in range(1, n): if a[i] != a[i - 1]: if a[i]: b += (i - c) * (i - c + 1) // 2 c = i if a[n - 1] == 0: b += (n - c) * (n - c + 1) // 2 print(b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) count = [0] * (n + 1) count[0] += 1 ini = 0 if k == 0: ans = 0 ini = 0 s = s + "1" for i in range(n + 1): if s[i] == "1": ans = ans + ini * (ini + 1) // 2 ini = 0 else: ini += 1 print(ans) else: for i in s: if i == "1": ini += 1 count[ini] += 1 ans = 0 for i in range(k, n + 1): ans += count[i] * count[i - k] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() prefix = [(0) for i in range(len(s) + 1)] for i in range(0, len(s)): prefix[i + 1] = int(int(s[i]) - int("0")) + prefix[i] ans = 0 mp = {} mp[k] = 1 for i in range(0, len(s)): if prefix[i + 1] in mp.keys(): ans += mp[prefix[i + 1]] if k + prefix[i + 1] in mp.keys(): mp[k + prefix[i + 1]] += 1 else: mp[prefix[i + 1] + k] = 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
n = int(input()) s = input() a = [(len(x) + 1) for x in s.split("1")] if n == 0: print(sum(x * (x - 1) // 2 for x in a)) else: print(sum(a * b for a, b in zip(a, a[n:])))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() cs = [0] * 1000001 arr = [0] * 1000001 arr[0] = 1 cnt = 0 for i in range(len(s)): cs[i + 1] = cs[i] + int(s[i]) for i in range(1, len(s) + 1): cnt += arr[cs[i] - k] arr[cs[i]] += 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() ans, z = 0, 0 dp = [1] + [0] * 10**6 for c in s: if c == "1": z += 1 if z >= k: ans += dp[z - k] dp[z] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
import sys input = sys.stdin.readline k = int(input()) s = input()[:-1] if k == 0: ans = 0 cnt = 1 for i in range(len(s) - 1): if s[i] == s[i + 1]: cnt += 1 else: if s[i] == "0": ans += cnt * (cnt + 1) // 2 cnt = 1 if s[-1] == "0": ans += cnt * (cnt + 1) // 2 print(ans) exit() idx = [] for i in range(len(s)): if s[i] == "1": idx.append(i + 1) idx = [0] + idx + [len(s) + 1] ans = 0 for i in range(1, len(idx) - 1): if i + k - 1 <= len(idx) - 2: ans += (idx[i] - idx[i - 1]) * (idx[i + k] - idx[i + k - 1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = list(map(int, input())) co = list() c = 1 for el in s: if el == 0: c += 1 else: co.append(c) c = 1 co.append(c) if k == 0: print(sum(map(lambda el: el * (el - 1) // 2, co))) exit() ans = 0 for i in range(len(co) - k): ans += co[i] * co[i + k] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def fun(k, s): le = len(s) lis = list(s) l = 0 r = 1 ans = [] j = le - 1 number_of_one = 0 count = 0 arr = [] zeros = 0 for i in range(le): if lis[i] == "1": arr.append(zeros) zeros = 0 else: zeros += 1 arr.append(zeros) for i in range(len(arr)): if i + k >= len(arr): break left = arr[i] right = arr[i + k] if k == 0: count += left * (left + 1) // 2 else: count += left * right + left + right + 1 return count k = input() st = input() print(fun(int(k), st))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() a = [] p = 1 otv = 0 f = 0 for i in s: if i == "1": a.append(p) p = 1 if i == "0": p += 1 a.append(p) if k != 0: for i in range(len(a) - k): otv += a[i] * a[i + k] print(otv) else: for i in range(len(a)): otv += (a[i] - 1) / 2.0 * a[i] print(int(otv))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) a = [0] + list(map(int, input())) n = len(a) curr = sol = 0 c = {(0): 1} for i in range(1, n): curr += a[i] sol += c.get(curr - k, 0) c[curr] = c.get(curr, 0) + 1 print(sol)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() ind, ans = [], 0 for i in range(len(s)): if s[i] == "1": ind.append(i) if k > len(ind): print(0) exit() if k == 0: if len(ind) == 0: ans = len(s) * (len(s) + 1) // 2 else: for i in range(1, len(ind)): ans += (ind[i] - ind[i - 1]) * (ind[i] - ind[i - 1] - 1) // 2 ans += ind[0] * (ind[0] + 1) // 2 ans += (len(s) - ind[len(ind) - 1]) * (len(s) - ind[len(ind) - 1] - 1) // 2 else: t1, t2 = [ind[0] + 1], [len(s) - ind[len(ind) - 1]] for i in range(1, len(ind) - k + 1): t1.append(ind[i] - ind[i - 1]) for i in range(len(ind) - 1, k - 1, -1): t2.append(ind[i] - ind[i - 1]) for i in range(len(t1)): ans += t1[i] * t2[len(t1) - i - 1] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST BIN_OP VAR NUMBER NUMBER LIST BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) ans = 0 cnt = 0 freq = [0] * (n + 1) freq[0] = 1 for i in range(n): if s[i] == "1": cnt += 1 if cnt - k >= 0: ans += freq[cnt - k] freq[cnt] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k, s = int(input()), input() ans, t = 0, 0 cnt = list(map(lambda x: 0, range(1200000))) cnt[0] = 1 for c in s: if c == "1": t += 1 if t - k >= 0: ans += cnt[t - k] cnt[t] += 1 print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
p = 0 g = -1 k = int(input()) s = input() n = len(s) a = [0] * (n + 1) for i in range(n): if s[i] == "1": a[p] = i - g g = i p += 1 a[p] = n - g su = 0 if k == 0: for i in range(p + 1): su += a[i] * (a[i] - 1) // 2 else: for i in range(p - k + 1): su += a[i] * a[i + k] print(su)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
K = int(input()) S = input() L = [0] * (len(S) + 1) L[0] = 1 X = Y = 0 for i in S: if i == "1": Y += 1 if Y >= K: X += L[Y - K] L[Y] += 1 print(X)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() cnt = 0 dp = [(0) for i in range(len(s))] d = {} for i in range(len(s)): if s[i] == "1": dp[i] = dp[i - 1] + 1 else: dp[i] = dp[i - 1] if dp[i] == k: cnt = cnt + 1 if dp[i] - k in d: cnt = cnt + d[dp[i] - k] if dp[i] not in d: d[dp[i]] = 1 else: d[dp[i]] = d[dp[i]] + 1 print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) l = [] cnt = 0 for i in range(n): if s[i] == "1": l.append(cnt) cnt = 0 else: cnt += 1 l.append(cnt) ans = 0 for i in range(len(l)): if i + k < len(l) and k > 0: ans += (l[i] + 1) * (l[i + k] + 1) elif i + k < len(l): ans += l[i] * (l[i] + 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() total = 0 counter = 0 zeroes = [0] * (len(s) + 100) for c in s: if c == "0": zeroes[counter] += 1 elif c == "1": counter += 1 for t in range(counter + 1): if t >= k: if k == 0: total += zeroes[t] * (zeroes[t] + 1) // 2 else: total += (1 + zeroes[t]) * (1 + zeroes[t - k]) print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR NUMBER IF VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) string = input() count = 0 modified_string = [] i = 0 n = len(string) while i < n: if string[i] == "0": count += 1 else: modified_string.append(count) count = 0 if i == n - 1: modified_string.append(count) i += 1 i = 0 ans = 0 n = len(modified_string) if k == 0: for each in modified_string: if each != 0: ans += int(each) * (int(each) + 1) // 2 else: while i + k < n: ans += (modified_string[i] + 1) * (modified_string[i + k] + 1) i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) st = str(input()) s = list(map(int, list(st))) n = len(s) if k == 0: total = 0 st = st.split("1") for seg in st: if "0" in seg: leng = len(seg) total += leng * (leng + 1) // 2 print(total) if k != 0: prefix = [(0) for i in range(n + 1)] suma = 0 for i in range(1, n + 1): suma += s[i - 1] prefix[i] = suma count_for_r = [(0) for i in range(n + 1)] l = 1 r = 1 while l < n + 1 and r < n + 1: if l > r: r = l if prefix[r] - prefix[l - 1] < k: r += 1 elif prefix[r] - prefix[l - 1] == k and s[l - 1] == 0: l += 1 count_for_r[r] += 1 elif prefix[r] - prefix[l - 1] == k: count_for_r[r] += 1 while r < n and s[r - 1] == 0: r += 1 r += 1 l += 1 total = 0 added_before = 0 for i in range(1, n + 1): if i != 0 and s[i - 1] == 0: total += added_before if s[i - 1] == 1: total += count_for_r[i] added_before = count_for_r[i] print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) d = [0] * 2000000 d[0] = 1 s = 0 for c in input(): s += int(c) d[s] += 1 res = 0 for i in range(k, s + 1): if k == 0: res += (d[i] - 1) * d[i] // 2 else: res += d[i] * d[i - k] print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() s = "1" + s + "1" one = [i for i in range(len(s)) if s[i] == "1"] total = 0 if k == 0: total = sum((len(seg) + 1) * len(seg) // 2 for seg in s.split("1")) else: for i in range(k, len(one) - 1): total += (one[i - k + 1] - one[i - k]) * (one[i + 1] - one[i]) print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
K = int(input()) s = input() N = len(s) res = 0 countOfOne = 0 freq = [(0) for i in range(N + 1)] freq[0] = 1 for i in range(0, N, 1): countOfOne += ord(s[i]) - ord("0") if countOfOne >= K: res += freq[countOfOne - K] freq[countOfOne] += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def readln(): return tuple(map(int, input().split())) (k,) = readln() inp = "#" + input() n = len(inp) pref = [0] * n for i in range(1, n): pref[i] = pref[i - 1] + (inp[i] == "1") cnt = [0] * (pref[n - 1] + 1) for v in pref: cnt[v] += 1 ans = 0 for i in range(pref[n - 1] - k + 1): ans += cnt[i] * cnt[i + k] if k == 0: print(sum([(len(_) * (len(_) + 1) // 2) for _ in inp[1:].split("1")])) else: print(ans)
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP STRING FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() pref = [0] for i in s: pref.append(pref[-1] + int(i)) d = {} ans = 0 for i in pref: ans += d.get(i - k, 0) d[i] = d.get(i, 0) + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
n = int(input()) S = input() a = [(len(x) + 1) for x in S.split("1")] if n == 0: k = 0 for x in a: k += int(x / 2 * (x - 1)) print(k) else: k = 0 for i in range(len(a) - n): k += a[i] * a[i + n] print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() l = [0] * len(s) r = [0] * len(s) one = [] ze = 0 specialCase = 0 for i in range(0, len(s)): if s[i] == "0": ze += 1 else: l[i] = ze specialCase += ze * (ze + 1) // 2 one.append(i) ze = 0 specialCase += ze * (ze + 1) // 2 ze = 0 for i in range(len(s) - 1, -1, -1): if s[i] == "0": ze += 1 else: r[i] = ze ze = 0 if len(one) < k: print(0) elif k == 0: print(specialCase) else: ans = 0 start, end = 0, k - 1 while end < len(one): ans += (l[one[start]] + 1) * (1 + r[one[end]]) start += 1 end += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
n = int(input()) s = input() zero = [] num = 0 for i in range(len(s)): if s[i] == "1": zero.append(num) num = 0 else: num += 1 zero.append(num) ans = 0 if n == 0: for i in range(len(zero)): ans += zero[i] * (zero[i] + 1) // 2 else: for i in range(len(zero) - n): ans += (zero[i] + 1) * (zero[i + n] + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = list(map(int, list(input()))) n = len(s) dp = [(0) for i in range(n + 1)] dp[0] = 1 ans = 0 numOne = 0 for i in range(n): if s[i] == 1: numOne += 1 if numOne >= k: ans += dp[numOne - k] dp[numOne] += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def main(): k, s, res = int(input()), input(), 0 if k: l = [i for i, c in enumerate(s) if c == "1"] if len(l) >= k: l.append(len(s)) a, c = -1, l[k - 1] for b, d in zip(l, l[k:]): res += (b - a) * (d - c) a, c = b, d else: l, j = [], -1 for i, c in enumerate(s): if c == "1": l.append(i - j - 1) j = i l.append(len(s) - j - 1) res = sum(i * (i + 1) for i in l) // 2 print(res) main()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR STRING IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) a = [0] * n dict = {} count = 0 for i in range(n): if s[i] == "1": count += 1 if count in dict: dict[count] += 1 else: dict[count] = 1 ans = 0 count = 0 for i in range(n): if k in dict: ans += dict[k] if s[i] == "1": k += 1 count += 1 if count in dict: dict[count] -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = str(input()) n = len(s) x = 0 y = 1 lis = [0] a = 0 if k == 0: a = 0 ans = 0 for i in range(n): if s[i] == "0": a += 1 else: if a > 0: ans += a * (a + 1) // 2 a = 0 if a > 0: ans += a * (a + 1) // 2 print(ans) else: for i in range(n): if s[i] == "1": a += 1 lis.append(a) ans = 0 while x <= n and y <= n: if lis[y] - lis[x] == k: temp1 = lis[y] temp2 = lis[x] b = 0 a = 0 while y <= n and lis[y] == temp1: y += 1 b += 1 while x <= n and lis[x] == temp2: x += 1 a += 1 ans += a * b elif lis[y] - lis[x] < k: if y == x: x = y y += 1 else: y += 1 else: x += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) if s.count("1") < k: print(0) exit(0) if k == 0: ans = 0 ct = 0 for j in range(n): if s[j] == "0": ct += 1 if s[j] == "1" or j == n - 1: ans += ct * (ct + 1) // 2 ct = 0 else: i, j, ct = 0, 0, 0 for j in range(n): if s[j] == "1": ct += 1 if ct == k: break i = s.find("1") ans = 0 b = i while j < n: j += 1 a = 0 while j < n and s[j] == "0": a += 1 j += 1 ans += (b + 1) * (a + 1) i += 1 b = 0 while i < n and s[i] == "0": b += 1 i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) st = input() s = {(0): 1} n, a = 0, 0 for c in st: v = int(c) n += v a += s.get(n - k, 0) s[n] = 1 + s.get(n, 0) print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() ar = [0] n = len(s) def get(i): if i < len(ar) and i > -1: return ar[i] + 1 return 1 if s[0] == "0": ar[-1] += 1 for i in range(1, n): if s[i] == "0": if ar[-1] == 0: ar += [1] else: ar[-1] += 1 else: ar += [0] second = 0 first = 1 n = len(ar) num = 0 if s[0] == "1": num += 1 first = 0 ans = 0 if k == 0: second = n while second < n: if ar[second] != 0: second += 1 if get(second) == 1: num += 1 continue if num > k: num -= 1 first += 1 if ar[first] != 0: first += 1 elif num == k: ans += get(first - 1) * get(second + 1) second += 1 if get(second) == 1: num += 1 else: second += 1 if get(second) == 1: num += 1 if k == 0: ans = 0 for i in ar: ans += i * (i + 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER STRING VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF VAR NUMBER NUMBER VAR LIST NUMBER VAR NUMBER NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() cnt = [0] * (len(s) + 1) for i in range(len(s)): cnt[i + 1] = cnt[i] + int(s[i] == "1") res = 0 summ = [1] + [0] * (len(s) + 1) for i in range(len(s)): if cnt[i + 1] - k >= 0: res += summ[cnt[i + 1] - k] summ[cnt[i + 1]] += 1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = [*map(len, input().split("1"))] res = 0 if k == 0: for i, x in enumerate(s): res += x * (x + 1) // 2 else: for i, x in enumerate(s[k:]): res += (s[i] + 1) * (x + 1) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() j = 0 ans = 0 next_j = 0 cnt_ones = 0 for i in range(len(s)): if j <= i: j = i + 1 cnt_ones += 1 if s[i] == "1" else 0 if cnt_ones < k or j <= i: while j < len(s) and cnt_ones < k: if s[j] == "1": cnt_ones += 1 j += 1 if next_j < j: next_j = j while next_j < len(s) and s[next_j] != "1": next_j += 1 if cnt_ones == k: ans += next_j - j + 1 if s[i] == "1": cnt_ones -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR STRING NUMBER NUMBER IF VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) num = input() if k == 0: c = 0 ans = 0 for i in range(len(num)): if num[i] == "0": c += 1 else: ans += c * (c + 1) // 2 c = 0 ans += c * (c + 1) // 2 else: inds = [-1] for i in range(len(num)): if num[i] == "1": inds.append(i) ans = 0 for i in range(1, len(inds) - k + 1): if i + k < len(inds): ans += (inds[i] - inds[i - 1]) * (inds[i + k] - inds[i + k - 1]) else: ans += (inds[i] - inds[i - 1]) * (len(num) - inds[i + k - 1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
import sys input = sys.stdin.readline a = int(input()) s = input() ans = [] count = 0 for i in range(len(s)): if s[i] == "0": count += 1 else: ans.append(count) count = 0 ans.append(count) if a == 0: total = 0 for i in range(len(ans) - 1): r = ans[i] total = total + r * (r + 1) // 2 total = total + ans[-1] * (ans[-1] + 1) // 2 print(total) exit() start = 1 end = a total = 0 ra = s.count("1") while end <= ra: total = total + (ans[start - 1] + 1) * (ans[end] + 1) start = start + 1 end = end + 1 print(total)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() n = len(s) p = [0] * (n + 1) for i in range(1, n + 1): p[i] = p[i - 1] + (ord(s[i - 1]) - ord("0")) b = 0 r = 0 ans = 0 for i in range(1, n + 1): if p[i] - p[b] > k: while p[i] - p[b] > k: b += 1 if k == 0: if p[i] - p[b] == k: ans += i - b else: if b >= r: r = b + 1 while r < i and s[r - 1] == "0": r += 1 if p[i] - p[b] == k: ans += r - b print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() a = [0] for i in range(len(s)): if s[i] == "1": a.append(i + 1) a.append(len(s) + 1) if len(a) - 2 < k: print(0) elif k == 0: ct = 0 for i in range(1, len(a)): ct += (a[i] - a[i - 1] - 1) * (a[i] - a[i - 1]) // 2 print(ct) else: l = 0 r = k + 1 ct = 0 while r < len(a): ct += (a[l + 1] - a[l]) * (a[r] - a[r - 1]) l += 1 r += 1 print(ct)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k, s, v = int(input()), input(), 0 n, p1 = len(s), [-1] + [i for i, ch in enumerate(s) if ch == "1"] p1.append(n) if k == 0: for i in range(len(p1) - 1): m = p1[i + 1] - p1[i] - 1 v += m * (m + 1) // 2 else: for i in range(k, len(p1) - 1): v += (p1[i - k + 1] - p1[i - k]) * (p1[i + 1] - p1[i]) print(v)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
def f(n): return n * (n + 1) // 2 k = int(input()) s = input() l = [] for i in range(len(s)): if s[i] == "1": l.append(i) n = len(l) ans = 0 if k == 0: if n == 0: ans = f(len(s)) else: ans += f(l[0]) for i in range(1, n): z = l[i] - l[i - 1] - 1 ans += f(z) ans += f(len(s) - l[-1] - 1) elif k <= n: for i in range(0, n - k + 1): p = l[i] - l[i - 1] - 1 if i > 0 else l[i] r = l[i + k] - l[i + k - 1] - 1 if i + k < n else len(s) - l[-1] - 1 ans += 1 + p + r + p * r print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k = int(input()) s = input() dp = [(0) for i in range(len(s) + 1)] dp[0] = 1 c = 0 n = 0 for i, v in enumerate(s, 1): c += int(v) if c - k >= 0: n += dp[c - k] dp[c] += 1 print(n)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A string is binary, if it consists only of characters "0" and "1". String v is a substring of string w if it has a non-zero length and can be read starting from some position in string w. For example, string "010" has six substrings: "0", "1", "0", "01", "10", "010". Two substrings are considered different if their positions of occurrence are different. So, if some string occurs multiple times, we should consider it the number of times it occurs. You are given a binary string s. Your task is to find the number of its substrings, containing exactly k characters "1". Input The first line contains the single integer k (0 ≀ k ≀ 106). The second line contains a non-empty binary string s. The length of s does not exceed 106 characters. Output Print the single number β€” the number of substrings of the given string, containing exactly k characters "1". Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 1 1010 Output 6 Input 2 01010 Output 4 Input 100 01010 Output 0 Note In the first sample the sought substrings are: "1", "1", "10", "01", "10", "010". In the second sample the sought substrings are: "101", "0101", "1010", "01010".
k, s, ones = int(input()), input(), [-1] for i, ch in enumerate(s): if ch == "1": ones.append(i) ones.append(len(s)) n = 0 if k == 0: for a1, a2 in zip(ones, ones[1:]): n += (a2 - a1) * (a2 - a1 - 1) // 2 else: for a1, a2, a3, a4 in zip(ones, ones[1:], ones[k:], ones[k + 1 :]): n += (a4 - a3) * (a2 - a1) print(n)
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR