description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | t = int(input())
for i in range(t):
s = str(input())
t = str(input())
x = len(s)
y = len(t)
count1 = [0] * 26
count2 = [0] * 26
z = 0
matrix1 = [[] for j in range(26)]
for j in range(x):
count1[ord(s[j]) - 97] += 1
matrix1[ord(s[j]) - 97].append(j + 1)
for j in range(y):
count2[ord(t[j]) - 97] += 1
for j in range(26):
if count2[j] != 0 and count1[j] == 0:
z += 1
break
if z == 1:
print(-1)
else:
matrix = [([0] * 26) for j in range(x)]
for j in range(26):
h = 0
l = len(matrix1[j])
for k in range(x):
if h == l:
break
elif h < l and matrix1[j][h] > k + 1:
matrix[k][j] = matrix1[j][h]
else:
while h < l:
if matrix1[j][h] > k + 1:
matrix[k][j] = matrix1[j][h]
break
h += 1
ans = 1
j = 0
lis = [0]
g = 0
while j < y:
if g == 0:
if t[j] == s[0]:
lis.append(0)
j += 1
g += 1
else:
temp = matrix[lis[-1]][ord(t[j]) - 97]
if temp != 0:
lis.append(temp - 1)
j += 1
g += 1
else:
g = 0
lis = [0]
ans += 1
else:
temp = matrix[lis[-1]][ord(t[j]) - 97]
if temp != 0:
lis.append(temp - 1)
j += 1
g += 1
else:
g = 0
lis = [0]
ans += 1
print(ans) | 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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def bsearch(a, x):
l = 0
r = len(a)
while l < r:
mid = (l + r) // 2
if a[mid] > x:
r = mid
else:
l = mid + 1
return l
for _ in range(int(input())):
s = input()
t = input()
Map = {}
for i in range(len(s)):
if s[i] not in Map:
Map[s[i]] = []
Map[s[i]].append(i)
flag = 0
c = 1
n = len(t)
i = 0
maxInd = -1
while i < n:
if t[i] not in Map:
flag = 1
print("-1")
break
if Map[t[i]][0] > maxInd:
maxInd = Map[t[i]][0]
i += 1
else:
find = bsearch(Map[t[i]], maxInd)
if find < len(Map[t[i]]):
maxInd = Map[t[i]][find]
i += 1
else:
c += 1
maxInd = -1
if flag == 0:
print(c) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | test = int(input())
for _ in range(test):
s = input()
t = input()
i1, i2 = 0, 0
s1, s2 = set(s), set(t)
if len(s2 - s1) > 0:
print(-1)
continue
dics = [(0) for i in range(len(s))]
sst = sorted(list(s))
main = {i: (0) for i in sst}
for i in range(len(s) - 1, -1, -1):
dics[i] = main.copy()
main[s[i]] = i
def nex(i, x):
val = dics[i][x]
if val == 0 and s[0] != x:
return dics[0][x], True
elif val == 0:
return 0, True
return val, False
i1, i2, ans = 0, 0, 1
while i1 < len(t):
if i2 >= len(s):
i2 = 0
ans += 1
if s[i2] != t[i1]:
i2, f = nex(i2, t[i1])
if f:
ans += 1
i2 += 1
i1 += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR RETURN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER NUMBER RETURN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | for _ in range(int(input())):
s = list(map(lambda c: ord(c) - 97, input()))
n = len(s)
t = list(map(lambda c: ord(c) - 97, input()))
if len(set(t) - set(s)) > 0:
print(-1)
continue
next_c = [-1] * 26
dp = [([-1] * 26) for _ in range(n)]
for i in range(n - 1, -1, -1):
next_c[s[i]] = i
for i in range(n - 1, -1, -1):
for j in range(26):
dp[i][j] = next_c[j]
next_c[s[i]] = i
pos = -1
ans = 1
for c in t:
if pos >= dp[pos][c]:
ans += 1
pos = dp[pos][c]
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
s = minp()
t = minp()
n = len(s)
m = len(t)
h = [None] * n
hh = [None] * 26
hh[ord(s[-1]) - ord("a")] = n - 1
h[-1] = hh
for i in range(n - 2, -1, -1):
hh = hh.copy()
hh[ord(s[i]) - ord("a")] = i
h[i] = hh
hh = [None] * 26
z = None
res = 0
for i in range(len(t)):
c = ord(t[i]) - ord("a")
if z == None:
z = 0
res += 1
ok = False
if h[z][c] != None:
z = h[z][c] + 1
ok = True
if z == n:
z = None
if not ok:
if h[0][c] == None:
print(-1)
return
res += 1
z = h[0][c] + 1
print(res)
for i in range(mint()):
solve() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR NONE ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NONE IF VAR IF VAR NUMBER VAR NONE EXPR FUNC_CALL VAR NUMBER RETURN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def getRow(c):
return ord(c) - 97
for t in range(int(input())):
s = input()
t = input()
inf = len(s)
grid = [([inf] * len(s)) for _ in range(26)]
grid[getRow(s[-1])][-1] = len(s) - 1
for col in reversed(range(len(s) - 1)):
for row in range(26):
grid[row][col] = grid[row][col + 1]
grid[getRow(s[col])][col] = col
res = 1
spot = 0
for c in t:
if spot >= inf:
res += 1
spot = 0
check = grid[getRow(c)][0]
if check >= inf:
res = -1
break
spot = grid[getRow(c)][spot]
if spot >= inf:
res += 1
spot = check
spot += 1
print(res) | FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | import sys
inf = float("inf")
input = lambda: sys.stdin.readline().rstrip("\r\n")
q = int(input())
for _ in range(q):
s = input()
t = input()
n = len(s)
loc = [[inf for __ in range(26)] for _ in range(n + 1)]
s2 = []
t2 = []
for c in s:
s2.append(ord(c) - ord("a"))
for c in t:
t2.append(ord(c) - ord("a"))
s = s2
t = t2
for j in range(n - 1, -1, -1):
for i in range(26):
loc[j][i] = loc[j + 1][i]
loc[j][s[j]] = j
possible = True
tptr = 0
nMoves = 0
while True:
nMoves += 1
sptr = 0
c = t[tptr]
if loc[sptr][c] == inf:
possible = False
break
while tptr < len(t) and loc[sptr][c] != inf:
sptr = loc[sptr][c] + 1
tptr += 1
if tptr == len(t):
break
c = t[tptr]
if tptr == len(t):
break
if possible:
print(nMoves)
else:
print(-1) | IMPORT ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
You are given two strings $s$ and $t$ consisting of lowercase Latin letters. Also you have a string $z$ which is initially empty. You want string $z$ to be equal to string $t$. You can perform the following operation to achieve this: append any subsequence of $s$ at the end of string $z$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $z = ac$, $s = abcde$, you may turn $z$ into following strings in one operation: $z = acace$ (if we choose subsequence $ace$); $z = acbcd$ (if we choose subsequence $bcd$); $z = acbce$ (if we choose subsequence $bce$).
Note that after this operation string $s$ doesn't change.
Calculate the minimum number of such operations to turn string $z$ into string $t$.
-----Input-----
The first line contains the integer $T$ ($1 \le T \le 100$) — the number of test cases.
The first line of each testcase contains one string $s$ ($1 \le |s| \le 10^5$) consisting of lowercase Latin letters.
The second line of each testcase contains one string $t$ ($1 \le |t| \le 10^5$) consisting of lowercase Latin letters.
It is guaranteed that the total length of all strings $s$ and $t$ in the input does not exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print one integer — the minimum number of operations to turn string $z$ into string $t$. If it's impossible print $-1$.
-----Example-----
Input
3
aabce
ace
abacaba
aax
ty
yyt
Output
1
-1
3 | def bin_poisk(q, arr):
l = 0
r = len(arr)
while r - l > 1:
x = (l + r) // 2
if arr[x] <= q:
l = x
else:
r = x
if arr[-1] <= q:
return -1
if arr[l] <= q:
if r == len(arr):
return arr[-1]
return arr[r]
return arr[l]
n = int(input())
for i in range(n):
d = dict()
nabor = input()
t = input()
e = len(t)
answer = 0
for j in range(len(nabor)):
if nabor[j] not in d:
d[nabor[j]] = []
d[nabor[j]].append(j)
o = -1
while o < e - 1:
answer += 1
w = -1
while o < e - 1:
if t[o + 1] not in d:
answer = -1
break
q = bin_poisk(w, d[t[o + 1]])
if q == -1:
break
w = q
o += 1
if answer == -1:
break
print(answer) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR NUMBER RETURN VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
Some time ago Lesha found an entertaining string $s$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.
Lesha chooses an arbitrary (possibly zero) number of pairs on positions $(i, i + 1)$ in such a way that the following conditions are satisfied: for each pair $(i, i + 1)$ the inequality $0 \le i < |s| - 1$ holds; for each pair $(i, i + 1)$ the equality $s_i = s_{i + 1}$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over.
Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.
-----Input-----
The only line contains the string $s$ ($1 \le |s| \le 10^5$) — the initial string consisting of lowercase English letters only.
-----Output-----
In $|s|$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $10$ characters, instead print the first $5$ characters, then "...", then the last $2$ characters of the answer.
-----Examples-----
Input
abcdd
Output
3 abc
2 bc
1 c
0
1 d
Input
abbcdddeaaffdfouurtytwoo
Output
18 abbcd...tw
17 bbcdd...tw
16 bcddd...tw
15 cddde...tw
14 dddea...tw
13 ddeaa...tw
12 deaad...tw
11 eaadf...tw
10 aadfortytw
9 adfortytw
8 dfortytw
9 fdfortytw
8 dfortytw
7 fortytw
6 ortytw
5 rtytw
6 urtytw
5 rtytw
4 tytw
3 ytw
2 tw
1 w
0
1 o
-----Note-----
Consider the first example.
The longest suffix is the whole string "abcdd". Choosing one pair $(4, 5)$, Lesha obtains "abc". The next longest suffix is "bcdd". Choosing one pair $(3, 4)$, we obtain "bc". The next longest suffix is "cdd". Choosing one pair $(2, 3)$, we obtain "c". The next longest suffix is "dd". Choosing one pair $(1, 2)$, we obtain "" (an empty string). The last suffix is the string "d". No pair can be chosen, so the answer is "d".
In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs $(11, 12)$, $(16, 17)$, $(23, 24)$ and we obtain "abbcdddeaadfortytw" | s = input()
cs = [""]
ans = []
last = [""]
poped = False
for c in s[::-1]:
if not poped and c == cs[-1] and c > last[-2]:
cs.pop()
if not cs or c != cs[-1]:
last.pop()
poped = True
else:
poped = False
if c != cs[-1]:
last.append(c)
cs.append(c)
if len(cs) <= 11:
ans.append((len(cs) - 1, "".join(cs[::-1])))
else:
ans.append((len(cs) - 1, ("".join(cs[:3]) + "..." + "".join(cs[-5:]))[::-1]))
for a, b in ans[::-1]:
print(a, b) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR LIST ASSIGN VAR LIST STRING ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL STRING VAR NUMBER STRING FUNC_CALL STRING VAR NUMBER NUMBER FOR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Some time ago Lesha found an entertaining string $s$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.
Lesha chooses an arbitrary (possibly zero) number of pairs on positions $(i, i + 1)$ in such a way that the following conditions are satisfied: for each pair $(i, i + 1)$ the inequality $0 \le i < |s| - 1$ holds; for each pair $(i, i + 1)$ the equality $s_i = s_{i + 1}$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over.
Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.
-----Input-----
The only line contains the string $s$ ($1 \le |s| \le 10^5$) — the initial string consisting of lowercase English letters only.
-----Output-----
In $|s|$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $10$ characters, instead print the first $5$ characters, then "...", then the last $2$ characters of the answer.
-----Examples-----
Input
abcdd
Output
3 abc
2 bc
1 c
0
1 d
Input
abbcdddeaaffdfouurtytwoo
Output
18 abbcd...tw
17 bbcdd...tw
16 bcddd...tw
15 cddde...tw
14 dddea...tw
13 ddeaa...tw
12 deaad...tw
11 eaadf...tw
10 aadfortytw
9 adfortytw
8 dfortytw
9 fdfortytw
8 dfortytw
7 fortytw
6 ortytw
5 rtytw
6 urtytw
5 rtytw
4 tytw
3 ytw
2 tw
1 w
0
1 o
-----Note-----
Consider the first example.
The longest suffix is the whole string "abcdd". Choosing one pair $(4, 5)$, Lesha obtains "abc". The next longest suffix is "bcdd". Choosing one pair $(3, 4)$, we obtain "bc". The next longest suffix is "cdd". Choosing one pair $(2, 3)$, we obtain "c". The next longest suffix is "dd". Choosing one pair $(1, 2)$, we obtain "" (an empty string). The last suffix is the string "d". No pair can be chosen, so the answer is "d".
In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs $(11, 12)$, $(16, 17)$, $(23, 24)$ and we obtain "abbcdddeaadfortytw" | def main(s):
if len(s) == 0:
return []
if len(s) == 1:
return [[s], [1]]
s = s[::-1]
sres = [s[0]]
nres = [1]
if s[0] == s[1]:
sres.append("")
nres.append(0)
else:
sres.append(s[1] + s[0])
nres.append(2)
for i in range(2, len(s)):
st = s[i] + sres[-1]
nt = nres[-1] + 1
if nt > 10:
st = st[:5] + "..." + st[-2:]
if s[i] == s[i - 1] and st > sres[-1]:
st = sres[-2]
nt = nres[-2]
sres.append(st)
nres.append(nt)
return [sres, nres]
s = input()
sres, nres = main(s)
for i in range(len(s) - 1, -1, -1):
print(nres[i], sres[i]) | FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN LIST IF FUNC_CALL VAR VAR NUMBER RETURN LIST LIST VAR LIST NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Some time ago Lesha found an entertaining string $s$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.
Lesha chooses an arbitrary (possibly zero) number of pairs on positions $(i, i + 1)$ in such a way that the following conditions are satisfied: for each pair $(i, i + 1)$ the inequality $0 \le i < |s| - 1$ holds; for each pair $(i, i + 1)$ the equality $s_i = s_{i + 1}$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over.
Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.
-----Input-----
The only line contains the string $s$ ($1 \le |s| \le 10^5$) — the initial string consisting of lowercase English letters only.
-----Output-----
In $|s|$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $10$ characters, instead print the first $5$ characters, then "...", then the last $2$ characters of the answer.
-----Examples-----
Input
abcdd
Output
3 abc
2 bc
1 c
0
1 d
Input
abbcdddeaaffdfouurtytwoo
Output
18 abbcd...tw
17 bbcdd...tw
16 bcddd...tw
15 cddde...tw
14 dddea...tw
13 ddeaa...tw
12 deaad...tw
11 eaadf...tw
10 aadfortytw
9 adfortytw
8 dfortytw
9 fdfortytw
8 dfortytw
7 fortytw
6 ortytw
5 rtytw
6 urtytw
5 rtytw
4 tytw
3 ytw
2 tw
1 w
0
1 o
-----Note-----
Consider the first example.
The longest suffix is the whole string "abcdd". Choosing one pair $(4, 5)$, Lesha obtains "abc". The next longest suffix is "bcdd". Choosing one pair $(3, 4)$, we obtain "bc". The next longest suffix is "cdd". Choosing one pair $(2, 3)$, we obtain "c". The next longest suffix is "dd". Choosing one pair $(1, 2)$, we obtain "" (an empty string). The last suffix is the string "d". No pair can be chosen, so the answer is "d".
In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs $(11, 12)$, $(16, 17)$, $(23, 24)$ and we obtain "abbcdddeaadfortytw" | import sys
z = sys.stdin.readline
s = z().strip()
l = len(s)
b = [" "]
o = []
m = [" "]
for i in range(l):
c = s[-1 - i]
if c == b[-1]:
if c > m[-1].lower() or c == m[-1].lower() and c > m[-2].lower():
b.pop()
if b[-1].lower() != m[-1].lower():
m.pop()
b[-1] = b[-1].upper()
m[-1] = m[-1].upper()
else:
b += [c]
else:
b += [c]
if b[-1] != m[-1].lower():
m += [b[-1]]
if len(b) > 11:
o += [
(str(len(b) - 1) + " " + "".join(b[:-6:-1]) + "..." + b[2] + b[1]).lower()
]
elif len(b) > 1:
o += [(str(len(b) - 1) + " " + "".join(b[-1::-1])).lower()]
else:
o.append("0")
print("\n".join(o[::-1])) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING ASSIGN VAR LIST ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER IF VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR NUMBER VAR LIST VAR VAR LIST VAR IF VAR NUMBER FUNC_CALL VAR NUMBER VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR LIST FUNC_CALL BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL STRING VAR NUMBER NUMBER STRING VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR LIST FUNC_CALL BIN_OP BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER |
Some time ago Lesha found an entertaining string $s$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.
Lesha chooses an arbitrary (possibly zero) number of pairs on positions $(i, i + 1)$ in such a way that the following conditions are satisfied: for each pair $(i, i + 1)$ the inequality $0 \le i < |s| - 1$ holds; for each pair $(i, i + 1)$ the equality $s_i = s_{i + 1}$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over.
Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.
-----Input-----
The only line contains the string $s$ ($1 \le |s| \le 10^5$) — the initial string consisting of lowercase English letters only.
-----Output-----
In $|s|$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $10$ characters, instead print the first $5$ characters, then "...", then the last $2$ characters of the answer.
-----Examples-----
Input
abcdd
Output
3 abc
2 bc
1 c
0
1 d
Input
abbcdddeaaffdfouurtytwoo
Output
18 abbcd...tw
17 bbcdd...tw
16 bcddd...tw
15 cddde...tw
14 dddea...tw
13 ddeaa...tw
12 deaad...tw
11 eaadf...tw
10 aadfortytw
9 adfortytw
8 dfortytw
9 fdfortytw
8 dfortytw
7 fortytw
6 ortytw
5 rtytw
6 urtytw
5 rtytw
4 tytw
3 ytw
2 tw
1 w
0
1 o
-----Note-----
Consider the first example.
The longest suffix is the whole string "abcdd". Choosing one pair $(4, 5)$, Lesha obtains "abc". The next longest suffix is "bcdd". Choosing one pair $(3, 4)$, we obtain "bc". The next longest suffix is "cdd". Choosing one pair $(2, 3)$, we obtain "c". The next longest suffix is "dd". Choosing one pair $(1, 2)$, we obtain "" (an empty string). The last suffix is the string "d". No pair can be chosen, so the answer is "d".
In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs $(11, 12)$, $(16, 17)$, $(23, 24)$ and we obtain "abbcdddeaadfortytw" | s = input()
n = len(s)
res = ["", s[-1]]
l = [1]
flag = True
last = [""]
for i in range(n - 2, -1, -1):
tmp = res[-1]
if flag and s[i] == tmp[0] and s[i] > last[-1]:
res.append(res[-2])
l.append(l[-1] - 1)
flag = False
if l[-1] > 0 and res[-1][0] != s[i]:
last.pop()
else:
l.append(l[-1] + 1)
tmp = s[i] + tmp
flag = True
if len(tmp) > 10:
res.append(tmp[:5] + "..." + tmp[-2:])
else:
res.append(tmp)
if len(res[-1]) > 1 and res[-1][0] != res[-1][1]:
last.append(res[-1][1])
res.pop(0)
for i in range(n - 1, -1, -1):
print(l[i], res[i]) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST STRING VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR |
Some time ago Lesha found an entertaining string $s$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.
Lesha chooses an arbitrary (possibly zero) number of pairs on positions $(i, i + 1)$ in such a way that the following conditions are satisfied: for each pair $(i, i + 1)$ the inequality $0 \le i < |s| - 1$ holds; for each pair $(i, i + 1)$ the equality $s_i = s_{i + 1}$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over.
Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.
-----Input-----
The only line contains the string $s$ ($1 \le |s| \le 10^5$) — the initial string consisting of lowercase English letters only.
-----Output-----
In $|s|$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $10$ characters, instead print the first $5$ characters, then "...", then the last $2$ characters of the answer.
-----Examples-----
Input
abcdd
Output
3 abc
2 bc
1 c
0
1 d
Input
abbcdddeaaffdfouurtytwoo
Output
18 abbcd...tw
17 bbcdd...tw
16 bcddd...tw
15 cddde...tw
14 dddea...tw
13 ddeaa...tw
12 deaad...tw
11 eaadf...tw
10 aadfortytw
9 adfortytw
8 dfortytw
9 fdfortytw
8 dfortytw
7 fortytw
6 ortytw
5 rtytw
6 urtytw
5 rtytw
4 tytw
3 ytw
2 tw
1 w
0
1 o
-----Note-----
Consider the first example.
The longest suffix is the whole string "abcdd". Choosing one pair $(4, 5)$, Lesha obtains "abc". The next longest suffix is "bcdd". Choosing one pair $(3, 4)$, we obtain "bc". The next longest suffix is "cdd". Choosing one pair $(2, 3)$, we obtain "c". The next longest suffix is "dd". Choosing one pair $(1, 2)$, we obtain "" (an empty string). The last suffix is the string "d". No pair can be chosen, so the answer is "d".
In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs $(11, 12)$, $(16, 17)$, $(23, 24)$ and we obtain "abbcdddeaadfortytw" | import sys
s = input().strip()
N = len(s)
if len(s) == 1:
print(1, s[0])
return
X = [s[-1], s[-2] + s[-1] if s[-2] != s[-1] else ""]
Y = [1, 2 if s[-2] != s[-1] else 0]
for i in range(N - 3, -1, -1):
c = s[i]
k1 = c + X[-1]
ng = Y[-1] + 1
if ng > 10:
k1 = k1[:5] + "..." + k1[-2:]
if c == s[i + 1] and k1 > X[-2]:
k1 = X[-2]
ng = Y[-2]
X.append(k1)
Y.append(ng)
for i in range(N - 1, -1, -1):
print(Y[i], X[i]) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN ASSIGN VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER STRING ASSIGN VAR LIST NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for tt in range(0, t):
n = int(input())
p = list(map(int, input().split()))
d = [0] * n
for i in range(1, n):
if p[i] <= p[i - 1]:
d[0] -= p[i - 1] - p[i]
d[i] += p[i - 1] - p[i]
for i in range(1, n):
d[i] += d[i - 1]
st = 1
for i in range(0, n):
p[i] += d[i]
if p[i] < 0:
st = 0
if i >= 1 and p[i - 1] > p[i]:
st = 0
if st == 1:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | from itertools import accumulate
def read_ints():
return map(int, input().split())
t_n = int(input())
for i_t in range(t_n):
n = int(input())
a = list(read_ints())
min_from_left = list(accumulate(a, min))
min_from_right = list(accumulate(a[::-1], min))[::-1]
deltas = [(x2 - x1) for x1, x2 in zip(a, a[1:])]
r = [0] * n
acc = 0
for i, delta in list(enumerate(deltas)):
if delta > 0:
acc += delta
r[i + 1] += acc
acc = 0
for i, delta in list(enumerate(deltas))[::-1]:
if delta < 0:
acc += abs(delta)
r[i] += acc
if all(x >= rx for rx, x in zip(r, a)):
print("YES")
else:
print("NO") | FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
r = a[0]
l = 0
for i in range(n):
if r + l < a[i]:
l = a[i] - r
elif r + l > a[i]:
r = a[i] - l
if r >= 0:
print("YES")
else:
print("NO") | IMPORT 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
while t:
n = int(input())
v = list(map(int, input().split()))
a = [(0) for i in range(n)]
b = [(0) for i in range(n)]
a[0] = v[0]
flag = True
for i in range(1, n):
a[i] = min(a[i - 1], v[i] - b[i - 1])
b[i] = v[i] - a[i]
if b[i] < b[i - 1] or a[i] < 0:
flag = False
break
if flag:
print("YES")
else:
print("NO")
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
s = p = a[0]
for i in range(1, n):
if a[i] < p:
s -= p - a[i]
p = a[i]
if s < 0:
print("NO")
else:
print("YES") | 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 VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = [0] * (n - 1)
for i in range(n - 1):
b[i] = a[i + 1] - a[i]
mi = 10000000
c = [0] * (n + 1)
for i in range(n - 1):
if b[i] < 0:
c[i + 1] += b[i]
c[0] -= b[i]
else:
c[i + 1] += b[i]
c[-1] -= b[i]
for i in range(n):
c[i + 1] += c[i]
for i in range(n):
if c[i] > a[i]:
print("NO")
break
else:
print("YES") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
a += [a[-1], a[0]]
min_a = a[0]
min_i = 0
for i in range(n):
if a[i] < min_a:
min_i = i
min_a = a[i]
need = 0
for i in range(n):
if i == min_i:
break
if a[i] > a[i - 1]:
need += a[i] - a[i - 1]
for i in range(n - 1, -1, -1):
if i == min_i:
break
if a[i] > a[i + 1]:
need += a[i] - a[i + 1]
print("YES" if need <= min_a else "NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
ans = "YES"
left_min = [(0) for _ in range(n)]
left_min[0] = a[0]
for i in range(1, n):
left_min[i] = min(left_min[i - 1], a[i])
for i in range(0, n - 1):
if i > 0:
left_min[i] = min(left_min[i - 1], left_min[i])
if a[i] > a[i + 1]:
if a[i] - a[i + 1] > left_min[i]:
ans = "NO"
break
else:
left_min[i] = left_min[i] - (a[i] - a[i + 1])
print(ans) | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
minl = arr[0]
maxl = 0
for i in range(1, n):
curr = arr[i]
arr[i] = min(arr[i - 1], arr[i] - maxl)
maxl = max(maxl, curr - arr[i])
if arr[n - 1] < 0:
print("NO")
else:
print("YES") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
d = [(a[i] if i == 0 else a[i] - a[i - 1]) for i in range(n)]
if abs(sum(x for x in d if x < 0)) <= d[0]:
print("YES")
else:
print("NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | def solve():
n = int(input())
a = list(map(int, input().split()))
a = [0] + a
print(
"YES"
if sum([max(0, a[i - 1] - a[i]) for i in range(1, n + 1)]) <= a[1]
else "NO"
)
def main():
t = 1
t = int(input())
for _ in range(t):
solve()
main() | 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 BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER STRING STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(0, int(input())):
n = int(input())
arr = [int(x) for x in input().split()]
lst = arr[0]
bad = False
arr[0] = 0
for i in range(1, n):
dec = min(lst, arr[i] - arr[i - 1])
lst = dec
arr[i] -= dec
if dec < 0:
bad = True
print("NO" if bad else "YES") | FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | a = int(input())
for i in range(a):
s = int(input())
z = list(map(int, input().split()))
dec = []
inc = []
dec.append(z[0])
inc.append(0)
flag = 0
for i in range(1, len(z)):
if z[i] == z[i - 1]:
dec.append(dec[-1])
inc.append(inc[-1])
if z[i] > z[i - 1]:
dec.append(dec[-1])
inc.append(z[i] - dec[-1])
if inc[-1] < inc[-2] or inc[-1] < 0:
flag = 1
break
else:
inc.append(inc[-1])
dec.append(z[i] - inc[-1])
if dec[-1] > dec[-2] or dec[-1] < 0:
flag = 1
break
if flag == 0:
print("YES")
else:
print("NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | T = int(input())
for _ in range(T):
n = int(input())
a = list(map(int, input().split()))
cur, maxn, flag, res = 0, 0, 0, 1
for i in range(1, n):
if flag == 0 and a[i] > a[i - 1]:
maxn = a[i - 1]
cur = a[i] - maxn
flag = 1
elif flag == 1 and a[i] >= cur:
maxn = min(maxn, a[i] - cur)
cur = a[i] - maxn
elif flag == 1 and a[i] < cur:
res = 0
break
if res:
print("YES")
else:
print("NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
def inp():
return int(input())
def inlt():
return list(map(int, input().split()))
def insr():
s = input()
return list(s[: len(s) - 1])
def invr():
return map(int, input().split())
n = inp()
b = [0] * 100000
c = [0] * 100000
for j in range(n):
t = inp()
a = list(invr())
b[0] = a[0]
c[0] = 0
chk = True
for i in range(1, t):
b[i] = min(a[i] - c[i - 1], b[i - 1])
c[i] = max(a[i] - b[i - 1], c[i - 1])
if b[i] + c[i] != a[i] or b[i] < 0 or c[i] < 0:
chk = False
print("NO")
break
if chk:
print("YES") | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
a = [0] * n
a[0] = arr[0]
b = [0] * n
fl = True
for i in range(1, n):
aMust = min(a[i - 1], arr[i] - b[i - 1])
bMust = arr[i] - aMust
a[i] = aMust
b[i] = bMust
if aMust < 0 or bMust > arr[i] or a[i] > a[i - 1] or b[i] < b[i - 1]:
fl = False
if fl:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
curr = 0
for i in range(n - 1):
diff = l[i + 1] - l[i]
l[i] -= curr
if diff > 0:
curr += diff
l[n - 1] -= curr
if min(l) >= 0:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
if n == 1 or n == 2:
print("Yes")
else:
arr = [10**18] + arr + [10**18]
imos = [0] * (n + 2)
for i in range(1, n + 1):
if arr[i] > arr[i - 1]:
d = arr[i] - arr[i - 1]
imos[i] += d
imos[n + 1] -= d
for i in range(1, n + 1):
imos[i] += imos[i - 1]
for i in range(1, n + 1):
arr[i] -= imos[i]
if arr[i] < 0:
print("No")
break
else:
print("Yes") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST BIN_OP NUMBER NUMBER VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | def read_generator():
while True:
tokens = input().split(" ")
for t in tokens:
yield t
reader = read_generator()
def readword():
return next(reader)
def readint():
return int(next(reader))
def readfloat():
return float(next(reader))
def readline():
return input()
def solve(a, n):
l = [a[0]]
for i in range(1, n):
l.append(min(l[i - 1], a[i - 1]))
r = [a[-1]]
j = 0
for i in range(n - 2, -1, -1):
r.append(min(r[j], a[i + 1]))
j += 1
r = list(reversed(r))
for i in range(n):
if l[i] + r[i] < a[i]:
return "NO"
b = a[:]
m = b[0]
d = 0
for i in range(1, n):
b[i] -= d
if b[i] > m:
d += b[i] - m
b[i] = m
else:
m = b[i]
if b[-1] >= 0:
return "YES"
b = list(reversed(a[:]))
m = b[0]
d = 0
for i in range(1, n):
b[i] -= d
if b[i] > m:
d += b[i] - m
b[i] = m
else:
m = b[i]
return "YES" if b[-1] >= 0 else "NO"
tests = readint()
for t in range(tests):
n = readint()
a = [readint() for _ in range(n)]
b = [a[0]]
for i in range(1, n):
if a[i] != b[len(b) - 1]:
b.append(a[i])
print(solve(b, len(b))) | FUNC_DEF WHILE NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR EXPR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR RETURN STRING ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER NUMBER STRING STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
flag = True
ar = list(map(int, input().split()))
su = 0
for i in range(n - 1):
if ar[i] >= ar[i + 1]:
su += ar[i] - ar[i + 1]
if su <= ar[0]:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
readline = sys.stdin.readline
T = int(readline())
INF = 10**9 + 7
Ans = [None] * T
for qu in range(T):
N = int(readline())
A = list(map(int, readline().split()))
res = INF
rem = 0
for i in range(N):
res = max(0, min(res, A[i] - rem))
A[i] -= res
rem = A[i]
Ans[qu] = "YES" if all(a2 - a1 >= 0 for a1, a2 in zip(A, A[1:])) else "NO"
print("\n".join(map(str, Ans))) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL 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 VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print("YES")
continue
d = [a[0]] + [(a[i] - a[i - 1]) for i in range(1, n)] + [-a[n - 1]]
need_plus = 0
for i in range(1, len(d) - 1):
if d[i] > 0:
need_plus += d[i]
if need_plus <= a[n - 1]:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
for i in range(n - 1, 0, -1):
a[i] -= a[i - 1]
minus = 0
for i in range(1, n):
if a[i] < 0:
minus -= a[i]
if a[0] - minus >= 0:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
x = 10**9
y = 0
for a in map(int, input().split()):
if a < x + y:
x = a - y
if x < 0:
print("NO")
break
else:
y = a - x
else:
print("YES") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | def process(A):
a0 = A[0]
b0 = 0
n = len(A)
for i in range(n):
X = A[i]
if 0 <= X - b0 <= a0:
b1 = b0
a1 = X - b0
elif X - a0 >= b0:
a1 = a0
b1 = X - a0
else:
return "NO"
a0, b0 = a1, b1
return "YES"
t = int(input())
for I in range(t):
n = int(input())
A = [int(x) for x in input().split()]
print(process(A)) | FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN STRING ASSIGN VAR VAR VAR VAR RETURN STRING 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 VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | N = int(input())
for _ in range(N):
n = int(input())
l = list(map(int, input().split()))
des_num = 0
for i in range(len(l) - 1):
if l[i + 1] - des_num < 0:
print("NO")
break
if l[i] < l[i + 1]:
des_num += l[i + 1] - l[i]
else:
print("YES") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for s in [*open(0)][2::2]:
x, *l = map(int, s.split())
a = y = 0
for i in l:
x = min(x, i - y)
y = max(y, i - x)
a |= x < 0
print("YNEOS"[a::2]) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.buffer.readline
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
rs = lambda: input().decode().rstrip("\n\r")
wrt = sys.stdout.write
pr = lambda *args, end="\n": wrt(" ".join([str(x) for x in args]) + end)
xrange = lambda args: reversed(range(args))
cdiv = lambda x, y: -(-x // y)
enum = enumerate
inf = float("inf")
mod = 10**9 + 7
Cases = ri()
for Case in range(Cases):
n = ri()
a = rl()
update = 0
for i in range(n - 1):
if update > a[i] or update > a[i + 1]:
print("NO")
break
update += max(0, a[i + 1] - a[i])
else:
print("YES") | 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 FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | I = lambda: map(int, input().split(" "))
for _ in range(int(input())):
n = int(input())
arr = [*I()]
s = 0
for i in range(n - 1):
s = s + max(0, arr[i] - arr[i + 1])
if s <= arr[0]:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**9
def solve():
l = inf
r = -1
for a in aa:
l = min(a, l, a - r)
r = a - l
if l < 0 or r < 0:
return "NO"
return "YES"
for _ in range(II()):
n = II()
aa = LI()
print(solve()) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
i = 1
l = 0
g = a[0]
while i < n and a[i] <= a[i - 1]:
g = min(g, a[i])
i += 1
l = i - 1
i = n - 2
h = a[-1]
while i >= 0 and a[i + 1] >= a[i]:
h = min(h, a[i])
i -= 1
r = i + 1
if r <= l:
print("YES")
else:
p = 0
q = 0
for i in range(l + 1, r + 1):
if a[i] <= a[i - 1]:
p += abs(a[i - 1] - a[i])
for i in range(r - 1, l - 1, -1):
if a[i] <= a[i + 1]:
q += abs(a[i + 1] - a[i])
if p <= g or q <= h:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
diff = [0] * n
diff[0] = arr[0]
sum = 0
for i in range(1, n):
diff[i] = arr[i] - arr[i - 1]
if diff[i] < 0:
sum -= diff[i]
if sum > diff[0]:
print("NO")
else:
print("YES") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
def main():
n = int(input())
alst = list(map(int, input().split()))
up = alst[0]
down = 0
for a in alst[1:]:
if up + down == a:
continue
elif up + down < a:
down = a - up
else:
up = a - down
if up < 0 or down < 0:
print("NO")
return
print("YES")
for _ in range(int(input())):
main() | IMPORT ASSIGN VAR VAR 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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
x = 0
flag = 1
for i in range(1, n):
x = max(x, a[i] - a[i - 1])
a[i] -= x
if a[i] < 0:
flag = 0
print("NO")
break
if flag:
print("YES") | IMPORT ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL 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 BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
a = int(input())
for i in range(a):
b = int(input())
c = list(map(int, input().split()))
d = [c[0]]
e = [0]
for j in range(1, b):
d.append(min(d[j - 1], c[j] - e[j - 1]))
e.append(c[j] - d[j])
def abc(d, e):
for j in range(1, b):
if d[j] > d[j - 1]:
return "NO"
if e[-j - 1] > e[-j]:
return "NO"
if d[j] < 0:
return "NO"
if e[-j - 1] < 0:
return "NO"
return "YES"
print(abc(d, e)) | IMPORT ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER VAR VAR RETURN STRING IF VAR VAR NUMBER RETURN STRING IF VAR BIN_OP VAR NUMBER NUMBER RETURN STRING RETURN STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
left, flag = a[0], False
for i in range(n - 1):
if a[i] > a[i + 1]:
left -= a[i] - a[i + 1]
if left < 0:
flag = True
break
left = min(left, a[i])
if flag:
print("NO")
else:
print("YES") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
def possible(a):
if len(a) == 0:
return "YES"
if a[0] < 0:
return "NO"
if len(a) == 1:
return "YES"
if a[0] > a[1]:
return possible(a[1:])
if a[-1] > a[-2]:
return possible(a[:-1])
return possible([(x - a[1] + a[0]) for x in a[1:]])
tests = int(input())
for test in range(tests):
n = int(input())
a = list(map(int, input().split()))
if n < 3:
print("YES")
else:
reduceAll = 0
for item in range(n - 1):
diff = a[item] - a[item + 1]
if a[item] - reduceAll < 0:
a[-1] = -1
break
elif diff < 0:
reduceAll -= diff
if a[-1] - reduceAll < 0:
print("NO")
else:
print("YES") | IMPORT ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR NUMBER NUMBER RETURN STRING IF FUNC_CALL VAR VAR NUMBER RETURN STRING IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.buffer.readline
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
rs = lambda: input().decode().rstrip("\n\r")
wrt = sys.stdout.write
pr = lambda *args, end="\n": wrt(" ".join([str(x) for x in args]) + end)
xrange = lambda args: reversed(range(args))
cdiv = lambda x, y: -(-x // y)
enum = enumerate
inf = float("inf")
mod = 10**9 + 7
class LazySegmentTree:
def __init__(self, data, default=0, func=max):
self._default = default
self._func = func
self._len = len(data)
self._size = _size = 1 << (self._len - 1).bit_length()
self._lazy = [0] * (2 * _size)
self.data = [default] * (2 * _size)
self.data[_size : _size + self._len] = data
for i in reversed(range(_size)):
self.data[i] = func(self.data[i + i], self.data[i + i + 1])
def __len__(self):
return self._len
def _push(self, idx):
q, self._lazy[idx] = self._lazy[idx], 0
self._lazy[2 * idx] += q
self._lazy[2 * idx + 1] += q
self.data[2 * idx] += q
self.data[2 * idx + 1] += q
def _update(self, idx):
for i in reversed(range(1, idx.bit_length())):
self._push(idx >> i)
def _build(self, idx):
idx >>= 1
while idx:
self.data[idx] = (
self._func(self.data[2 * idx], self.data[2 * idx + 1]) + self._lazy[idx]
)
idx >>= 1
def add(self, start, stop, value):
start = start_copy = start + self._size
stop = stop_copy = stop + self._size
while start < stop:
if start & 1:
self._lazy[start] += value
self.data[start] += value
start += 1
if stop & 1:
stop -= 1
self._lazy[stop] += value
self.data[stop] += value
start >>= 1
stop >>= 1
self._build(start_copy)
self._build(stop_copy - 1)
def query(self, start, stop, default=0):
start += self._size
stop += self._size
self._update(start)
self._update(stop - 1)
res = default
while start < stop:
if start & 1:
res = self._func(res, self.data[start])
start += 1
if stop & 1:
stop -= 1
res = self._func(res, self.data[stop])
start >>= 1
stop >>= 1
return res
def __repr__(self):
return "LazySegmentTree({0})".format(self.data)
Cases = ri()
for Case in range(Cases):
n = ri()
a = rl()
a = LazySegmentTree(a, func=lambda x, y: x + y)
for i in range(n - 1):
now = a.query(i, i + 1)
nex = a.query(i + 1, i + 2)
if now < 0 or nex < 0:
print("NO")
break
diff = abs(now - nex)
if now >= nex:
a.add(0, i + 1, -diff)
else:
a.add(i + 1, n, -diff)
else:
print("YES") | 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 FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF VAR NUMBER WHILE VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | inf = 10**9
def solve():
l = inf
r = -1
for a in aa:
l = min(a, l, a - r)
r = a - l
if l < 0 or r < 0:
return "NO"
return "YES"
for _ in range(int(input())):
n = int(input())
aa = list(map(int, input().split()))
print(solve()) | ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | mod = 10**9 + 7
INF = float("inf")
def getlist():
return list(map(int, input().split()))
def main():
T = int(input())
for _ in range(T):
N = int(input())
A = getlist()
B = [0] * N
leftminus = [None] * N
B[0] = 0
leftminus[0] = A[0]
jud = 1
for i in range(1, N):
val = min(leftminus[i - 1], A[i] - B[i - 1])
if val < 0:
jud = 0
break
B[i] = A[i] - val
leftminus[i] = val
for i in range(N - 2, -1, -1):
if B[i] > B[i + 1]:
jud = 0
if jud == 0:
print("NO")
else:
print("YES")
main() | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if a[0] - sum(-min(a[i] - a[i - 1], 0) for i in range(1, n)) >= 0:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
(N,) = map(int, input().split())
X = list(map(int, input().split()))
f = 0
ks = -1
si = -1
i = 0
for b, x in zip(X[:], X[1:]):
if not f and b >= x:
X[i] = 0
i += 1
continue
if not f and b < x:
f = 1
X[i] = 0
ks = b
X[i + 1] -= ks
i += 1
continue
if f:
if X[i] and X[i + 1] >= X[i]:
ks = min(ks, X[i + 1] - X[i])
X[i + 1] -= min(ks, X[i + 1] - X[i])
else:
X[i + 1] -= ks
i += 1
continue
f = 0
for i in range(N - 1):
if X[i] > X[i + 1]:
print("NO")
break
else:
print("YES") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | tests = int(input())
for test in range(tests):
n = int(input())
a = list(map(int, input().split()))
l = [0] * n
r = [0] * n
for i in range(n):
if i > 0 and a[i] > a[i - 1]:
l[i] = a[i] - a[i - 1]
if i < n - 1 and a[i] > a[i + 1]:
r[i] = a[i] - a[i + 1]
rr = 0
for i in range(n):
rr += l[i]
a[i] -= rr
ll = 0
for i in range(n - 1, -1, -1):
ll += r[i]
a[i] -= ll
if a[0] >= 0 and sum(a) == a[0] * n:
print("YES")
else:
print("NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
aa = 0
bb = a[0]
for i in range(1, n):
if a[i] <= a[i - 1]:
bb -= a[i - 1] - a[i]
else:
aa += a[i] - a[i - 1]
ok = True
ok &= bb >= 0
ok &= aa <= a[-1]
print("YES" if ok else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
def input():
return sys.stdin.readline().strip()
def list2d(a, b, c):
return [[c for j in range(b)] for i in range(a)]
def list3d(a, b, c, d):
return [[[d for k in range(c)] for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e):
return [
[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)
]
def ceil(x, y=1):
return int(-(-x // y))
def INT():
return int(input())
def MAP():
return map(int, input().split())
def LIST(N=None):
return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes():
print("Yes")
def No():
print("No")
def YES():
print("YES")
def NO():
print("NO")
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10
for _ in range(INT()):
N = INT()
A = LIST()
cur = A[0]
A[0] = 0
for i in range(1, N):
use = max(min(cur, A[i] - A[i - 1]), 0)
A[i] -= use
cur = use
ok = True
for i in range(1, N):
if A[i - 1] > A[i]:
ok = False
break
if ok:
YES()
else:
NO() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF NONE RETURN VAR NONE FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for _ in range(int(input())):
n = int(input())
a = [*map(int, input().split())]
ans = sum(max(a[i] - a[i + 1], 0) for i in range(n - 1))
print("YES" if ans <= a[0] else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER STRING STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | total = int(input())
for _ in range(total):
size = int(input())
arr = [int(c) for c in input().split()]
preOperationCount = arr[0]
rightOperationCount = 0
for val in arr:
if val < rightOperationCount:
print("NO")
break
else:
rightOperationCount = max(val - preOperationCount, rightOperationCount)
preOperationCount = min(preOperationCount, val - rightOperationCount)
else:
print("YES") | 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 VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split(" ")))
d = [0] * n
d[0] = a[0]
can = True
for i in range(1, n):
d[i] = a[i] - a[i - 1]
if d[i] < 0:
d[0] += d[i]
d[i] = 0
if d[0] < 0:
can = False
break
elif d[i] > 0:
d[i] -= d[i]
print("YES") if can else print("NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR EXPR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
b = a.copy()
x = 0
for i in range(n - 2, -1, -1):
if a[i] > b[i + 1]:
x += a[i] - b[i + 1]
a[i] -= x
if a[0] >= 0:
print("YES")
else:
print("NO") | ASSIGN VAR 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | from sys import stdin
t = int(stdin.readline())
for case in range(t):
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
left = [a[0]]
right = [a[-1]]
for x in range(1, n):
left.append(min(left[-1], a[x]))
right.append(min(right[-1], a[-x - 1]))
right = right[::-1]
good = "YES"
cLeft = a[0]
rightVein = 0
for x in range(n):
if cLeft + rightVein >= a[x]:
cLeft = min(cLeft, a[x] - rightVein)
elif right[x] + cLeft >= a[x]:
rightVein = max(rightVein, a[x] - cLeft)
cLeft = min(cLeft, a[x] - rightVein)
else:
good = "NO"
break
print(good) | 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 VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | rep = int(input())
for i in range(rep):
n = int(input())
a = input()
answer = "YES"
if n > 2:
a = a.split(" ")
f = int(a[0])
b = 0
c = int(a[1])
for i in range(2, n):
if int(a[i]) >= c - f + b and int(a[i]) >= b:
if b < c - f + b:
b = c - f + b
f = int(a[i - 1])
c = int(a[i])
else:
answer = "NO"
break
print(answer) | 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 VAR ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
def solve(n, arr):
cur = arr[0]
transformed = [0]
for i in range(1, n):
transformed.append(max(transformed[i - 1], arr[i] - cur))
cur = min(cur, arr[i] - transformed[i])
cur = arr[-1]
for i in range(n - 1, -1, -1):
if arr[i] < transformed[i]:
return False
return True
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
can = solve(n, arr)
if can:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL 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 VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
def input():
return sys.stdin.readline().rstrip()
def slv():
n = int(input())
a = list(map(int, input().split()))
l, r = 0, 1 << 128
def greedy_update(l, r, S):
y = min(S - l, r)
x = S - y
return x, y
for v in a:
new_l, new_r = greedy_update(l, r, v)
if new_r < 0:
print("NO")
return
else:
l, r = new_l, new_r
continue
print("YES")
return
def main():
t = int(input())
for i in range(t):
slv()
return
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR 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 VAR NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
A = map(int, input().split())
l, r = 10000000000, 0
possible = True
for a in A:
if a < r:
possible = False
break
a -= r
l = min(l, a)
r += a - l
if possible:
print("YES")
else:
print("NO") | IMPORT ASSIGN VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | for i in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
if n <= 2:
print("YES")
else:
for j in range(1, n):
if a[j] > a[j - 1]:
break
rrr = 0
x = a[j - 1]
for jj in range(j, n - 1):
if a[jj + 1] < a[jj]:
rrr = rrr + a[jj] - a[jj + 1]
if x >= rrr:
print("YES")
else:
print("NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | n = int(input())
answer = []
for i in range(n):
k = int(input())
list = input().split()
for j in range(k):
list[j] = int(list[j])
result = True
leftIndex = 0
leftPoints = list[0]
rightPoints = 0
rightMaxPoints = 0
for j in range(1, k - 1):
if list[j] < rightPoints:
result = False
break
if list[j] < leftPoints + rightPoints:
if list[j] < rightPoints:
result = False
break
else:
leftPoints = list[j] - rightPoints
else:
rightPoints = list[j] - leftPoints
if list[k - 1] < rightPoints:
result = False
answer.append(result)
for i in range(n):
if answer[i]:
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
You are given an array $a$ of $n$ positive integers.
You can use the following operation as many times as you like: select any integer $1 \le k \le n$ and do one of two things: decrement by one $k$ of the first elements of the array. decrement by one $k$ of the last elements of the array.
For example, if $n=5$ and $a=[3,2,2,1,4]$, then you can apply one of the following operations to it (not all possible options are listed below): decrement from the first two elements of the array. After this operation $a=[2, 1, 2, 1, 4]$; decrement from the last three elements of the array. After this operation $a=[3, 2, 1, 0, 3]$; decrement from the first five elements of the array. After this operation $a=[2, 1, 1, 0, 3]$;
Determine if it is possible to make all the elements of the array equal to zero by applying a certain number of operations.
-----Input-----
The first line contains one positive integer $t$ ($1 \le t \le 30000$) — the number of test cases. Then $t$ test cases follow.
Each test case begins with a line containing one integer $n$ ($1 \le n \le 30000$) — the number of elements in the array.
The second line of each test case contains $n$ integers $a_1 \ldots a_n$ ($1 \le a_i \le 10^6$).
The sum of $n$ over all test cases does not exceed $30000$.
-----Output-----
For each test case, output on a separate line: YES, if it is possible to make all elements of the array equal to zero by applying a certain number of operations. NO, otherwise.
The letters in the words YES and NO can be outputed in any case.
-----Example-----
Input
4
3
1 2 1
5
11 7 9 6 8
5
1 3 1 3 1
4
5 2 1 10
Output
YES
YES
NO
YES | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
l = 0
r = n - 1
maxi = 10**8
for i in range(n):
if a[l] <= maxi:
maxi = a[l]
l += 1
else:
break
lmax = maxi
maxi = 10**8
for i in range(n):
if a[r] <= maxi:
maxi = a[r]
r -= 1
else:
break
rmax = maxi
a = a[l : r + 1]
if len(a) == 0:
print("YES")
continue
dec = min(a[0], lmax)
a[0] -= dec
lmax = dec
for i in range(len(a) - 1):
dec = min(a[i + 1] - a[i], a[i + 1], lmax)
a[i + 1] -= dec
lmax = dec
flag = 1
if max(a) <= rmax:
if sorted(a) == a:
pass
else:
flag = 0
else:
flag = 0
if flag == 1:
print("YES")
else:
print("NO") | 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
-----Output-----
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
-----Examples-----
Input
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
0.279
-----Note-----
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. | n, t = int(input()), [int(i[-3:]) for i in input().split()]
k, s = t.count(0), sum(t)
c = s // 1000 + int(s % 1000 > 500)
a, b = max(0, n - k), min(2 * n - k, n)
if a <= c <= b:
s = abs(c * 1000 - s)
else:
s = min(abs(a * 1000 - s), abs(b * 1000 - s))
print(str(s // 1000) + "." + str(s % 1000).zfill(3)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING FUNC_CALL FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
-----Output-----
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
-----Examples-----
Input
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
0.279
-----Note-----
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. | N = int(input())
n = N
A = list(map(float, input().strip().split(" ")))
z = 0
for i in range(len(A)):
A[i] = round(A[i], 3) - int(A[i])
if A[i] == 0:
z += 1
ANS = sum(A)
ans = 10**10
for j in range(n - z, n + 1):
ans = min(ans, abs(ANS - j))
print("%.3f" % ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR |
Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
-----Output-----
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
-----Examples-----
Input
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
0.279
-----Note-----
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. | from sys import *
s1 = stdin.readline().strip()
n = int(s1)
s1 = stdin.readline().strip()
a = list(map(float, s1.split()))
b = []
for i in range(2 * n):
if int(a[i]) != a[i]:
b.append(round(1000 * (a[i] - int(a[i]))))
m = len(b)
r = 0
for i in range(m):
r = r + b[i]
if m <= n:
if r >= 1000 * m:
r = r - 1000 * m
else:
r = min(r - 1000 * (r // 1000), 1000 - r + 1000 * (r // 1000))
elif r >= n * 1000:
r = r - 1000 * n
elif r <= 1000 * (m - n):
r = 1000 * (m - n) - r
else:
r = min(r - 1000 * (r // 1000), 1000 - r + 1000 * (r // 1000))
r = r / 1000
print("%.3f" % r) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR IF VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR |
Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
-----Output-----
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
-----Examples-----
Input
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
0.279
-----Note-----
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. | n = int(input())
arr = list(map(float, input().split()))
arr = sorted([(x - int(x)) for x in arr if x - int(x) != 0])
o = 2 * n - len(arr)
arr_sum = sum(arr)
res = int(2000000000.0)
for i in range(n + 1):
if i + o >= n:
res = min(res, abs(i - arr_sum))
print("%.3f" % res) | 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 BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR |
Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
-----Output-----
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
-----Examples-----
Input
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
0.279
-----Note-----
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. | k = 0
ans = 0
n = int(input())
a = input().split()
for i in range(2 * n):
s = float(a[i])
if s != int(s):
k += 1
ans += int(s) + 1 - s
if ans - int(ans) > 0.5:
p = int(ans) + 1
else:
p = int(ans)
if p > n:
p = n
if p + n >= k:
print("%.3f" % abs(ans - p))
else:
print("%.3f" % abs(ans - k + n)) | ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
Jeff got 2n real numbers a_1, a_2, ..., a_2n as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes n operations, each of them goes as follows: choose indexes i and j (i ≠ j) that haven't been chosen yet; round element a_{i} to the nearest integer that isn't more than a_{i} (assign to a_{i}: ⌊ a_{i} ⌋); round element a_{j} to the nearest integer that isn't less than a_{j} (assign to a_{j}: ⌈ a_{j} ⌉).
Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference.
-----Input-----
The first line contains integer n (1 ≤ n ≤ 2000). The next line contains 2n real numbers a_1, a_2, ..., a_2n (0 ≤ a_{i} ≤ 10000), given with exactly three digits after the decimal point. The numbers are separated by spaces.
-----Output-----
In a single line print a single real number — the required difference with exactly three digits after the decimal point.
-----Examples-----
Input
3
0.000 0.500 0.750 1.000 2.000 3.000
Output
0.250
Input
3
4469.000 6526.000 4864.000 9356.383 7490.000 995.896
Output
0.279
-----Note-----
In the first test case you need to perform the operations as follows: (i = 1, j = 4), (i = 2, j = 3), (i = 5, j = 6). In this case, the difference will equal |(0 + 0.5 + 0.75 + 1 + 2 + 3) - (0 + 0 + 1 + 1 + 2 + 3)| = 0.25. | n = int(input())
As = list(map(float, input().split()))
B = list(x - int(x) for x in As if x - int(x) > 0.0)
l = len(B)
if l == 0:
print("{:.3f}".format(0))
exit(0)
S = sum(x for x in B)
ll = l if l % 2 == 0 else l + 1
ans = 10000000000.0
for i in range(max(0, l - n), (n if l > n else l) + 1):
ans = min(ans, abs(i - S))
print("{:.3f}".format(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 BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input().strip("\n")
cur = s[0]
l = 1
for i in range(1, n):
if s[i] != cur:
l += 1
cur = s[i]
print(min(l + 2, n)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
t = [int(i) for i in input()]
ch = 0
sm = 0
for i in range(n - 1):
if t[i] == t[i + 1]:
sm += 1
else:
ch += 1
print(ch + min(sm, 2) + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
def solve(n, s):
if n <= 3:
return n
cum = 0
p = "2"
count = 0
for i, v in enumerate(s):
if v != p:
cum += 1
else:
count += 1
p = v
if count == 0:
return cum
elif count == 1:
return cum + 1
else:
return cum + 2
print(solve(n, s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
repeat = 0
count = 0
for i in range(len(s) - 1):
if s[i] == s[i + 1]:
repeat += 1
else:
count += 1
count += 1
if repeat == 1:
count += 1
if repeat >= 2:
count += 2
print(count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | import sys
def solve(n, x):
ant = x[0]
cont = 1
maximo = 1
maxi = 1
dos = 0
for i in range(1, n):
if ant != x[i]:
cont += 1
ant = x[i]
if maximo >= 2:
dos += 1
maximo = 1
else:
maximo += 1
maxi = max(maxi, maximo)
if maximo >= 2:
dos += 1
if maxi >= 3 or dos >= 2:
return cont + 2
if dos == 1:
return cont + 1
return cont
n = int(input())
x = input()
print(solve(n, x)) | IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER RETURN BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
k = 0
i = 0
r = int(s[0])
while i < n:
if int(s[i]) == r:
r = 1 - r
k += 1
i += 1
print(min(k + 2, n)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
res = 1
for i in range(1, n):
if s[i] != s[i - 1]:
res += 1
print(min(res + 2, n)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = map(int, input())
s = input()
l = []
cnt = 1
for i in range(1, len(s)):
if s[i] != s[i - 1]:
l.append(cnt)
cnt = 1
else:
cnt += 1
l.append(cnt)
result = len(l)
max_pair = 0
max_count = 0
for i in range(len(l)):
if l[i] == 2:
max_pair += 1
max_count = max(max_count, l[i])
if max_pair >= 2 or max_count >= 3:
add = 2
elif max_pair == 1:
add = 1
else:
add = 0
print(result + add) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
c = 1
seg = []
for i in range(1, n):
if s[i] == s[i - 1]:
c += 1
else:
seg.append(c)
c = 1
seg.append(c)
f = 0
for i in range(len(seg)):
if seg[i] >= 3:
f = max(f, 2)
if seg[0] == 1 and seg[len(seg) - 1] == 2 or seg[0] == 2 and seg[len(seg) - 1] == 1:
f = max(1, f)
if seg[0] == seg[len(seg) - 1] == 2:
f = max(f, 2)
print(min(n, len(seg) + 2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | from sys import stdin
def get_count(c):
count = 0
flag = False
i = 1
while i < n + 2:
if arr[i] == arr[i - 1] and arr[i] == c:
if i == n + 1 or i == n:
flag = True
count += 1
i += 2
continue
i += 1
return count, flag
n = int(stdin.readline().strip())
arr = list(map(int, list(stdin.readline().strip())))
aux = [0] * n
aux[0] = 1
for i in range(1, n):
aux[i] = aux[i - 1]
if arr[i] != arr[i - 1]:
aux[i] += 1
count = 0
for i in range(0, n - 1):
if arr[i] == arr[i + 1]:
count += 1
count = min(count, 2)
print(aux[-1] + count) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n, s, ans = int(input()), list(input()), 1
flag = 0
for i in range(1, n):
if s[i] == s[i - 1]:
s[i] = str(int(s[i]) ^ 1)
flag = 1
elif flag:
break
last = s[0]
for i in s[1:]:
if i != last:
last = i
ans += 1
print(ans) | ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | def max_score(s):
a = 0
p = None
for c in s:
if c != p:
a += 1
p = c
return a + min(len(s) - a, 2)
n = int(input())
s = input()
print(max_score(s)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
a = input()
p = [1] * 4
for i in range(1, n):
for j in range(2, -1, -1):
p[j + 1] = max(p[j + 1], p[j] + (a[i - 1] == a[i]))
p[j] += a[i - 1] != a[i]
print(max(p[0:3])) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | __author__ = "Utena"
n = input()
s = input()
t = []
def alt(a):
b = ""
b += a[0]
for i in a[1 : len(a)]:
if i != b[-1]:
b += i
return b
m = len(alt(s))
judge = 0
for i in range(len(s) - 1):
if s[i + 1] == s[i]:
judge = 1
if i < len(s) - 2:
for j in range(i + 1, len(s) - 1):
if s[j + 1] == s[j]:
print(m + 2)
exit(0)
if judge == 1:
print(m + 1)
else:
print(m) | ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR STRING VAR VAR NUMBER FOR VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
ch = str(input())
if n == 1:
print(1)
else:
B = [(0) for _ in range(n)]
B[0] = 1
c = ch[0]
for k in range(1, n):
if ch[k] == c:
B[k] = B[k - 1]
else:
B[k] = B[k - 1] + 1
c = ch[k]
T = [(0) for _ in range(n)]
F = [(0) for _ in range(n)]
T[1] = B[0] + 1
if ch[1] == ch[0]:
F[1] = 1
for i in range(1):
for j in range(i + 2, n):
c = 0
if ch[j] == ch[j - 1] and not F[j - 1]:
temp = max([T[j - 1], B[j - 1] + 1])
if temp == 1 + B[j - 1]:
c = 1
else:
temp = T[j - 1] + 1
if F[j - 1] and ch[j] != ch[j - 1]:
c = 1
T[j] = temp
F[j] = c
print(T[n - 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
k1, k2, k1l, k2l = 0, 0, "0", "1"
for i in s:
if k1l != i:
k1 += 1
k1l = i
if k2l != i:
k2 += 1
k2l = i
print(min(len(s), max(k1, k2) + 2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER STRING STRING FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
mem = s[0]
streak = 1
amount = 0
res = 0
score = 1
for l in range(1, n):
if s[l] == mem:
streak += 1
else:
mem = s[l]
streak = 1
score += 1
if streak > 2:
res = 2
elif streak == 2:
amount += 1
if amount > 1:
res = 2
if amount + max(res - 1, 0) == 1:
res = 1
print(score + res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = list(input())
lis = [0] * n
bb = []
p = s[0]
lis[0] = 1
kk = 0
for i in range(1, len(s)):
if s[i] != p:
lis[i] = 1
p = s[i]
for i in range(1, len(s)):
if s[i] == s[i - 1]:
kk += 1
else:
bb.append(kk)
kk = 0
bb.append(kk)
ans = c = 0
if max(bb) > 1:
print(sum(lis) + 2)
else:
print(sum(lis) + min(2, bb.count(1))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | import sys
n = int(sys.stdin.readline())
s = sys.stdin.readline()
s = list(s)
a = []
k = 1
for i in range(n):
if i == n - 1:
a.append(k)
break
if s[i] == s[i + 1]:
k += 1
else:
a.append(k)
k = 1
add_amount = 0
count = 0
for i in range(n - 1):
if s[i] == s[i + 1]:
count += 1
add_amount = min(2, count)
print(len(a) + add_amount) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input().strip())
s = input().strip()
segs = []
start = 0
i = 1
while i <= n:
if i == n or s[i] != s[start]:
segs.append(i - start)
start = i
i = start + 1
else:
i += 1
res = len(segs)
segs.sort()
if segs[-1] >= 3:
res += 2
elif len(segs) >= 2 and segs[-1] >= 2 and segs[-2] >= 2:
res += 2
elif segs[-1] >= 2:
res += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | import sys
__author__ = "MoonBall"
T = 1
def process():
input()
s = input()
if len(s) == 1:
print(1)
return
b = []
for x in s:
if not b or b[-1][0] != x:
b.append((x, 1))
else:
b[-1] = b[-1][0], b[-1][1] + 1
ans = len(b)
c2 = 0
c3 = 0
for x in b:
if x[1] >= 2:
c2 = c2 + 1
ans = max(ans, len(b) + 1)
if x[1] >= 3:
c3 = c3 + 1
if c2 >= 2:
ans = max(ans, len(b) + 2)
if c3 >= 1:
ans = max(ans, len(b) + 2)
print(ans)
for _ in range(T):
process() | IMPORT ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | N = int(input())
s = input()
count1 = []
x = s[0]
i = 1
count = 1
while i < N:
if s[i] == s[i - 1]:
count += 1
else:
count1.append(count)
count = 1
i += 1
count1.append(count)
if any(i > 2 for i in count1):
print(len(count1) + 2)
elif count1.count(2) > 1:
print(len(count1) + 2)
elif count1.count(2) == 1:
print(len(count1) + 1)
else:
print(len(count1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | from sys import stdin
length = int(stdin.readline().rstrip())
scores = stdin.readline().rstrip()
doubles_found = 0
score = 1
for i in range(length - 1):
if scores[i] != scores[i + 1]:
score += 1
elif doubles_found < 2:
score += 1
doubles_found += 1
print(score) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiad—'1' for a correctly identified cow and '0' otherwise.
However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.
Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substring—that is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
Input
The first line contains the number of questions on the olympiad n (1 ≤ n ≤ 100 000).
The following line contains a binary string of length n representing Kevin's results on the USAICO.
Output
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
Examples
Input
8
10000011
Output
5
Input
2
01
Output
2
Note
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.
In the second sample, Kevin can flip the entire string and still have the same score. | n = int(input())
s = input()
dl = 1
now = s[0]
k = 0
for i in range(1, n):
if now != s[i]:
now = s[i]
dl += 1
for i in range(n - 1):
if s[i] == s[i + 1] == "0" or s[i] == s[i + 1] == "1":
k += 1
if k >= 2:
print(dl + 2)
elif k == 1:
print(dl + 1)
else:
print(dl) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.