description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys mod, MOD = 1000000007, 998244353 def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() def check(Arr): come = set() go = set() for i in Arr: if i < 0: if abs(i) not in come: return False if i > 0: if i in come: return False else: come.add(i) return True n = int(input()) Arr = get_array() dp = Arr.copy() for i in range(1, n): dp[i] += dp[i - 1] last = 0 flag = 0 ans = [] count = 0 if dp[-1] != 0: print(-1) exit() for i in range(n): if dp[i] == 0: if check(Arr[last : i + 1]): ans.append(i - last + 1) count += 1 last = i + 1 else: flag = 1 break if flag == 0: print(count) print(*ans) else: print(-1)
IMPORT ASSIGN VAR VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys input = sys.stdin.readline n = int(input()) A = list(map(int, input().split())) NOW = set() USED = set() ANS = [0] for i in range(n): if A[i] > 0: if A[i] in NOW or A[i] in USED: print(-1) return else: NOW.add(A[i]) USED.add(A[i]) elif -A[i] in NOW: NOW.remove(-A[i]) else: print(-1) return if NOW == set(): ANS.append(i + 1) USED = set() if len(NOW) > 0: print(-1) return print(len(ANS) - 1) ANS2 = [(ANS[i] - ANS[i - 1]) for i in range(1, len(ANS))] print(" ".join(map(str, ANS2)))
IMPORT ASSIGN VAR 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN IF VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) set_ = set([]) set_2 = set([]) ans = [] for i in range(n): if a[i] > 0: if a[i] not in set_2: set_.add(a[i]) else: print(-1) exit() set_2.add(a[i]) elif abs(a[i]) in set_: set_.remove(-a[i]) else: print(-1) exit() if not set_: set_2 = set([]) ans.append(i + 1) if set_: print(-1) exit() res = [0] * len(ans) for i in range(len(ans)): if i == 0: res[i] = ans[i] else: res[i] = ans[i] - ans[i - 1] print(len(res)) print(*res)
IMPORT ASSIGN VAR 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 FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
N = int(input()) current = set() entered = set() events = [] A = list(map(int, input().split())) try: n_events = 0 for i, ai in enumerate(A): n_events += 1 if ai < 0: assert -ai in current current.remove(-ai) else: assert ai not in entered current.add(ai) entered.add(ai) if not current: events.append(n_events) n_events = 0 entered = set() assert not current if n_events != 0: events.append(n_events) print(len(events)) print(" ".join(map(str, events))) except AssertionError: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def main(): n = int(input()) err_ = False array = list(map(int, input().split())) peolpe = {} peolpe_sum = 0 event = 0 ans = [] for i in range(n): a = array[i] if a > 0 and a not in peolpe: peolpe[a] = True peolpe_sum += a elif a < 0 and -a in peolpe: peolpe_sum += a else: err_ = True break event += 1 if peolpe_sum == 0: ans.append(str(event)) peolpe = {} event = 0 if err_ or peolpe_sum != 0: print(-1) else: print(len(ans)) print(" ".join(ans)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def valid(a): s = set() for i in a: if i < 0: if abs(i) not in s: return 0 if i > 0: if i in s: return 0 else: s.add(i) return 1 n = int(input()) a = list(map(int, input().split())) l = a.copy() c = 0 f = 0 r = [] flag = 0 for i in range(1, n): l[i] = l[i] + l[i - 1] if l[-1] != 0: print(-1) exit() for i in range(n): if l[i] == 0: if valid(a[f : i + 1]) == 1: r.append(i - f + 1) c += 1 f = i + 1 else: flag = 1 break if flag == 0: print(c) print(*r) else: print(-1)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def main(n, a): num = 0 num_list = [0] tmp = set() used = set() for elem in a: if elem > 0: if elem not in used: tmp.add(elem) used.add(elem) num_list[-1] += 1 else: print(-1) exit() elif -elem in tmp: tmp.remove(-elem) num_list[-1] += 1 else: print(-1) exit() if len(tmp) == 0: used = set() num += 1 num_list.append(0) if len(tmp) != 0: print(-1) exit() print(len(num_list) - 1) print(*num_list[:-1]) n = int(input()) a = list(map(int, input().split())) main(n, a)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER 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 VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) count = 0 e, l = {}, {} ans = [] for i in a: if i > 0 and i in e: count = -1 break elif i > 0: e[i] = 1 count += 1 elif i in l: count = -1 break else: if -1 * i not in e: count = -1 break l[i] = 1 count += 1 if len(e) == len(l): e, l = {}, {} ans.append(count) count = 0 if len(e) != len(l): print(-1) exit() if count == -1: print(-1) exit() print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR DICT DICT ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR DICT DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) arr = [int(p) for p in input().split()] preSum = arr.copy() for i in range(1, n): preSum[i] += preSum[i - 1] s = set() size = [] for i in range(len(preSum)): if arr[i] < 0: if -arr[i] not in s: print(-1) exit() if preSum[i] == 0: size.append(len(s) + 1) s = set() elif arr[i] in s: print(-1) exit() else: s.add(arr[i]) if sum(size) == n: print(len(size)) print(*size) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
vis = [(-1) for i in range(1000005)] val_days = list() def valid_day(mov): tot_sum = 0 if mov[0] < 0: print(-1) return for m in mov: tot_sum += m if tot_sum != 0: print(-1) return curr_sum = 0 curr_cnt = 0 for i in range(len(mov)): if mov[i] > 0: if vis[mov[i]] < len(val_days): vis[mov[i]] = len(val_days) curr_sum += mov[i] curr_cnt += 1 else: print(-1) return elif vis[abs(mov[i])] == len(val_days): curr_sum += mov[i] curr_cnt += 1 else: print(-1) return if curr_cnt != 0: if curr_sum == 0: val_days.append(curr_cnt) curr_sum = 0 curr_cnt = 0 print(len(val_days)) fin_ans = str(val_days[0]) for i in range(1, len(val_days)): fin_ans += " " + str(val_days[i]) print(fin_ans) n = int(input()) arr = input().split(" ") mov = list() for a in arr: mov.append(int(a)) valid_day(mov)
ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) ls = [int(x) for x in input().split()] s = set() vis = set() flag = 0 ans = [] count = 0 for i in range(len(ls)): count += 1 if ls[i] > 0: if ls[i] in s or ls[i] in vis: flag = 1 break else: s.add(ls[i]) vis.add(ls[i]) else: a = -1 * ls[i] if a not in s: flag = 1 break s.remove(a) if len(s) == 0: ans.append(count) count = 0 vis = set() if flag == 1 or len(s) > 0: print("-1") else: print(len(ans)) print(" ".join(str(i) for i in ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) rab = set() office = set() dc = [] end = True d = 0 c = -1 for i in range(len(a)): if a[i] > 0: if a[i] not in office: office.add(a[i]) rab.add(a[i]) else: print(-1) exit() elif -a[i] in office: if -a[i] in rab: rab.remove(-a[i]) else: print(-1) exit() else: print(-1) exit() if not rab: dc.append(i - c) d += 1 c = i office = set() if rab: print(-1) else: print(d) print(*dc)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) logs = list(map(int, input().split())) days = [0] c, p, chk = set(), set(), True for i in range(n): if logs[i] > 0 and logs[i] in c or logs[i] < 0 and -1 * logs[i] not in p: chk = False break if logs[i] < 0: p.remove(-1 * logs[i]) if len(p) == 0: days.append(i + 1) c, p = set(), set() if logs[i] > 0: c.add(logs[i]) p.add(logs[i]) if chk and len(p) == 0: print(len(days) - 1) for i in range(1, len(days)): print(days[i] - days[i - 1], end=" ") else: print(-1)
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 NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(j) for j in input().split()] days = [] cur_emp = set() day_emp = set() day_cnt = 0 for event in a: emp = abs(event) if event > 0: if emp in day_emp: print(-1) exit(0) day_emp.add(emp) cur_emp.add(emp) else: if emp not in cur_emp: print(-1) exit(0) cur_emp.discard(emp) day_cnt += 1 if len(cur_emp) == 0: day_emp = set() days.append(day_cnt) day_cnt = 0 if len(cur_emp) != 0: print(-1) exit(0) print(len(days)) for day in days: print(day)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys def main(): n = int(input()) a = list(map(int, input().split())) dp = {} arr = [] cnt = 0 last = 0 for i in range(n): if a[i] > 0: if dp.get(a[i]) != None: print(-1) return dp[a[i]] = 1 cnt += 1 elif a[i] < 0: if dp.get(abs(a[i])) == None or dp[abs(a[i])] == 0: print(-1) return else: dp[abs(a[i])] = 0 cnt -= 1 if cnt == 0: arr.append(i + 1 - last) last = i + 1 dp = {} if cnt != 0: print(-1) return print(len(arr)) for x in arr: print(x, end=" ") print() return main()
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR NONE VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, input().split(" "))) t = [] t1 = [] a = [0] * 1000001 flag = 0 s = 0 for i in range(0, n): if l[i] > 0: a[l[i]] += 1 if a[l[i]] > 1: flag = 1 break if l[i] < 0: if a[-l[i]] == 0: flag = 1 break if a[-l[i]] == 1: a[-l[i]] = 0 s += l[i] t.append(l[i]) if s == 0: if len(t) != len(set(t)): flag = 1 break t1.append(len(t)) t = [] if s != 0: print(-1) elif flag == 1: print(-1) else: print(len(t1)) print(*t1, sep=" ")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
from sys import stdin n = int(stdin.readline().strip()) s = list(map(int, stdin.readline().strip().split())) en = [(0) for i in range(10**6 + 7)] vis = [(0) for i in range(10**6 + 7)] ans = [0] flag = True cnt = 0 v = [] for i in range(n): ans[-1] += 1 vis if s[i] > 0: en[s[i]] += 1 vis[s[i]] += 1 v.append(s[i]) cnt += 1 else: en[-s[i]] -= 1 cnt -= 1 s[i] = abs(s[i]) if en[s[i]] < 0 or en[s[i]] > 1 or vis[s[i]] > 1: flag = False break if i > 0 and cnt == 0: ans.append(0) for j in v: vis[j] = 0 v = [] if flag == False or ans[-1] != 0: print(-1) else: if ans[-1] == 0: ans.pop() print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, input().split())) d = [] c, s = 0, 0 dd = {} ddd = {} f = 0 for i in range(n): if l[i] > 0: if l[i] not in dd: dd[l[i]] = 1 else: dd[l[i]] += 1 if dd[l[i]] > 1: f = 1 break else: x = abs(l[i]) if x not in ddd: ddd[x] = 1 else: ddd[x] += 1 c = c + 1 s = s + l[i] if dd == ddd: d.append(c) c = 0 s = 0 ddd = {} dd = {} elif s < 0: f = 1 break if s > 0: print(-1) elif f == 1: print(-1) elif len(d) == 0: print(-1) else: print(len(d)) print(*d)
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 VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys input = sys.stdin.readline def main(): emp_n = 10**6 N = int(input()) A = [int(x) for x in input().split()] ent = [0] * (emp_n + 1) days = 0 s = set() is_valid = True co = 0 ans = [] for i in range(N): if not s: ans.append(co) co = 0 days += 1 co += 1 if A[i] > 0: if ent[A[i]] == days: is_valid = False break ent[A[i]] = days if A[i] in s: is_valid = False break else: s.add(A[i]) elif abs(A[i]) in s: s.remove(abs(A[i])) else: is_valid = False break ans.append(co) if s: is_valid = False if is_valid: print(days) print(*ans[1:]) else: print(-1) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys input = sys.stdin.buffer.readline n = int(input()) l = list(map(int, input().split())) entered = 0 left = 0 array = [0] * (10**6 + 1) no_of_days = [] flag = 0 log = [] for i in l: if entered - left < 0: flag = 1 break if i > 0: if array[i] == 0: array[i] += 1 entered += 1 log.append(i) else: flag = 1 break elif i < 0: if array[abs(i)] == 1: array[abs(i)] += 2 left += 1 else: flag = 1 break if entered == left: no_of_days.append(entered + left) entered = 0 left = 0 for j in log: array[j] = 0 log = [] if flag or entered > 0: print(-1) else: print(len(no_of_days)) no_of_days = list(map(str, no_of_days)) print(" ".join(no_of_days))
IMPORT ASSIGN VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def main(): input() office, table, res, i = set(), set(), [], -1 for j, s in enumerate(input().split()): if s[0] == "-": s = s[1:] if s not in office: return print("-1") office.remove(s) if not office: res.append(j - i) i, table = j, set() else: if s in table: return print("-1") office.add(s) table.add(s) if office: return print("-1") print(len(res), " ".join(map(str, res)), sep="\n") main()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR VAR RETURN FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) A = list(map(int, input().split())) res = [] cnt = 0 employees = set() last = 0 emp_cnt = set() for i in range(n): if A[i] > 0: if A[i] in employees: print("-1") break else: employees.add(A[i]) if A[i] in emp_cnt: print("-1") break else: emp_cnt.add(A[i]) elif -A[i] in employees: employees.remove(-A[i]) else: print("-1") break if i != 0 and len(employees) == 0: res.append(i + 1 - last) last = i + 1 cnt += 1 emp_cnt = set() else: if len(employees) == 0: print(cnt) print(*res) else: print(-1)
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 FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
inside = set() entered = set() n = int(input()) events = list(map(int, input().split())) days = 0 edays = [] currente = 0 for i in range(n): p = events[i] if p > 0: if p not in entered: inside.add(p) entered.add(p) currente += 1 else: days = -1 edays = [] break elif -p in inside: inside.remove(-p) currente += 1 if inside == set(): days += 1 edays.append(currente) currente = 0 entered = set() else: days = -1 edays = [] break if inside != set(): days = -1 edays = [] print(days) print(" ".join([str(x) for x in edays]))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR 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 NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) list1 = list(map(int, input().split())) list2 = [] set1 = {list1[0]} list2.append(list1[0]) ll = [] f = 0 dict1 = {} if list1[0] > 0: dict1[list1[0]] = 1 else: f = 1 if f == 0: for i in range(1, n): if list1[i] > 0: if list1[i] in set1: f = 1 break elif list1[i] not in dict1: set1.add(list1[i]) list2.append(list1[i]) dict1[list1[i]] = 1 else: f = 1 break if list1[i] < 0: if -list1[i] in set1: set1.remove(-list1[i]) list2.append(list1[i]) else: f = 1 break if f == 0 and not set1: ll.append(list2) list2 = [] dict1 = {} if f == 0 and not list2: print(len(ll)) for i in range(len(ll)): print(len(ll[i]), end=" ") else: print(-1)
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 VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT IF VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) s = set() d = 0 ans = [] c = 0 o = set() for i in range(n): x = a[i] if x > 0: if i == 0: c += 1 s.add(x) o.add(x) elif len(s): if x in s or x in o: d = -1 break else: c += 1 s.add(x) o.add(x) else: d += 1 ans.append(c) s.add(x) o = {x} c = 1 elif x < 0: if i == 0: d = -1 break elif len(s): if -1 * x in s: s.remove(-1 * x) c += 1 else: d = -1 break else: d = -1 break if d != -1: if len(s): print("-1") else: d += 1 ans.append(c) print(d) print(*ans) else: print("-1")
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 ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
a = int(input()) b = list(map(int, input().split())) def solve(a, b): checkedin = set() blocked = set() ans = [] if a % 2 != 0: return -1 for i in b: if i > 0: if i in blocked or i in checkedin: return -1 else: checkedin.add(i) elif -i in checkedin: blocked.add(-i) checkedin.remove(-i) else: return -1 if len(checkedin) == 0: ans.append(len(blocked) * 2) blocked.clear() if len(checkedin) != 0: return -1 return ans c = solve(a, b) if c == -1: print(-1) else: print(len(c)) print(" ".join([str(x) for x in c]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys n = int(input()) array = [int(s) for s in input().split()] a = set() people = [0] * 1000001 used = [] prev = 0 ans = [] for i in range(len(array)): if array[i] < 0 and not -array[i] in a or array[i] > 0 and people[array[i]] == 1: print(-1) sys.exit(0) else: if array[i] < 0 and -array[i] in a: a.remove(-array[i]) elif array[i] > 0: a.add(array[i]) people[array[i]] = 1 used.append(array[i]) if len(a) == 0: ans.append(i - prev + 1) prev = i + 1 for i in range(len(used)): people[used[i]] = 0 used = [] if len(a) != 0: print(-1) else: print(len(ans)) print(" ".join([str(s) for s in ans]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) inside = set() flag = True j = 1 ans = list() b = set() for i in range(len(a)): if a[i] > 0: if a[i] in inside or a[i] in b: flag = False else: inside.add(a[i]) b.add(a[i]) elif -a[i] in inside: inside.remove(-a[i]) if len(inside) == 0: ans.append(j) b.clear() j = 0 else: flag = False j += 1 if len(inside) != 0: flag = False if flag: print(len(ans)) print(*ans) else: print(-1)
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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) l = list(map(int, input().split())) flag = True d = {} for i in l: try: d[i] += 1 except: d[i] = 1 for i in d.keys(): try: if d[i] != d[-1 * i]: flag = False break except: flag = False break if flag: ans = [] d = {} i = 0 c = 0 e = {} while i < n: try: x = d[-1 * l[i]] del d[-1 * l[i]] c += 1 if len(d) == 0: ans.append(c) c = 0 d = {} e = {} except: if l[i] < 0: break try: x = e[l[i]] break except: d[l[i]] = 0 e[l[i]] = 0 c += 1 i += 1 if i == n: print(len(ans)) print(*ans) else: print(-1) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) s = 0 a = [-1] m = set() tmp = list(map(int, input().split())) for i in range(n): j = tmp[i] s += j if j < 0 and -j not in m or j in m: print("-1") exit() m.add(j) if not s: m = set() a.append(i) if a[-1] != n - 1: print(-1) else: print(len(a) - 1) print(*[(a[i + 1] - a[i]) for i in range(len(a) - 1)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
d = {} input() i = s = 0 r = [0] for x in map(int, input().split()): i += 1 s += x if x > 0: if d.get(x, 0): r = (-1,) break d[x] = 2 else: if d.get(-x, 0) < 2: r = (-1,) break d[-x] = 1 if s == 0: r[0] += 1 r += (i,) d = {} i = 0 if s: r = (-1,) print(r[0]) print(*r[1:])
ASSIGN VAR DICT EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(j) for j in input().split()] l = [(0) for i in range(int(1000000.0 + 1))] tot = 0 flag = True days = [] so_fa = 0 s = set() for i in range(n): if a[i] > 0: if l[a[i]] == 0 and a[i] not in s: s.add(a[i]) l[a[i]] += 1 tot += 1 so_fa += 1 else: flag = False break elif l[-1 * a[i]] == 1: l[-1 * a[i]] -= 1 tot -= 1 so_fa += 1 if tot == 0: days.append(so_fa) so_fa = 0 tot = 0 s = set() else: flag = False break if tot > 0: flag = False if flag: print(len(days)) for i in range(len(days)): print(days[i], end=" ") print() else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
import sys def input(): str = sys.stdin.readline() return str[:-1] n = int(input()) arr = list(map(int, input().split(" "))) ans = [] rec = {} totRec = {} for j in range(len(arr)): i = arr[j] if i > 0: if i not in rec and i not in totRec: rec[i] = 1 totRec[i] = 1 else: ans = [-1] break else: i = -i if i not in rec: ans = [-1] break else: rec.pop(i) if len(rec) == 0: ans.append(j + 1) totRec = {} if ans[0] == -1 or len(rec) != 0 or len(totRec) != 0: print(-1) else: print(len(ans)) pre = 0 for i in ans: print(i - pre, end=" ") pre = i
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING ASSIGN VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) days = [int(i) for i in input().split()] pos = True soe = set() resA = [] rs = 0 cd = 0 c = 0 oC = 0 for d in days: if d < 0 and -d not in soe or d in soe: pos = False break if d > 0: oC += 1 else: oC -= 1 rs += d c += 1 soe.add(d) if rs == 0: if oC != 0 or c % 2 != 0: pos = False break cd += 1 resA.append(c) c = 0 soe = set() oC = 0 if len(days) % 2 != 0 or rs != 0: pos = False if pos: print(cd) for i in resA: print(i, end=" ") else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
def solve(l, n): ans = [] d = {} vis = {} s = 0 s_ans = 0 for t in range(n): if l[t] < 0: if d.get(-1 * l[t], 0) == 0: return -1 else: s += l[t] d[-1 * l[t]] = 0 if l[t] > 0: if d.get(l[t], 0) == 1: return -1 if vis.get(l[t], 0): return -1 vis[l[t]] = 1 s += l[t] d[l[t]] = 1 if s == 0: d.clear() vis.clear() if len(ans) == 0: ans.append(t + 1) s_ans += t + 1 else: ans.append(t + 1 - s_ans) s_ans += ans[-1] if s == 0: return ans else: return -1 nn = int(input()) ln = list(map(int, input().split())) p = solve(ln, nn) if p == -1: print(p) else: print(len(p)) print(*p)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER NUMBER RETURN NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR RETURN 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 VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
ints = lambda: [int(x) for x in input().split()] def solve(arr): p, inside, entered = [], set(), set() for x in arr: if x < 0 and -x not in inside or x > 0 and x in entered: return [] elif x < 0: inside.remove(-x) if len(inside) == 0: p.append(2 * len(entered)) entered = set() else: inside.add(x) entered.add(x) if inside: return [] return p def main(): while 1: try: (n,) = ints() except EOFError: break ans = solve(ints()) if not ans: print("-1") else: print(len(ans)) print(*ans) return main()
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN LIST RETURN VAR FUNC_DEF WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) t = [int(s) for s in input().split()] la = set() pl = set() bon = True las = 0 rep = [] for i in range(n): e = abs(t[i]) if t[i] > 0: if e in la or e in pl: bon = False break else: la.add(e) elif e in la: la.remove(e) pl.add(e) if len(la) == 0: la = set() pl = set() rep.append(i + 1 - las) las = i + 1 else: bon = False break if len(la) == 0 and len(pl) == 0 and bon: print(len(rep)) for i in rep: print(i) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
from itertools import accumulate n = int(input()) a = [*map(int, input().split())] aa = [*accumulate(a)] empc = 0 day = set() ans = [] for i, j in zip(a, aa): if i in day or i < 0 and -i not in day: print(-1) exit() else: day.add(i) empc += 1 if j == 0: ans.append(empc) empc = 0 day.clear() if len(day) == 0: print(len(ans)) print(*ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) A = list(map(int, input().split())) now = set() used = set() pre = -1 ans = [] for i, a in enumerate(A): if a > 0: if a in now or a in used: print(-1) exit() else: now.add(a) used.add(a) elif -a not in now: print(-1) exit() else: now.remove(-a) if not now: ans.append(i - pre) pre = i used = set() if now: print(-1) exit() print(len(ans)) print(*ans)
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) t = list(map(int, input().split(" "))) sum_ = 0 elements = set() err_, events = False, 0 result = [] for index in range(n): element = t[index] if element > 0 and element not in elements: elements.add(element) sum_ += element elif element < 0 and -element in elements: sum_ += element else: err_ = True break events += 1 if sum_ == 0: result.append(events) events = 0 elements = set() if err_ or sum_ != 0: print(-1) else: print(len(result)) print(*result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) ln = [int(i) for i in input().split(" ")] seen = {} sm = 0 f = True tk = 0 pts = [] for i in range(0, len(ln)): sm += ln[i] if ln[i] < 0 and -ln[i] not in seen: f = False if ln[i] in seen: seen[ln[i]] += 1 f = False break else: seen[ln[i]] = 1 tk += 1 if sm == 0: seen = {} pts.append(tk) tk = 0 if sm != 0: f = False if not f: print(-1) else: print(len(pts)) print(" ".join([str(i) for i in pts]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
input() ans = [] cur = 0 status = set() were = set() try: for ai in map(int, input().split()): cur += 1 if ai < 0: status.remove(abs(ai)) if not status: ans.append(cur) cur = 0 were = set() else: assert ai not in were status.add(ai) were.add(ai) assert not were print(len(ans)) print(*ans) except (AssertionError, KeyError): print("-1")
EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = list(map(int, input().split())) b = [0] * (10**6 + 1) ans = [] num = 0 p = {} poor = 0 start = 0 for i in range(n): if a[i] > 0: if not a[i] in p: p[a[i]] = 1 num += 1 start += 1 else: poor = 1 break elif abs(a[i]) in p: if p[abs(a[i])] == 1: num += 1 start -= 1 p[abs(a[i])] -= 1 else: poor = 1 break if start == 0: ans.append(num) p = {} num = 0 if poor == 1 or num != 0: print(-1) else: print(len(ans)) print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The Central Company has an office with a sophisticated security system. There are $10^6$ employees, numbered from $1$ to $10^6$. The security system logs entrances and departures. The entrance of the $i$-th employee is denoted by the integer $i$, while the departure of the $i$-th employee is denoted by the integer $-i$. The company has some strict rules about access to its office: An employee can enter the office at most once per day. He obviously can't leave the office if he didn't enter it earlier that day. In the beginning and at the end of every day, the office is empty (employees can't stay at night). It may also be empty at any moment of the day. Any array of events satisfying these conditions is called a valid day. Some examples of valid or invalid days: $[1, 7, -7, 3, -1, -3]$ is a valid day ($1$ enters, $7$ enters, $7$ leaves, $3$ enters, $1$ leaves, $3$ leaves). $[2, -2, 3, -3]$ is also a valid day. $[2, 5, -5, 5, -5, -2]$ is not a valid day, because $5$ entered the office twice during the same day. $[-4, 4]$ is not a valid day, because $4$ left the office without being in it. $[4]$ is not a valid day, because $4$ entered the office and didn't leave it before the end of the day. There are $n$ events $a_1, a_2, \ldots, a_n$, in the order they occurred. This array corresponds to one or more consecutive days. The system administrator erased the dates of events by mistake, but he didn't change the order of the events. You must partition (to cut) the array $a$ of events into contiguous subarrays, which must represent non-empty valid days (or say that it's impossible). Each array element should belong to exactly one contiguous subarray of a partition. Each contiguous subarray of a partition should be a valid day. For example, if $n=8$ and $a=[1, -1, 1, 2, -1, -2, 3, -3]$ then he can partition it into two contiguous subarrays which are valid days: $a = [1, -1~ \boldsymbol{|}~ 1, 2, -1, -2, 3, -3]$. Help the administrator to partition the given array $a$ in the required way or report that it is impossible to do. Find any required partition, you should not minimize or maximize the number of parts. -----Input----- The first line contains a single integer $n$ ($1 \le n \le 10^5$). The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^6 \le a_i \le 10^6$ and $a_i \neq 0$). -----Output----- If there is no valid partition, print $-1$. Otherwise, print any valid partition in the following format: On the first line print the number $d$ of days ($1 \le d \le n$). On the second line, print $d$ integers $c_1, c_2, \ldots, c_d$ ($1 \le c_i \le n$ and $c_1 + c_2 + \ldots + c_d = n$), where $c_i$ is the number of events in the $i$-th day. If there are many valid solutions, you can print any of them. You don't have to minimize nor maximize the number of days. -----Examples----- Input 6 1 7 -7 3 -1 -3 Output 1 6 Input 8 1 -1 1 2 -1 -2 3 -3 Output 2 2 6 Input 6 2 5 -5 5 -5 -2 Output -1 Input 3 -8 1 1 Output -1 -----Note----- In the first example, the whole array is a valid day. In the second example, one possible valid solution is to split the array into $[1, -1]$ and $[1, 2, -1, -2, 3, -3]$ ($d = 2$ and $c = [2, 6]$). The only other valid solution would be to split the array into $[1, -1]$, $[1, 2, -1, -2]$ and $[3, -3]$ ($d = 3$ and $c = [2, 4, 2]$). Both solutions are accepted. In the third and fourth examples, we can prove that there exists no valid solution. Please note that the array given in input is not guaranteed to represent a coherent set of events.
n = int(input()) a = [int(x) for x in input().split()] b = [0] * (10**6 + 1) tmp = 0 res = [] tmp2 = [] d = True for i in range(n): if a[i] > 0: if b[a[i]] == 0: b[a[i]] += 1 tmp += 1 tmp2.append(a[i]) else: d = False break elif b[-a[i]] == 1: tmp -= 1 b[-a[i]] += 1 if tmp == 0: for j in tmp2: b[j] = 0 tmp2 = [] res.append(i + 1) else: d = False break if d and tmp == 0: for i in range(len(res) - 1, 0, -1): res[i] -= res[i - 1] print(len(res)) print(*res) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out. You are given two strings $s$ and $t$ of the same length $n$. Their characters are numbered from $1$ to $n$ from left to right (i.e. from the beginning to the end). In a single move you can do the following sequence of actions: choose any valid index $i$ ($1 \le i \le n$), move the $i$-th character of $s$ from its position to the beginning of the string or move the $i$-th character of $s$ from its position to the end of the string. Note, that the moves don't change the length of the string $s$. You can apply a move only to the string $s$. For example, if $s=$"test" in one move you can obtain: if $i=1$ and you move to the beginning, then the result is "test" (the string doesn't change), if $i=2$ and you move to the beginning, then the result is "etst", if $i=3$ and you move to the beginning, then the result is "stet", if $i=4$ and you move to the beginning, then the result is "ttes", if $i=1$ and you move to the end, then the result is "estt", if $i=2$ and you move to the end, then the result is "tste", if $i=3$ and you move to the end, then the result is "tets", if $i=4$ and you move to the end, then the result is "test" (the string doesn't change). You want to make the string $s$ equal to the string $t$. What is the minimum number of moves you need? If it is impossible to transform $s$ to $t$, print -1. -----Input----- The first line contains integer $q$ ($1 \le q \le 100$) β€” the number of independent test cases in the input. Each test case is given in three lines. The first line of a test case contains $n$ ($1 \le n \le 100$) β€” the length of the strings $s$ and $t$. The second line contains $s$, the third line contains $t$. Both strings $s$ and $t$ have length $n$ and contain only lowercase Latin letters. There are no constraints on the sum of $n$ in the test (i.e. the input with $q=100$ and all $n=100$ is allowed). -----Output----- For every test print minimum possible number of moves, which are needed to transform $s$ into $t$, or -1, if it is impossible to do. -----Examples----- Input 3 9 iredppipe piedpiper 4 estt test 4 tste test Output 2 1 2 Input 4 1 a z 5 adhas dasha 5 aashd dasha 5 aahsd dasha Output -1 2 2 3 -----Note----- In the first example, the moves in one of the optimal answers are: for the first test case $s=$"iredppipe", $t=$"piedpiper": "iredppipe" $\rightarrow$ "iedppiper" $\rightarrow$ "piedpiper"; for the second test case $s=$"estt", $t=$"test": "estt" $\rightarrow$ "test"; for the third test case $s=$"tste", $t=$"test": "tste" $\rightarrow$ "etst" $\rightarrow$ "test".
t = int(input()) for h in range(t): ans = 1000000000 n = int(input()) a = str(input()) b = str(input()) if sorted(a) != sorted(b): ans = -1 else: ans = 10000000000000 for i in range(n): k = i count = 0 for j in range(n): if k < n and a[j] == b[k]: k += 1 count += 1 ans = min(ans, n - count) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
The problem was inspired by Pied Piper story. After a challenge from Hooli's compression competitor Nucleus, Richard pulled an all-nighter to invent a new approach to compression: middle-out. You are given two strings $s$ and $t$ of the same length $n$. Their characters are numbered from $1$ to $n$ from left to right (i.e. from the beginning to the end). In a single move you can do the following sequence of actions: choose any valid index $i$ ($1 \le i \le n$), move the $i$-th character of $s$ from its position to the beginning of the string or move the $i$-th character of $s$ from its position to the end of the string. Note, that the moves don't change the length of the string $s$. You can apply a move only to the string $s$. For example, if $s=$"test" in one move you can obtain: if $i=1$ and you move to the beginning, then the result is "test" (the string doesn't change), if $i=2$ and you move to the beginning, then the result is "etst", if $i=3$ and you move to the beginning, then the result is "stet", if $i=4$ and you move to the beginning, then the result is "ttes", if $i=1$ and you move to the end, then the result is "estt", if $i=2$ and you move to the end, then the result is "tste", if $i=3$ and you move to the end, then the result is "tets", if $i=4$ and you move to the end, then the result is "test" (the string doesn't change). You want to make the string $s$ equal to the string $t$. What is the minimum number of moves you need? If it is impossible to transform $s$ to $t$, print -1. -----Input----- The first line contains integer $q$ ($1 \le q \le 100$) β€” the number of independent test cases in the input. Each test case is given in three lines. The first line of a test case contains $n$ ($1 \le n \le 100$) β€” the length of the strings $s$ and $t$. The second line contains $s$, the third line contains $t$. Both strings $s$ and $t$ have length $n$ and contain only lowercase Latin letters. There are no constraints on the sum of $n$ in the test (i.e. the input with $q=100$ and all $n=100$ is allowed). -----Output----- For every test print minimum possible number of moves, which are needed to transform $s$ into $t$, or -1, if it is impossible to do. -----Examples----- Input 3 9 iredppipe piedpiper 4 estt test 4 tste test Output 2 1 2 Input 4 1 a z 5 adhas dasha 5 aashd dasha 5 aahsd dasha Output -1 2 2 3 -----Note----- In the first example, the moves in one of the optimal answers are: for the first test case $s=$"iredppipe", $t=$"piedpiper": "iredppipe" $\rightarrow$ "iedppiper" $\rightarrow$ "piedpiper"; for the second test case $s=$"estt", $t=$"test": "estt" $\rightarrow$ "test"; for the third test case $s=$"tste", $t=$"test": "tste" $\rightarrow$ "etst" $\rightarrow$ "test".
from sys import stdin, stdout def getminmove(s, t): len1 = len(s) len2 = len(t) if len1 != len2: return -1 sa = [0] * 26 ta = [0] * 26 for c in s: sa[ord(c) - ord("a")] += 1 for c in t: ta[ord(c) - ord("a")] += 1 for i in range(26): if sa[i] != ta[i]: return -1 res = 1000000000000000000000 for i in range(len(s)): k = i sum = 0 for j in range(len(t)): if k < len(s) and t[k] == s[j]: sum += 1 k += 1 res = min(len(s) - sum, res) return res q = int(stdin.readline()) for i in range(q): n = int(stdin.readline()) s = stdin.readline().strip() t = stdin.readline().strip() move = getminmove(s, t) stdout.write(str(move) + "\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): st = "" if D * 9 <= S: return -1 for i in range(D, 0, -1): if S == 1: if i == 1: st += "1" else: st += "0" elif S - 1 >= 9: st += "9" S -= 9 elif i == 1: st += str(S) else: st += str(S - 1) S -= S - 1 x = "" for i in range(D): if st[i + 1] != "9": x += str(int(st[i]) - 1) x += str(int(st[i + 1]) + 1) x += st[i + 2 :] break x += st[i] return int(x[::-1])
CLASS_DEF FUNC_DEF ASSIGN VAR STRING IF BIN_OP VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER VAR STRING VAR STRING IF BIN_OP VAR NUMBER NUMBER VAR STRING VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): t = 9 * D if S >= t or D == 1: return -1 else: a = [(9) for i in range(D)] rem = 9 * D - S j = 0 if rem <= 8: a[0] = a[0] - rem + 1 a[1] = a[1] - 1 else: a[0] = a[0] - 8 rem = rem - 8 j = j + 1 while rem > 0 and j < D: if rem <= 9: a[j] = a[j] - rem rem = 0 else: a[j] = a[j] - 9 rem = rem - 9 j = j + 1 if j == D - 1: a[j - 1] = a[j - 1] + 1 a[j] = a[j] - 1 else: a[j] = a[j] + 1 a[j + 1] = a[j + 1] - 1 ans = "" for k in a: ans = ans + str(k) ansf = int(ans) return ansf
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): l = [] if S < 10 and D == 1: return -1 if S >= 9 * D: return -1 for i in range(1, D): if S > 9: l.append(9) S = S - 9 continue if S <= 9: l.append(S - 1) S -= S - 1 l.append(S) if l[0] == 8: l[0] -= 1 l[1] += 1 else: for i, v in enumerate(l): if v != 9: l[i] += 1 l[i - 1] -= 1 break l.reverse() s = "" for v in l: s += str(v) return s
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D == 1: return -1 res = [0] * D res[0] = 1 t = 1 i = D - 1 while t < S: t += 9 - res[i] res[i] = 9 i -= 1 if i == -1: break if t <= S and i == -1: return -1 if t > S: i += 1 res[i] -= t - S i = min(i, D - 2) res[i] += 1 res[i + 1] -= 1 return int("".join(str(i) for i in res))
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, s, d): if d * 9 <= s: return -1 p = [1] for i in range(d - 1): p.append(0) s -= 1 for i in range(d - 1, -1, -1): if s > 9: p[i] += 9 s -= 9 else: p[i] += s s = 0 for i in range(d - 2, -1, -1): if p[i] != 9: p[i] += 1 p[i + 1] -= 1 break ans = "" for i in p: ans += str(i) return int(ans)
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D == 1: if S < 10: return S num = [(0) for i in range(D)] for i in range(D - 1, 0, -1): num[i] = min(S - 1, 9) S -= num[i] if S > 9: return -1 num[0] = S change = 0 for i in range(D - 1, 0, -1): if num[i] > 0 and num[i - 1] < 9: num[i] -= 1 num[i - 1] += 1 change = 1 n = [str(i) for i in num] return "".join(n) return -1
CLASS_DEF FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR RETURN NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): minD = S // 9 + 1 sum = S if D < minD: return -1 digits = "" res = 0 while S > 0: d = min(S, 9) S -= d digits = str(d) + digits n = len(digits) if n < D: if n < 2: return 2 * 10 + sum - 2 else: digits = str(int(digits[0]) - 1) + digits[1:] res = 10 ** (D - 1) + int( str(int(digits[0]) + 1) + str(int(digits[1]) - 1) + digits[2:] ) elif n > 2: res = int(str(int(digits[0]) + 1) + str(int(digits[1]) - 1) + digits[2:]) else: res = int(str(int(digits[0]) + 1) + str(int(digits[1]) - 1)) return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): a = [0] * D a[0] = 1 val = S - 1 if S >= 9 * D: return -1 for i in range(0, len(a)): a[-(i + 1)] += min(9, val) val -= a[-(i + 1)] for i in range(1, len(a)): if a[-i - 1] + 1 <= 9 and a[-i] - 1 >= 0: a[-i - 1] += 1 a[-i] -= 1 break return "".join(str(num) for num in a)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): temp = [None] * D su = S if S >= D * 9: return "-1" if D == 1: return -1 for i in range(D - 1, 0, -1): d = min(su - 1, 9) temp[i] = d su -= d temp[0] = su i = D - 1 while i > 0: if temp[i] != 0 and temp[i - 1] != 9: temp[i] -= 1 temp[i - 1] += 1 return "".join(str(i) for i in temp) i -= 1 return "-1"
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN STRING IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN STRING
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= D * 9: return "-1" if D == 1: return str(S) a = "" temp = S - 1 for i in range(D): if temp >= 9: a += "9" temp -= 9 else: a += str(temp) temp = 0 cnt = int(a[-1]) + 1 a = a[:-1] + str(cnt) a = a[::-1] pre = a[-1] for i in range(len(a) - 2, -1, -1): if pre != a[i]: a = a[:i] + str(int(a[i]) + 1) + str(int(a[i + 1]) - 1) + a[i + 2 :] break return a
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN STRING IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR STRING VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if 9 * D <= S: return -1 s = [(0) for i in range(D)] s[0] = 1 i = D - 1 S -= 1 for i in range(D - 1, -1, -1): if S >= 9: s[i] = 9 S -= 9 else: s[i] += S S = 0 for i in range(D - 1, 0, -1): if s[i] != s[i - 1]: s[i] -= 1 s[i - 1] += 1 break s = [str(i) for i in s] return "".join(s)
CLASS_DEF FUNC_DEF IF BIN_OP NUMBER VAR VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL STRING VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S == D and D == 1: return -1 if D * 9 <= S: return -1 ans = ["0"] * D S -= 1 ans[0] = "1" idx = -1 while S: if S >= 9: ans[idx] = "9" S -= 9 else: ans[idx] = str(int(ans[idx]) + S) S = 0 idx -= 1 for i in range(D - 1, 0, -1): if ans[i] != ans[i - 1]: ans[i] = str(int(ans[i]) - 1) ans[i - 1] = str(int(ans[i - 1]) + 1) break else: ans[1] = str(int(ans[1]) - 1) ans[0] = str(int(ans[0]) + 1) return int("".join(ans))
CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST STRING VAR VAR NUMBER ASSIGN VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): d = D arr = [0] * D arr[0] = 1 s = 1 while D > 0 and s != S: arr[D - 1] += min(S - s, 9) s += arr[D - 1] D -= 1 arr = [str(i) for i in arr] temp = arr flag = True for i in range(len(temp) - 2, -1, -1): if int(temp[i]) < 9: temp[i] = int(temp[i]) + 1 temp[i + 1] = int(temp[i + 1]) - 1 flag = False break sex = int("".join([str(i) for i in temp])) if len(str(sex)) > d or flag: return -1 return sex
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): num = S tot = D s = num - 1 flag = 0 num1 = [] if num < 9 and tot == 1: return num if num >= tot * 9: return -1 else: for i in range(tot - 1, 0, -1): if s >= 9: num1.append("9") s -= 9 else: num1.append(str(s)) s -= s num1.append(str(1 + s)) num1 = num1[::-1] for i in range(len(num1)): if num1[i] == "9": num1[i] = "8" num1[i - 1] = str(int(num1[i - 1]) + 1) flag = 1 break if flag == 1: ans = "".join(num1) ans = int(ans) else: ans = "".join(num1) ans = int(ans) + 9 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D == 1 or S == 1: return -1 k = S // 9 if k >= D: return -1 r = S % 9 if r == 0: r = 9 k -= 1 if k == 0: return 1 * 10 ** (D - 1) + 10 + (r - 2) else: return 1 * 10 ** (D - 1) + r * 10**k + 9 * 10 ** (k - 1) - 1
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, s, d): if s == d or s == d * 9 or s > d * 9 or s == 1: return -1 ans = "" f = True while s: val = min(9, s - 1) ans += str(val) s -= val d -= 1 if d == 1: ans += str(s) break res = "" seen = True pos = 0 for i in ans: if i != "9": break pos += 1 if pos: return ( ans[: pos - 1] + str(int(ans[pos - 1]) - 1) + str(int(ans[pos]) + 1) + ans[pos + 1 :] )[::-1] return ( ans[:pos] + str(int(ans[pos]) - 1) + str(int(ans[pos + 1]) + 1) + ans[pos + 2 :] )[::-1]
CLASS_DEF FUNC_DEF IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR RETURN BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= 9 * D: return -1 count = 0 result = [0] * D S -= 1 for i in range(D - 1, 0, -1): if S > 9: result[i] = 9 S -= 9 else: result[i] = S S = 0 result[0] = S + 1 i = D - 1 while result[i - 1] == 9 and result[i] == 9: i -= 1 result[i - 1] += 1 result[i] -= 1 return "".join(str(num) for num in result)
CLASS_DEF FUNC_DEF IF VAR BIN_OP NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= 9 * D: return -1 ans = [(0) for _ in range(D)] ans[0] = 1 S -= 1 for i in range(D - 1, -1, -1): if S == 0: break dif = min(S, 9 - ans[i]) ans[i] += dif S -= dif found = False for i in range(D - 2, -1, -1): if ans[i] != 9: ans[i] += 1 ans[i + 1] -= 1 found = True break if not found: return -1 res = "" for i in range(D): res += str(ans[i]) return res
CLASS_DEF FUNC_DEF IF VAR BIN_OP NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= D * 9: return "-1" if S <= 9 and D == 1: return str(S) if D == 1 and S >= 10: return "-1" dup = D S -= 1 ans = "" for i in range(D - 1, 0, -1): if S > 9: ans += str(9) S -= 9 else: ans += str(S) S = 0 ans += str(S + 1) ans = ans[::-1] ind = dup - 1 for i in range(dup - 1, -1, -1): if ans[i] == "9": ind = i else: break ans = list(ans) ans[ind] = str(int(ans[ind]) - 1) ans[ind - 1] = str(int(ans[ind - 1]) + 1) ans = "".join(ans) return ans
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN STRING IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def sm_num(self, S, D): kll = S - 1 ioo = "" nu = kll // 9 rem = kll % 9 if nu > 0: rem += 1 ioo = (nu - 1) * "9" ioo = str(rem) + "8" + ioo elif rem > 0: ioo = str(1) + str(rem - 1) if len(ioo) < D: ioo = (D - len(ioo)) * "0" + ioo ioo = str(int(ioo[0]) + 1) + ioo[1 : len(ioo)] return ioo def secondSmallest(self, S, D): if S == 1 or S >= 9 * D or D == 1: return -1 else: return self.sm_num(S, D)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, s, d): if d == 1: return -1 else: if d * 9 < s: return -1 r = "" for i in range(d - 1, -1, -1): e = i * 9 if e - s >= 0 and i == d - 1: r += "1" s -= 1 elif e - s >= 0: r += "0" else: r += str(s - e) s -= s - e c = 0 for i in range(len(r) - 1, -1, -1): if r[i] == "9": c += 1 else: break if c == 0 or c == 1: result = int(r) + 9 else: result = int(r) + 9 * 10 ** (c - 1) if len(str(result)) == d: return result else: return -1
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR STRING VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR STRING VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= D * 9: return -1 if S <= 9 and D == 1: return S S -= 1 ans = [] while D: if S >= 9: ans += [9] S -= 9 else: ans += [S] S = 0 D -= 1 ans[-1] += 1 ans.reverse() n = len(ans) for i in range(n - 2, -1, -1): if ans[i] < 9: ans[i] += 1 ans[i + 1] -= 1 break return "".join([str(i) for i in ans])
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR IF VAR NUMBER VAR LIST NUMBER VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S <= 9 and D == 1: return S if S >= 9 * D: return -1 ret = [] while S: if S >= 9: ret.append(9) S -= 9 else: ret.append(S) S = 0 if len(ret) < D: ret[-1] -= 1 while len(ret) < D: ret.append(0) ret[-1] += 1 for i in range(D): if ret[i] != 9: if i >= 1: ret[i] += 1 ret[i - 1] -= 1 else: ret[i] -= 1 ret[i + 1] += 1 break return int("".join(map(str, ret[::-1])))
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR BIN_OP NUMBER VAR RETURN NUMBER ASSIGN VAR LIST WHILE VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, s, D): num = [None] * D for i in range(D - 1, 0, -1): d = min(s - 1, 9) num[i] = d s -= d if s > 9: return "-1" num[0] = s i = D - 1 while i > 0: if num[i] != 0 and num[i - 1] != 9: num[i] -= 1 num[i - 1] += 1 return "".join(str(x) for x in num) i -= 1 return "-1"
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN STRING
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): d = [0] * D if S >= 9 * D: return -1 if D == 1: return S d[0] = 1 S -= 1 count = 0 for i in range(D - 1, -1, -1): if S > 0: if S < 9: d[i] += S else: d[i] = 9 count += 1 S -= 9 else: break var = "" for j in d: var += str(j) if count == 0 or count == 1: t = int(var) + 9 else: t = int(var) + 9 * pow(10, count - 1) return t
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR BIN_OP NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= D * 9 or D == 1: return -1 arr = [] while D > 1: if S == 1: arr.append(0) else: temp = min(9, S - 1) arr.append(temp) S -= temp D -= 1 arr.append(S) if arr == [1, 1]: arr[0], arr[1] = 2, 0 else: for i in range(len(arr) - 1): if arr[i] > arr[i + 1]: arr[i] -= 1 arr[i + 1] += 1 break output = "" for n in arr: output += str(n) return output[::-1]
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST WHILE VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= 9 * D: return "-1" num = [0] * D num[0] = 1 S -= 1 for i in range(D - 1, -1, -1): num[i] += min(S, 9) S -= min(S, 9) if S == 0: break for i in range(D - 2, -1, -1): if num[i] < num[i + 1]: num[i] += 1 num[i + 1] -= 1 return "".join(list(map(str, num))) return "-1"
CLASS_DEF FUNC_DEF IF VAR BIN_OP NUMBER VAR RETURN STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN STRING
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D == 1 or S == 1 or S >= 9 * D: return "-1" isChanged = False sb = [] for i in range(0, D): digit = 0 if S > 9: digit = 9 S -= 9 elif i == D - 1: digit = S elif S > 1: digit = S - 1 S = 1 else: digit = 0 if not isChanged and digit < 9 and len(sb) > 0: digit += 1 c = sb[len(sb) - 1] sb[len(sb) - 1] = c - 1 isChanged = True sb.append(digit) s = "" for i in range(len(sb) - 1, -1, -1): s += str(sb[i]) return s
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
import sys class Solution: def secondSmallest(self, S, D): if D < 2 or S < 2 or S > 9 * D - 1: return "-1" q, r = divmod(S, 9) digits = [9] * q + [r] + [0] * (D - 1 - q) lnz = q - (r == 0) digits[lnz] -= 1 digits[-1] += 1 if lnz == 0: lnz = 1 digits[lnz] += 1 digits[lnz - 1] -= 1 return "".join(map(str, reversed(digits)))
IMPORT CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST NUMBER VAR LIST VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D == 1 or S == 1: return "-1" ans = ["0"] * D ans[0] = "1" S -= 1 i = D - 1 while i > 0: if S > 9: ans[i] = "9" S -= 9 else: ans[i] = str(S) S = 0 break i -= 1 if S > 7: return "-1" if S: ans[0] = str(S + 2) ans[1] = "8" return "".join(ans) else: if ans[i] < "9": if i + 1 < D: ans[i] = str(int(ans[i]) + 1) ans[i + 1] = "8" else: ans[i - 1] = str(int(ans[i - 1]) + 1) ans[i] = str(int(ans[i]) - 1) else: ans[i - 1] = str(int(ans[i - 1]) + 1) ans[i] = str(int(ans[i]) - 1) return "".join(ans)
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN STRING IF VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER STRING RETURN FUNC_CALL STRING VAR IF VAR VAR STRING IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL STRING VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D < 2 or 9 * D - 1 < S: return -1 t = "" s = 0 for i in range(1, 9): if (S - i) / (D - 1) <= 9.0: t = t + str(i) s = s + i break for i in range(2, D): f = 0 j = 0 while j < 10 and f == 0: if (S - s - j) / (D - i) <= 9.0: t = t + str(j) s += j f = 1 j = j + 1 t = t + str(S - s) if S < 18: t = str(int(t) + 9) elif S % 9 != 0: t = str(int(t) + 9 * pow(10, int(S / 9) - 1)) else: t = str(int(t) + 9 * pow(10, int(S / 9) - 2)) return t
CLASS_DEF FUNC_DEF IF VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): ans = [] if D == 1 or S == 1 or D * 9 <= S: return "-1" for i in range(0, D): if S > 9: ans.append("9") S -= 9 elif S == 1: if i == D - 1: ans.append("1") else: ans.append("0") elif i == D - 1: ans.append(str(S)) else: ans.append(str(S - 1)) S = 1 st = [] ans = ans[::-1] for i in range(D - 2, -1, -1): if int(ans[i]) < int(ans[i + 1]): ans[i] = str(int(ans[i]) + 1) ans[i + 1] = str(int(ans[i + 1]) - 1) return "".join(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR RETURN STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if D == 1 and S <= 9: return S if S == 1 or S >= D * 9: return -1 sumthing = [(0) for i in range(D)] for i in range(D): if i == 0: assign = max(1, S - (D - 1 - i) * 9) else: assign = max(0, S - (D - 1 - i) * 9) sumthing[i] = assign S -= assign out = "" swap = False for i in range(D - 1, -1, -1): if not swap and i > 0 and sumthing[i - 1] < 9: sumthing[i] -= 1 sumthing[i - 1] += 1 swap = True out = str(sumthing[i]) + out return out
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= D * 9: return -1 ans = [(0) for i in range(D)] ans[0] = 1 S -= 1 D -= 1 ind = len(ans) - 1 for i in range(len(ans) - 1, -1, -1): if S >= 9: ans[i] += 9 S -= 9 ind = i else: ans[i] += S S = 0 ans[ind] = ans[ind] - 1 ans[ind - 1] = ans[ind - 1] + 1 return int("".join(map(str, ans)))
CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S / 9 > D: return -1 if S == 1: return -1 if S / 9 == D: return -1 if D == 1: return S i = D - 1 curr = S ans = [(0) for _ in range(D)] nines = 0 while curr > 1: if curr > 9: ans[i] = 9 curr -= 9 nines += 1 else: ans[i] = curr - 1 curr = 1 i -= 1 ans[0] += 1 ans[D - nines] += 9 for i in range(D - nines, 0, -1): remainder = ans[i] % 10 carry = ans[i] // 10 ans[i] = remainder ans[i - 1] += carry if ans[0] > 9: return -1 return "".join(str(x) for x in ans)
CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER NUMBER RETURN NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S >= 9 * D: return -1 dig = [1] + [0] * (D - 1) S = S - 1 for i in range(D - 1, -1, -1): if S >= 9: dig[i] += 9 S = S - 9 if S == 0: dig[i] -= 1 dig[i - 1] += 1 break else: dig[i] += S S = 0 if i == D - 1: dig[i] -= 1 dig[i - 1] += 1 else: dig[i] += 1 dig[i + 1] -= 1 break ans = "" for i in range(D): ans = ans + str(dig[i]) return int(ans)
CLASS_DEF FUNC_DEF IF VAR BIN_OP NUMBER VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, s, d): if s >= 9 * d or s == 1: return -1 a = [(0) for i in range(d)] a[0] = 1 s = s - 1 i = d - 1 while i >= 0 and s > 0: if s >= 9: a[i] += 9 s -= 9 else: a[i] += s break i -= 1 i, j = d - 1, d - 2 while j >= 0: if a[i] == a[j] == 9: i -= 1 j -= 1 else: break a[j] += 1 a[i] -= 1 ans = "" for i in a: ans += str(i) return int(ans)
CLASS_DEF FUNC_DEF IF VAR BIN_OP NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if 9 * D < S: return -1 S = S - 1 ans = "" for i in range(D - 1, 0, -1): if S > 9: ans = "9" + ans S = S - 9 elif i == 0: ans = str(S + 1) + ans else: ans = str(S) + ans S = 0 ans = str(S + 1) + ans if int(10**D - 1) == int(ans): return -1 l = list(ans) idx = D - 1 for i in range(D - 1, 0, -1): if l[i] == "9": idx = i else: break l[idx] = str(int(l[idx]) - 1) l[idx - 1] = str(int(l[idx - 1]) + 1) ans = "".join(l) return ans
CLASS_DEF FUNC_DEF IF BIN_OP NUMBER VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL STRING VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): n = [0] * D n[D - 1] = min(S - 2, 8) if n[D - 1] == -1: return -1 S -= n[D - 1] for j in reversed(range(1, D - 1)): if S - 1 > 9: n[j] = 8 n[j + 1] = n[j + 1] + 1 S -= 9 else: c = max(0, min(S - 1, 9)) S -= c n[j] = c if S < 10 and S > 0: n[0] = S return "".join(map(str, n)) return -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN NUMBER
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): ans = ["0"] * D if D * 9 <= S: return "-1" S = S - 1 e = D - 1 for i in range(D - 1, 0, -1): if S >= 9: ans[i] = "9" S = S - 9 e = i else: ans[i] = str(S) S = 0 ans[0] = str(1 + S) ans[e] = str(int(ans[e]) - 1) ans[e - 1] = str(int(ans[e - 1]) + 1) return "".join(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST STRING VAR IF BIN_OP VAR NUMBER VAR RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL STRING VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
import sys class Solution: def secondSmallest(self, S, D): if D * 9 <= S or D == 1 or S == 1: return "-1" num = [0] * D num[0] = 1 S = S - 1 for i in range(D - 1, 0, -1): if S > 9: num[i] = 9 S = S - 9 else: num[i] = S S = 0 if S != 0: num[0] += S for i in range(D - 1, 0, -1): if num[i] <= 9 and num[i - 1] != 9: num[i - 1] += 1 num[i] -= 1 break s = "" for i in num: s += str(i) return s
IMPORT CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): if S == 1 or D == 1: return -1 digits = [] for i in range(D): digits.append(0) digits[0] = 1 index = D - 1 S -= 1 while S > 0: S -= 9 if S >= 0: if index == 0: return -1 digits[index] = 9 index -= 1 else: digits[index] += 9 + S if index != D - 1: index += 1 digits[index] -= 1 digits[index - 1] += 1 if digits[index - 1] > 9: return -1 result = "" for i in range(D): result += str(digits[i]) return result
CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. Example 1: Input: S = 9 D = 2 Output: 27 Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Example 2: Input: S = 16 D = 3 Output: 178 Explanation: 169 is the smallest number possible with sum is 16 and total digits = 3, Whereas the second smallest is 178. Your Task: You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". Expected Time Complexity: O(D) Expected Space Complexity: O(1) Constraints: 1 ≀ S ≀ 10^{5} 1 ≀ D ≀ 10^{5}
class Solution: def secondSmallest(self, S, D): s = S st = "" if S >= 9 * D or D == 1 or S == 0: return -1 d = D S -= 1 for i in range(d - 1): if S > 9 and D > 1: S -= 9 st += str(9) D -= 1 else: st += str(S) S = 0 st += str(S + 1) fin = st[::-1] ans = int(fin) new = 10 ** (s // 9 - 1) if s % 9 == 0 and new > 1: new /= 10 if new <= 1: ans += 9 else: ans += new * 9 return int(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR STRING IF VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
MOD = 1000000007 ii = lambda: int(input()) si = lambda: input() dgl = lambda: list(map(int, input())) f = lambda: map(int, input().split()) il = lambda: list(map(int, input().split())) ls = lambda: list(input()) n, v = f() l = [0] * 3003 for _ in range(n): a, b = f() l[a] += b fromprevDay = 0 currDay = 0 temp = 0 ans = 0 for i in range(1, 3002): currDay = l[i] if fromprevDay + currDay <= v: ans += fromprevDay + currDay fromprevDay = 0 elif fromprevDay >= v: ans += v fromprevDay = currDay else: ans += v temp = v - fromprevDay fromprevDay = currDay - temp print(ans)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
def main(): n, v = map(int, input().split()) l = [0] * 3002 for _ in range(n): a, b = map(int, input().split()) l[a] += b a = res = 0 for b in l: x = a + b if x < v: res += x a = 0 else: res += v if a < v: a = x - v else: a = b print(res) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, v = map(int, input().split()) f = [0] * 3002 for _ in range(n): a, b = map(int, input().split()) f[a] += b ans = 0 cur = 0 for i in range(1, 3002): if cur + f[i] <= v: ans += cur + f[i] cur = 0 else: ans += v cur = f[i] - max(v - cur, 0) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, limit = map(int, input().split()) arr = [0] * (3000 + 1) for i in range(n): a, b = map(int, input().split()) arr[a] += b next = 0 fruits = 0 for i in range(1, 3001): v = limit temp = min(v, next) fruits += temp v -= temp temp = min(v, arr[i]) fruits += temp next = arr[i] - temp v = limit fruits += min(v, next) print(fruits)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, v = [int(x) for x in input().split()] l = [0] * 3002 for i in range(n): a, b = [int(x) for x in input().split()] l[a] += b ans = 0 for i in range(1, 3002): left = v if l[i - 1]: ans += min(left, l[i - 1]) left -= min(left, l[i - 1]) ans += min(left, l[i]) l[i] -= min(left, l[i]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
rip = [0] * 3050 n, v = [int(x) for x in input().split()] for _ in range(n): d, p = [int(x) for x in input().split()] rip[d] += p ans = 0 for i in range(1, 3050): t = min(v, rip[i - 1] + rip[i]) rip[i] = rip[i] - max(0, t - rip[i - 1]) ans += t print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, v = [int(i) for i in input().split()] a = [0] * n b = [0] * n days1 = [0] * 3001 days2 = [0] * 3001 for i in range(n): a[i], b[i] = [int(j) for j in input().split()] days1[a[i] - 1] += b[i] days2[a[i]] += b[i] res = 0 for i in range(3001): left = max(0, v - days2[i]) res += max(0, min(v, days2[i])) res += min(left, days1[i]) if i != 3000: days2[i + 1] -= min(left, days1[i]) print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, v = map(int, input().split(" ")) trees = dict() count = 0 b_last = 0 for i in range(n): a, b = map(int, input().split(" ")) if trees.get(a): trees[a] += b else: trees[a] = b m = max(trees.keys()) for i in range(1, m + 2): if trees.get(i): k = min(v, b_last) count += k k1 = min(v - k, trees[i]) count += k1 b_last = trees[i] - k1 else: count += min(v, b_last) b_last = 0 print(count)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
read = lambda: map(int, input().split()) n, v = read() N = 3002 p = [0] * N for i in range(n): a, b = read() p[a] += b ans = 0 rem = 0 for i in range(1, N): cur = min(v, rem + p[i]) ans += cur if rem >= v: rem = p[i] else: rem = max(0, p[i] - (v - rem)) print(ans)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, v = map(int, input().split()) d, s = {}, 0 for i in range(n): a, b = map(int, input().split()) d[a] = d.get(a, 0) + b s += b r = v for i in sorted(d.keys()): d[i] -= min(d[i], r) r, b = v, min(d[i], v) d[i] -= b if i + 1 in d: r -= b print(s - sum(d.values()))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR
Valera loves his garden, where n fruit trees grow. This year he will enjoy a great harvest! On the i-th tree b_{i} fruit grow, they will ripen on a day number a_{i}. Unfortunately, the fruit on the tree get withered, so they can only be collected on day a_{i} and day a_{i} + 1 (all fruits that are not collected in these two days, become unfit to eat). Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than v fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well? -----Input----- The first line contains two space-separated integers n and v (1 ≀ n, v ≀ 3000) β€” the number of fruit trees in the garden and the number of fruits that Valera can collect in a day. Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers a_{i} and b_{i} (1 ≀ a_{i}, b_{i} ≀ 3000) β€” the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree. -----Output----- Print a single integer β€” the maximum number of fruit that Valera can collect. -----Examples----- Input 2 3 1 5 2 3 Output 8 Input 5 10 3 20 2 20 1 20 4 20 5 20 Output 60 -----Note----- In the first sample, in order to obtain the optimal answer, you should act as follows. On the first day collect 3 fruits from the 1-st tree. On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree. On the third day collect the remaining fruits from the 2-nd tree. In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
n, v = map(int, input().split(" ")) a, b = [], [] for i in range(n): x, y = map(int, input().split(" ")) a.append(x) b.append(y) fi = max(a) li = [0] * 3002 for i in range(n): li[a[i]] += b[i] ans = 0 for i in range(1, 3002): ans += min(v, li[i - 1] + li[i]) li[i] = max(0, li[i] - max(0, v - li[i - 1])) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR