description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction) -> int:
n = satisfaction.__len__()
if n == 0:
return 0
satisfaction.sort()
if satisfaction[-1] <= 0:
return 0
else:
mannux = 0
while satisfaction:
tem = 0
for i in range(n):
tem += (i + 1) * satisfaction[i]
if mannux < tem:
mannux = tem
satisfaction.pop(0)
n -= 1
return mannux | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
n = len(satisfaction)
dp = [-1000000009] * (n + 1)
dp[0] = 0
for i in range(n):
for j in range(i, -1, -1):
dp[j + 1] = max(dp[j + 1], satisfaction[i] * (j + 1) + dp[j])
answer = -1000000009
for i in range(n + 1):
answer = max(dp[i], answer)
return answer | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
temp = [0] * len(satisfaction)
output = 0
satisfaction.sort()
for i, _ in enumerate(satisfaction):
for j, s in enumerate(satisfaction[i:]):
temp[i] += s * (j + 1)
for s in temp:
if s > output:
output = s
return output | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
n = len(satisfaction)
dp = [([-1000000000.0] * n) for i in range(n)]
for i in range(n):
for j in range(i + 1):
dp[i][j] = max(
(dp[i - 1][j - 1] if i - 1 >= 0 and j - 1 >= 0 else 0)
+ satisfaction[i] * (j + 1),
dp[i - 1][j],
)
ret = max(dp[-1])
return ret if ret > 0 else 0 | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
sorted_satis = sorted(satisfaction)
max_satis = sorted_satis[-1]
if max_satis <= 0:
return 0
def calculate_satisfaction(start_index):
s = 0
for i in range(len(sorted_satis) - start_index):
s += (i + 1) * sorted_satis[start_index + i]
return s
max_satisfaction = -1
for i in range(len(sorted_satis)):
max_satisfaction = max(max_satisfaction, calculate_satisfaction(i))
if sorted_satis[i] >= 0:
return max_satisfaction
return max_satisfaction | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
if satisfaction[0] >= 0:
return calc_satisfaction(satisfaction)
min_val = -sys.maxsize
min_index = None
for i, v in enumerate(satisfaction):
if 0 > v > min_val:
min_index = i
min_val = v
curr_sum = sum(satisfaction[min_index + 1 :])
while min_index >= 0:
print((min_index, satisfaction))
print((satisfaction[min_index], curr_sum))
if abs(satisfaction[min_index]) < curr_sum:
curr_sum = curr_sum + satisfaction[min_index]
else:
break
min_index -= 1
print((min_index, satisfaction))
return calc_satisfaction(satisfaction[min_index + 1 :])
def calc_satisfaction(dishes):
total = 0
for i, s in enumerate(dishes):
total += (i + 1) * s
return total | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
dishes = len(satisfaction)
best = 0
for i in range(dishes):
sum = 0
tmp = satisfaction[-i - 1 : dishes]
for j, val in enumerate(tmp):
sum += (j + 1) * val
best = max(best, sum)
return best | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction = sorted(satisfaction)
ans = 0
for i, s in enumerate(satisfaction):
index = i
curr_ans = 0
j = 1
while index <= len(satisfaction) - 1:
curr_ans += j * satisfaction[index]
j += 1
index += 1
ans = max(ans, curr_ans)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
output = 0
result = 0
for i in range(len(satisfaction)):
output += satisfaction[i] * i
for i in range(0, len(satisfaction)):
for j in range(i, len(satisfaction)):
result += satisfaction[j] * (j - i + 1)
if output < result:
output = result
result = 0
if output < 0:
output = 0
return output | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
currmax = 0
satsort = sorted(satisfaction)
leng1 = len(satsort)
for nn in range(leng1):
satpart = satsort[nn:]
leng2 = leng1 - nn
time = [ii for ii in range(1, leng2 + 1)]
coeff = []
for ii in range(leng2):
coeff.append(satpart[ii] * time[ii])
cumul = [coeff[0]]
for ii in range(1, leng2):
cumul.append(cumul[ii - 1] + coeff[ii])
for elem in cumul:
if currmax < elem:
currmax = elem
return currmax | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
res = total = 0
i = len(satisfaction) - 1
satisfaction.sort()
while i >= 0 and satisfaction[i] + total > 0:
total += satisfaction[i]
res += total
i -= 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
bestSat = float("-inf")
for i in range(len(satisfaction)):
n = len(satisfaction) - 1
numItems = i + 1
numAdded = 0
currSat = 0
while numAdded <= numItems:
currSat += satisfaction[n - numAdded] * (numItems - numAdded)
numAdded += 1
bestSat = max(bestSat, currSat)
return max(bestSat, 0) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
score = 0
satisfaction.sort()
print(satisfaction)
if satisfaction[0] >= 0:
for i in range(len(satisfaction)):
score += (i + 1) * satisfaction[i]
return score
elif satisfaction[len(satisfaction) - 1] <= 0:
return score
else:
for i in range(len(satisfaction)):
score += (i + 1) * satisfaction[i]
badDishes = []
for sat in satisfaction:
if sat < 0:
badDishes.append(sat)
for bad in badDishes:
satisfaction.remove(bad)
print(satisfaction)
temp_best = 0
for i in range(len(satisfaction)):
temp_best += (i + 1) * satisfaction[i]
print(temp_best)
if temp_best > score:
score = temp_best
return score | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
table = [([None] * len(satisfaction)) for i in range(len(satisfaction))]
for i in range(len(satisfaction)):
for j in range(i, len(satisfaction)):
table[i][j] = satisfaction[j] * (i + 1)
bestSat = float("-inf")
for i in range(len(satisfaction)):
row = i
col = len(satisfaction) - 1
currSat = 0
while row >= 0:
currSat += table[row][col]
row -= 1
col -= 1
bestSat = max(bestSat, currSat)
return max(bestSat, 0) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
n = len(satisfaction)
satisfaction.sort()
ans = 0
for i in range(n):
coef = 0
for k in range(1, n + 1):
if i + k - 1 < n:
coef += satisfaction[i + k - 1] * k
ans = max(ans, coef)
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, s: List[int]) -> int:
s.sort()
n = len(s)
dp = [0] * (n + 2)
for dish in range(n - 1, -1, -1):
maxT = n + 1
for time in range(1, maxT):
if dish == n - 1:
dp[time] = time * s[dish]
else:
include = time * s[dish] + dp[time + 1]
dp[time] = max(include, dp[time])
maxT -= 1
return max(0, dp[1]) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER VAR NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
total = 0
beneficial = deque()
negative = []
if satisfaction[0] > 0:
return calc_satisfaction(satisfaction)
for s in satisfaction:
if s >= 0:
beneficial.append(s)
else:
negative.append(s)
curr_satisfaction = calc_satisfaction(beneficial)
for s in reversed(negative):
beneficial.appendleft(s)
temp_satisfaction = calc_satisfaction(beneficial)
if temp_satisfaction > curr_satisfaction:
curr_satisfaction = temp_satisfaction
else:
beneficial.popleft()
break
return curr_satisfaction
def calc_satisfaction(dishes):
total = 0
for i, s in enumerate(dishes):
total += (i + 1) * s
return total | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
total = 0
beneficial = [s for s in satisfaction if s >= 0]
negative = [s for s in satisfaction if s < 0]
for s in reversed(negative):
if calc_satisfaction([s] + beneficial) > calc_satisfaction(beneficial):
beneficial.insert(0, s)
return calc_satisfaction(beneficial)
def calc_satisfaction(dishes):
total = 0
for i, s in enumerate(dishes):
total += (i + 1) * s
return total | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP LIST VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | def main():
n = int(input())
V = []
for i in range(n):
x, w = map(int, input().split())
V.append((x - w, x + w))
V.sort(key=lambda x: x[1])
ans = 1
now = V[0]
for i in range(1, n):
if V[i][0] >= now[1]:
now = V[i]
ans += 1
print(ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
rng = []
for _ in range(n):
x, w = map(int, input().split())
rng.append((x - w, x + w))
rng.sort(key=lambda x: (x[1], x[0]))
ans = 0
tmp = -(10**10)
for l, r in rng:
if tmp <= l:
ans += 1
tmp = r
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
d = []
for i in range(n):
xx, ww = [int(i) for i in input().split()]
d.append([xx - ww, xx + ww])
d.sort(key=lambda x: x[0])
last = -100000000000
ans = 0
for i in range(n):
if last <= d[i][0]:
last = d[i][1]
ans += 1
elif last > d[i][1]:
last = d[i][1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | import sys
MOD = 10**9 + 7
INF = 10**10
sys.setrecursionlimit(100000000)
dy = -1, 0, 1, 0
dx = 0, 1, 0, -1
def main():
n = int(input())
P = [tuple(map(int, input().split())) for _ in range(n)]
P.sort(key=lambda x: sum(x))
ans = 0
X, W = -INF, 0
for x, w in P:
if abs(x - X) >= W + w:
ans += 1
X = x
W = w
print(ans)
main() | IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
ls = [list(map(int, input().split())) for i in range(n)]
lsr = [[max(ls[i][0] - ls[i][1], 0), ls[i][0] + ls[i][1]] for i in range(n)]
lsr.sort(key=lambda x: x[1])
idx = 0
ans = 0
for l in lsr:
if idx <= l[0]:
idx = l[1]
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
xw = [list(map(int, input().split())) for _ in [0] * n]
ab = sorted([[x - w, x + w] for x, w in xw], key=lambda x: (x[1], x[0]))
k = ab[0][0]
cnt = 0
for a, b in ab:
if k <= a:
cnt += 1
k = b
print(cnt) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | N = int(input())
span = []
for _ in range(N):
x, w = list(map(int, input().split()))
span.append([x - w, x + w])
span = sorted(span, key=lambda x: x[1])
max_clique = 1
for i in range(1, N):
if span[i - 1][1] > span[i][0]:
span[i][1] = span[i - 1][1]
else:
max_clique += 1
print(max_clique) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | def on(l1, l2):
return abs(l1[0] - l2[0]) >= l1[1] + l2[1]
n = int(input())
inf = []
for i in range(n):
a, b = map(int, input().split())
inf.append([a + b, a - b])
inf.sort()
res = 1
last = 0
for i in range(1, n):
if inf[i][1] >= inf[last][0]:
res += 1
last = i
print(res) | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
f_inf = float("inf")
mod = 10**9 + 7
def resolve():
n = int(input())
XW = [list(map(int, input().split())) for _ in range(n)]
T = [[x + w, x - w] for x, w in XW]
T.sort(key=lambda x: x[0])
last = 0
res = 1
for i in range(n):
if T[i][1] >= T[last][0]:
last = i
res += 1
print(res)
resolve() | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
l = []
for i in range(n):
x, y = map(int, input().split())
l += [(x + y, x - y)]
l.sort()
r = -2000000000
a = 0
for u in l:
if u[1] >= r:
a += 1
r = u[0]
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | import sys
stdin = sys.stdin
ip = lambda: int(sp())
fp = lambda: float(sp())
lp = lambda: list(map(int, stdin.readline().split()))
sp = lambda: stdin.readline().rstrip()
Yp = lambda: print("Yes")
Np = lambda: print("No")
N = ip()
L = []
for _ in range(N):
x, w = lp()
L.append([x - w, x + w])
L.sort(reverse=True)
ans = 0
edge = 1 << 40
for i in range(N):
if L[i][1] <= edge:
edge = L[i][0]
ans += 1
print(ans) | IMPORT ASSIGN 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | N = int(input())
pairs = []
for i in range(N):
x, w = map(int, input().split())
pairs.append((x, w))
sorted_pairs = sorted(pairs, key=lambda x: x[0])
stack = []
stack.append(sorted_pairs[0])
for x, w in sorted_pairs[1:N]:
right_x, right_w = stack[-1]
if right_x + right_w <= x - w:
stack.append((x, w))
elif x + w < right_x + right_w:
stack[-1] = x, w
print(len(stack)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | N = int(input())
intervals = []
for i in range(N):
x, w = map(int, input().split())
left = x - w
right = x + w
intervals.append((left, right))
intervals.sort(key=lambda x: x[1])
left = -1000000007
ans = 0
for i in range(N):
if intervals[i][0] >= left:
ans += 1
left = intervals[i][1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
l = []
for _ in range(n):
k, m = map(int, input().strip().split())
l.append((k, m))
l.sort(key=lambda x: x[0] + x[1])
last = 0
ans = 1
for i in range(1, n):
if (
l[i][0] - l[i][1] >= l[last][0] - l[last][1]
and abs(l[i][0] - l[last][0]) >= l[i][1] + l[last][1]
):
last = i
ans = ans + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | length = int(input())
start = []
end = []
for i in range(length):
a, b = map(int, input().split(" "))
e, s = a - b, a + b
end.append([e, i])
start.append([s, i])
end.sort(key=lambda x: x[0])
start.sort(key=lambda x: x[0])
cant_visit = set()
answer = 0
end_index = 0
for s, i in start:
if i not in cant_visit:
answer += 1
while end_index < length and end[end_index][0] < s:
cant_visit |= {end[end_index][1]}
end_index += 1
print(answer) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate xi and weight wi. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |xi - xj| ≥ wi + wj.
Find the size of the maximum clique in such graph.
Input
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers xi, wi (0 ≤ xi ≤ 109, 1 ≤ wi ≤ 109) — the coordinate and the weight of a point. All xi are different.
Output
Print a single number — the number of vertexes in the maximum clique of the given graph.
Examples
Input
4
2 3
3 1
6 1
0 2
Output
3
Note
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test.
<image> | n = int(input())
x = [0] * n
w = [0] * n
for i in range(n):
x[i], w[i] = map(int, input().split())
xw = [([0] * 2) for _ in range(n)]
for i in range(n):
xw[i][0] = x[i] + w[i]
xw[i][1] = x[i] - w[i]
xw.sort()
now = xw[0][1]
ans = 0
for i in range(n):
if now <= xw[i][1]:
now = xw[i][0]
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution:
def findLongestChain(self, pairs):
pairs = sorted(pairs, key=lambda t: t[0])
p, L = None, 0
for t in sorted(pairs, key=lambda t: t[1]):
if p is None or t[0] > p:
p, L = t[1], L + 1
return L | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NONE NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NONE VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution:
def findLongestChain(self, pairs):
queue = collections.deque(sorted(pairs, key=lambda pair: pair[1]))
count = 0
while len(queue) > 0:
count += 1
curr = queue.popleft()
while len(queue) > 0 and queue[0][0] <= curr[1]:
queue.popleft()
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution:
def findLongestChain(self, pairs):
if not pairs:
return 0
pairs.sort(key=lambda x: x[1])
size = 1
prev = pairs[0]
for p in pairs[1:]:
if prev[1] < p[0]:
size += 1
prev = p
return size | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution(object):
def findLongestChain(self, pairs):
pairs = sorted(pairs, key=lambda x: x[1])
count = 1
curr = pairs[0]
for pair in pairs:
if curr[1] < pair[0]:
count += 1
curr = pair
return count | CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution:
def findLongestChain(self, pairs):
pairs = sorted([tuple(x) for x in pairs], key=lambda i: i[1])
current, count = -1061109567, 0
for a, b in pairs:
if a > current:
current = b
count += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution:
def findLongestChain(self, pairs):
pairs.sort(key=lambda x: x[1])
cur = pairs[0][0] - 1
ans = 0
for a, b in pairs:
if cur < a:
cur = b
ans += 1
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
You are given n pairs of numbers. In every pair, the first number is always smaller than the second number.
Now, we define a pair (c, d) can follow another pair (a, b) if and only if b < c. Chain of pairs can be formed in this fashion.
Given a set of pairs, find the length longest chain which can be formed. You needn't use up all the given pairs. You can select pairs in any order.
Example 1:
Input: [[1,2], [2,3], [3,4]]
Output: 2
Explanation: The longest chain is [1,2] -> [3,4]
Note:
The number of given pairs will be in the range [1, 1000]. | class Solution:
def findLongestChain(self, pairs):
cur, res = float("-inf"), 0
for x, y in sorted(pairs, key=operator.itemgetter(1)):
if cur < x:
cur = y
res += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR |
There is a planet named “Ruizland” having $N$ cities, These cities are connected in the form of a tree by a bidirectional path of specified length(in miles). Cities are numbered from $1$ to $N$. City number $1$ is the capital city.
$M$ pilgrims started their journey from the capital city having initial energy $E_{1}, E_{2}, \ldots, E_{M}$ respectively which reduces on moving from one city to another by a certain amount$(A)$.
$A$ is calculated as follows:-
Consider two cities $U$ and $V$ having an edge length of $K$ miles.
The total number of distinct cities from the capital city to the city $U$ is $D$ (city $U$ and capital city both are inclusive).
If a pilgrim moves from city $U$ to $V$, then $A=K*D$.
Out of $N$ cities, There are some special cities (except the capital city) that are connected to only one city. All pilgrims want to reach any of the special cities. A pilgrim will stop his journey as soon as the energy of the pilgrims becomes $0$ or reaches any of the special cities.
Initially, all cities have $0$ pilgrims except the capital city.
Find the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims given that pilgrims are allowed to take any path during their journey.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two integers $N$ and $M$ separated by space, where $N$ is the number of cities and $M$ is the number of pilgrims.
The second line contains $M$ integers separated by spaces, denoting the initial energy of the pilgrims.
Next, $N-1$ lines consists of three integers $U$, $V$ and $K$ separated by space where city $U$ and $V$ are connected by a bidirectional path whose length is $K$ miles
------ Output ------
For each test case, Print the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims.
------ Constraints ------
$1 ≤ T ≤ 50$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$1 ≤ E_{i} ≤ 10^{18}$
$1 ≤ U, V ≤ N$
$1 ≤ K ≤ 10^{6}$
It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$.
It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
1
6 4
100 20 10 50
1 2 10
2 3 20
2 4 30
4 5 40
1 6 50
----- Sample Output 1 ------
2
----- explanation 1 ------
There are $3$ special cities namely $3, 5$ and $6$. The energy expected to reach these special cities are $50, 190$ and $50$ respectively. Special city $5$ cannot be reached by any pilgrim. If Pilgrim $1$ goes to the special city $3$ and pilgrim $4$ goes to the special city $6$, then the maximum number of non-empty special cities is $2$. | from sys import setrecursionlimit
setrecursionlimit(10**8)
def dfs(p, prev, val, d):
for i in child[p]:
if i[0] != prev:
dfs(i[0], p, val + i[1] * d, d + 1)
if len(child[p]) == 1 and prev != 0:
x.append(val)
def answer():
ans, ind = 0, 0
for i in range(len(x)):
for j in range(ind, len(e)):
if e[j] >= x[i]:
ind = j + 1
ans += 1
break
return ans
for T in range(int(input())):
n, m = map(int, input().split())
e = sorted(list(map(int, input().split())))
child = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v, k = map(int, input().split())
child[u].append([v, k])
child[v].append([u, k])
x = []
dfs(1, 0, 0, 1)
x.sort()
print(answer()) | EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF FOR VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
There is a planet named “Ruizland” having $N$ cities, These cities are connected in the form of a tree by a bidirectional path of specified length(in miles). Cities are numbered from $1$ to $N$. City number $1$ is the capital city.
$M$ pilgrims started their journey from the capital city having initial energy $E_{1}, E_{2}, \ldots, E_{M}$ respectively which reduces on moving from one city to another by a certain amount$(A)$.
$A$ is calculated as follows:-
Consider two cities $U$ and $V$ having an edge length of $K$ miles.
The total number of distinct cities from the capital city to the city $U$ is $D$ (city $U$ and capital city both are inclusive).
If a pilgrim moves from city $U$ to $V$, then $A=K*D$.
Out of $N$ cities, There are some special cities (except the capital city) that are connected to only one city. All pilgrims want to reach any of the special cities. A pilgrim will stop his journey as soon as the energy of the pilgrims becomes $0$ or reaches any of the special cities.
Initially, all cities have $0$ pilgrims except the capital city.
Find the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims given that pilgrims are allowed to take any path during their journey.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two integers $N$ and $M$ separated by space, where $N$ is the number of cities and $M$ is the number of pilgrims.
The second line contains $M$ integers separated by spaces, denoting the initial energy of the pilgrims.
Next, $N-1$ lines consists of three integers $U$, $V$ and $K$ separated by space where city $U$ and $V$ are connected by a bidirectional path whose length is $K$ miles
------ Output ------
For each test case, Print the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims.
------ Constraints ------
$1 ≤ T ≤ 50$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$1 ≤ E_{i} ≤ 10^{18}$
$1 ≤ U, V ≤ N$
$1 ≤ K ≤ 10^{6}$
It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$.
It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
1
6 4
100 20 10 50
1 2 10
2 3 20
2 4 30
4 5 40
1 6 50
----- Sample Output 1 ------
2
----- explanation 1 ------
There are $3$ special cities namely $3, 5$ and $6$. The energy expected to reach these special cities are $50, 190$ and $50$ respectively. Special city $5$ cannot be reached by any pilgrim. If Pilgrim $1$ goes to the special city $3$ and pilgrim $4$ goes to the special city $6$, then the maximum number of non-empty special cities is $2$. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
arr = list(map(int, input().split()))
d = {}
dist = {}
for i in range(1, n + 1):
d[i] = set()
for i in range(n - 1):
u, v, k = map(int, input().split())
d[u].add(v)
d[v].add(u)
dist[u, v] = k
dist[v, u] = k
visited = set()
l = []
l.append((1, 0, 0))
visited.add(1)
count = 0
cost = 0
e = []
while l:
o, c, count = l.pop(0)
num = 0
for nbour in d[o]:
if nbour not in visited:
num = num + 1
visited.add(nbour)
cost = c + (count + 1) * dist[o, nbour]
l.append((nbour, cost, count + 1))
if num == 0:
e.append(c)
arr.sort()
e.sort()
i = 0
ans = 0
j = 0
ln1 = len(arr)
ln2 = len(e)
while i < ln1 and j < ln2:
if arr[i] >= e[j]:
i += 1
j += 1
ans += 1
else:
i += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a planet named “Ruizland” having $N$ cities, These cities are connected in the form of a tree by a bidirectional path of specified length(in miles). Cities are numbered from $1$ to $N$. City number $1$ is the capital city.
$M$ pilgrims started their journey from the capital city having initial energy $E_{1}, E_{2}, \ldots, E_{M}$ respectively which reduces on moving from one city to another by a certain amount$(A)$.
$A$ is calculated as follows:-
Consider two cities $U$ and $V$ having an edge length of $K$ miles.
The total number of distinct cities from the capital city to the city $U$ is $D$ (city $U$ and capital city both are inclusive).
If a pilgrim moves from city $U$ to $V$, then $A=K*D$.
Out of $N$ cities, There are some special cities (except the capital city) that are connected to only one city. All pilgrims want to reach any of the special cities. A pilgrim will stop his journey as soon as the energy of the pilgrims becomes $0$ or reaches any of the special cities.
Initially, all cities have $0$ pilgrims except the capital city.
Find the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims given that pilgrims are allowed to take any path during their journey.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two integers $N$ and $M$ separated by space, where $N$ is the number of cities and $M$ is the number of pilgrims.
The second line contains $M$ integers separated by spaces, denoting the initial energy of the pilgrims.
Next, $N-1$ lines consists of three integers $U$, $V$ and $K$ separated by space where city $U$ and $V$ are connected by a bidirectional path whose length is $K$ miles
------ Output ------
For each test case, Print the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims.
------ Constraints ------
$1 ≤ T ≤ 50$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$1 ≤ E_{i} ≤ 10^{18}$
$1 ≤ U, V ≤ N$
$1 ≤ K ≤ 10^{6}$
It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$.
It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
1
6 4
100 20 10 50
1 2 10
2 3 20
2 4 30
4 5 40
1 6 50
----- Sample Output 1 ------
2
----- explanation 1 ------
There are $3$ special cities namely $3, 5$ and $6$. The energy expected to reach these special cities are $50, 190$ and $50$ respectively. Special city $5$ cannot be reached by any pilgrim. If Pilgrim $1$ goes to the special city $3$ and pilgrim $4$ goes to the special city $6$, then the maximum number of non-empty special cities is $2$. | import sys
sys.setrecursionlimit(10**9)
def traverse(n, m, adjList, sp, src, visited, u, prev):
if len(adjList[src]) == 1 and visited[adjList[src][0][0]]:
sp.append(prev)
else:
for a in adjList[src]:
if not visited[a[0]]:
visited[a[0]] = True
traverse(n, m, adjList, sp, a[0], visited, u + 1, prev + a[1] * u)
for _ in range(int(input())):
n, m = map(int, input().split())
e = sorted(list(map(int, input().split())))
adjList = [[] for _ in range(n)]
for _ in range(n - 1):
a, b, k = map(int, input().split())
a -= 1
b -= 1
adjList[a].append([b, k])
adjList[b].append([a, k])
sp = []
visited = [False] * n
visited[0] = True
traverse(n, m, adjList, sp, 0, visited, 1, 0)
sp.sort()
i = 0
j = 0
while i < len(e) and j < len(sp):
if sp[j] <= e[i]:
i += 1
j += 1
else:
i += 1
print(j) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a planet named “Ruizland” having $N$ cities, These cities are connected in the form of a tree by a bidirectional path of specified length(in miles). Cities are numbered from $1$ to $N$. City number $1$ is the capital city.
$M$ pilgrims started their journey from the capital city having initial energy $E_{1}, E_{2}, \ldots, E_{M}$ respectively which reduces on moving from one city to another by a certain amount$(A)$.
$A$ is calculated as follows:-
Consider two cities $U$ and $V$ having an edge length of $K$ miles.
The total number of distinct cities from the capital city to the city $U$ is $D$ (city $U$ and capital city both are inclusive).
If a pilgrim moves from city $U$ to $V$, then $A=K*D$.
Out of $N$ cities, There are some special cities (except the capital city) that are connected to only one city. All pilgrims want to reach any of the special cities. A pilgrim will stop his journey as soon as the energy of the pilgrims becomes $0$ or reaches any of the special cities.
Initially, all cities have $0$ pilgrims except the capital city.
Find the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims given that pilgrims are allowed to take any path during their journey.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two integers $N$ and $M$ separated by space, where $N$ is the number of cities and $M$ is the number of pilgrims.
The second line contains $M$ integers separated by spaces, denoting the initial energy of the pilgrims.
Next, $N-1$ lines consists of three integers $U$, $V$ and $K$ separated by space where city $U$ and $V$ are connected by a bidirectional path whose length is $K$ miles
------ Output ------
For each test case, Print the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims.
------ Constraints ------
$1 ≤ T ≤ 50$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$1 ≤ E_{i} ≤ 10^{18}$
$1 ≤ U, V ≤ N$
$1 ≤ K ≤ 10^{6}$
It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$.
It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
1
6 4
100 20 10 50
1 2 10
2 3 20
2 4 30
4 5 40
1 6 50
----- Sample Output 1 ------
2
----- explanation 1 ------
There are $3$ special cities namely $3, 5$ and $6$. The energy expected to reach these special cities are $50, 190$ and $50$ respectively. Special city $5$ cannot be reached by any pilgrim. If Pilgrim $1$ goes to the special city $3$ and pilgrim $4$ goes to the special city $6$, then the maximum number of non-empty special cities is $2$. | def dfs(node, parent):
s = set()
visited[1] = True
stack = [[node, parent]]
levelarray[node] = 1
while stack:
node, parent = stack.pop()
levelarray[node] += levelarray[parent]
costarray[node] = costarray[node] + costarray[parent]
flag = 1
for child, cost in g[node]:
if visited[child] == False and child != parent:
costarray[child] += levelarray[node] * cost
flag = 0
visited[child] = True
levelarray[child] += 1
stack.append([child, node])
if flag == 1:
s.add(node)
return s
for _ in range(int(input())):
n, m = map(int, input().split())
l = list(map(int, input().split()))
g = {i: [] for i in range(1, n + 1)}
s = set()
visited = [(False) for i in range(n + 1)]
levelarray = [(0) for i in range(n + 1)]
costarray = [(0) for i in range(n + 1)]
for i in range(n - 1):
u, v, c = map(int, input().split())
g[u].append([v, c])
g[v].append([u, c])
s = dfs(1, 0)
p = [costarray[i] for i in s]
ans = 0
p.sort()
a = len(p)
l.sort()
i = 0
ans = 0
j = 0
while i < m and j < a:
if l[i] < p[j]:
i += 1
else:
i += 1
j += 1
ans += 1
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a planet named “Ruizland” having $N$ cities, These cities are connected in the form of a tree by a bidirectional path of specified length(in miles). Cities are numbered from $1$ to $N$. City number $1$ is the capital city.
$M$ pilgrims started their journey from the capital city having initial energy $E_{1}, E_{2}, \ldots, E_{M}$ respectively which reduces on moving from one city to another by a certain amount$(A)$.
$A$ is calculated as follows:-
Consider two cities $U$ and $V$ having an edge length of $K$ miles.
The total number of distinct cities from the capital city to the city $U$ is $D$ (city $U$ and capital city both are inclusive).
If a pilgrim moves from city $U$ to $V$, then $A=K*D$.
Out of $N$ cities, There are some special cities (except the capital city) that are connected to only one city. All pilgrims want to reach any of the special cities. A pilgrim will stop his journey as soon as the energy of the pilgrims becomes $0$ or reaches any of the special cities.
Initially, all cities have $0$ pilgrims except the capital city.
Find the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims given that pilgrims are allowed to take any path during their journey.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two integers $N$ and $M$ separated by space, where $N$ is the number of cities and $M$ is the number of pilgrims.
The second line contains $M$ integers separated by spaces, denoting the initial energy of the pilgrims.
Next, $N-1$ lines consists of three integers $U$, $V$ and $K$ separated by space where city $U$ and $V$ are connected by a bidirectional path whose length is $K$ miles
------ Output ------
For each test case, Print the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims.
------ Constraints ------
$1 ≤ T ≤ 50$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$1 ≤ E_{i} ≤ 10^{18}$
$1 ≤ U, V ≤ N$
$1 ≤ K ≤ 10^{6}$
It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$.
It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
1
6 4
100 20 10 50
1 2 10
2 3 20
2 4 30
4 5 40
1 6 50
----- Sample Output 1 ------
2
----- explanation 1 ------
There are $3$ special cities namely $3, 5$ and $6$. The energy expected to reach these special cities are $50, 190$ and $50$ respectively. Special city $5$ cannot be reached by any pilgrim. If Pilgrim $1$ goes to the special city $3$ and pilgrim $4$ goes to the special city $6$, then the maximum number of non-empty special cities is $2$. | import sys
sys.setrecursionlimit(1000001)
def dfs(start, par, ln, val):
foo = 0
ln += 1
for child in adj[start]:
if child[0] != par:
foo = 1
amt = child[1]
dfs(child[0], start, ln, val + ln * amt)
if foo == 0:
arr.append(val)
for y in range(int(input())):
n, m = map(int, input().split())
lst = list(map(int, input().split()))
adj = []
for i in range(n):
adj.append([])
for i in range(n - 1):
a, b, c = map(int, input().split())
adj[a - 1].append([b - 1, c])
adj[b - 1].append([a - 1, c])
arr = []
dfs(0, -1, 0, 0)
arr.sort()
lst.sort()
i = 0
cnt = 0
j = 0
ln1 = len(lst)
ln2 = len(arr)
while i < ln1 and j < ln2:
if lst[i] >= arr[j]:
i += 1
j += 1
cnt += 1
else:
i += 1
print(cnt) | IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a planet named “Ruizland” having $N$ cities, These cities are connected in the form of a tree by a bidirectional path of specified length(in miles). Cities are numbered from $1$ to $N$. City number $1$ is the capital city.
$M$ pilgrims started their journey from the capital city having initial energy $E_{1}, E_{2}, \ldots, E_{M}$ respectively which reduces on moving from one city to another by a certain amount$(A)$.
$A$ is calculated as follows:-
Consider two cities $U$ and $V$ having an edge length of $K$ miles.
The total number of distinct cities from the capital city to the city $U$ is $D$ (city $U$ and capital city both are inclusive).
If a pilgrim moves from city $U$ to $V$, then $A=K*D$.
Out of $N$ cities, There are some special cities (except the capital city) that are connected to only one city. All pilgrims want to reach any of the special cities. A pilgrim will stop his journey as soon as the energy of the pilgrims becomes $0$ or reaches any of the special cities.
Initially, all cities have $0$ pilgrims except the capital city.
Find the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims given that pilgrims are allowed to take any path during their journey.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two integers $N$ and $M$ separated by space, where $N$ is the number of cities and $M$ is the number of pilgrims.
The second line contains $M$ integers separated by spaces, denoting the initial energy of the pilgrims.
Next, $N-1$ lines consists of three integers $U$, $V$ and $K$ separated by space where city $U$ and $V$ are connected by a bidirectional path whose length is $K$ miles
------ Output ------
For each test case, Print the maximum count of special cities that is non-empty (at least one pilgrim is present) after the end of the journey of all the pilgrims.
------ Constraints ------
$1 ≤ T ≤ 50$
$2 ≤ N ≤ 10^{5}$
$1 ≤ M ≤ 10^{5}$
$1 ≤ E_{i} ≤ 10^{18}$
$1 ≤ U, V ≤ N$
$1 ≤ K ≤ 10^{6}$
It is guaranteed that the sum of $N$ over all test cases does not exceed $10^{5}$.
It is guaranteed that the sum of $M$ over all test cases does not exceed $10^{5}$.
----- Sample Input 1 ------
1
6 4
100 20 10 50
1 2 10
2 3 20
2 4 30
4 5 40
1 6 50
----- Sample Output 1 ------
2
----- explanation 1 ------
There are $3$ special cities namely $3, 5$ and $6$. The energy expected to reach these special cities are $50, 190$ and $50$ respectively. Special city $5$ cannot be reached by any pilgrim. If Pilgrim $1$ goes to the special city $3$ and pilgrim $4$ goes to the special city $6$, then the maximum number of non-empty special cities is $2$. | import sys
sys.setrecursionlimit(10**8)
def dfs(node, e, parent, d):
isLeaf = True
for v, k in Edges[node]:
if v != parent:
dfs(v, e + k * d, node, d + 1)
isLeaf = False
if isLeaf:
SCE.append(e)
t = int(input())
for tc in range(t):
n, m = map(int, input().split())
PE = [0] + list(map(int, input().split()))
Edges = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v, k = map(int, input().split())
Edges[u].append([v, k])
Edges[v].append([u, k])
SCE = []
dfs(1, 0, 0, 1)
count = 0
PE = sorted(PE)
SCE = sorted(SCE)
j = m
for i in range(len(SCE) - 1, -1, -1):
if j > 0 and SCE[i] <= PE[j]:
count += 1
j -= 1
print(count) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There is a grid, consisting of $2$ rows and $m$ columns. The rows are numbered from $1$ to $2$ from top to bottom. The columns are numbered from $1$ to $m$ from left to right.
The robot starts in a cell $(1, 1)$. In one second, it can perform either of two actions:
move into a cell adjacent by a side: up, right, down or left;
remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell $(1, 1)$, are locked. Each cell $(i, j)$ contains a value $a_{i,j}$ — the moment that this cell gets unlocked. The robot can only move into a cell $(i, j)$ if at least $a_{i,j}$ seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell $(1, 1)$ is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
The first line of each testcase contains a single integer $m$ ($2 \le m \le 2 \cdot 10^5$) — the number of columns of the grid.
The $i$-th of the next $2$ lines contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($0 \le a_{i,j} \le 10^9$) — the moment of time each cell gets unlocked. $a_{1,1} = 0$. If $a_{i,j} = 0$, then cell $(i, j)$ is unlocked from the start.
The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
-----Examples-----
Input
4
3
0 0 1
4 3 2
5
0 4 8 12 16
2 6 10 14 18
4
0 10 10 10
10 10 10 10
2
0 0
0 0
Output
5
19
17
3
-----Note-----
None | for _ in range(int(input())):
c = int(input())
a = [list(map(int, input().split())) for _ in range(2)]
a[0][0] = -1
pos = 0, 0
dync = [(0) for _ in range(c)]
y = 0
for x in range(c - 1):
y = not y
curr = max(a[y][x] + 2, a[y][x + 1] + 1)
dync[x + 1] = max(curr, dync[x] + 2)
top = list(range(0, c, 2))
latestTime = 0
for x in top[::-1]:
dist = (c - x) * 2 - 1
if x + 1 < c:
curr = max(
a[0][x] + dist + 1, a[0][x + 1] + dist, a[1][x] + 1, a[1][x + 1] + 2
)
else:
curr = max(a[0][x] + 2, a[1][x] + 1)
latestTime = max(latestTime + 2, curr)
dync[x] = max(dync[x] + dist, latestTime)
bottom = list(range(1, c, 2))
latestTime = 0
for x in bottom[::-1]:
dist = (c - x) * 2 - 1
if x + 1 < c:
curr = max(
a[1][x] + dist + 1, a[1][x + 1] + dist, a[0][x] + 1, a[0][x + 1] + 2
)
else:
curr = max(a[1][x] + 2, a[0][x] + 1)
latestTime = max(latestTime + 2, curr)
dync[x] = max(dync[x] + dist, latestTime)
print(min(dync)) | 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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
There is a grid, consisting of $2$ rows and $m$ columns. The rows are numbered from $1$ to $2$ from top to bottom. The columns are numbered from $1$ to $m$ from left to right.
The robot starts in a cell $(1, 1)$. In one second, it can perform either of two actions:
move into a cell adjacent by a side: up, right, down or left;
remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell $(1, 1)$, are locked. Each cell $(i, j)$ contains a value $a_{i,j}$ — the moment that this cell gets unlocked. The robot can only move into a cell $(i, j)$ if at least $a_{i,j}$ seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell $(1, 1)$ is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
The first line of each testcase contains a single integer $m$ ($2 \le m \le 2 \cdot 10^5$) — the number of columns of the grid.
The $i$-th of the next $2$ lines contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($0 \le a_{i,j} \le 10^9$) — the moment of time each cell gets unlocked. $a_{1,1} = 0$. If $a_{i,j} = 0$, then cell $(i, j)$ is unlocked from the start.
The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
-----Examples-----
Input
4
3
0 0 1
4 3 2
5
0 4 8 12 16
2 6 10 14 18
4
0 10 10 10
10 10 10 10
2
0 0
0 0
Output
5
19
17
3
-----Note-----
None | INF = 2 * 10**9
for _ in range(int(input())):
m = int(input())
a = [[int(x) for x in input().split()] for i in range(2)]
su = [[(-INF) for j in range(m + 1)] for i in range(2)]
for i in range(2):
for j in range(m - 1, -1, -1):
su[i][j] = max(su[i][j + 1] - 1, a[i][j], a[i ^ 1][j] - (2 * (m - j) - 1))
pr = a[0][0] - 1
ans = INF
for j in range(m):
jj = j & 1
ans = min(ans, max(pr, su[jj][j + 1] - 2 * j - 1, a[jj ^ 1][j] - 2 * m + 1))
pr = max(pr, a[jj ^ 1][j] - 2 * j - 1)
ans = min(ans, max(pr, su[jj ^ 1][j + 1] - 2 * j - 2))
if j < m - 1:
pr = max(pr, a[jj ^ 1][j + 1] - 2 * j - 2)
print(ans + 2 * m) | ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR 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 VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER VAR |
There is a grid, consisting of $2$ rows and $m$ columns. The rows are numbered from $1$ to $2$ from top to bottom. The columns are numbered from $1$ to $m$ from left to right.
The robot starts in a cell $(1, 1)$. In one second, it can perform either of two actions:
move into a cell adjacent by a side: up, right, down or left;
remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell $(1, 1)$, are locked. Each cell $(i, j)$ contains a value $a_{i,j}$ — the moment that this cell gets unlocked. The robot can only move into a cell $(i, j)$ if at least $a_{i,j}$ seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell $(1, 1)$ is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
The first line of each testcase contains a single integer $m$ ($2 \le m \le 2 \cdot 10^5$) — the number of columns of the grid.
The $i$-th of the next $2$ lines contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($0 \le a_{i,j} \le 10^9$) — the moment of time each cell gets unlocked. $a_{1,1} = 0$. If $a_{i,j} = 0$, then cell $(i, j)$ is unlocked from the start.
The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
-----Examples-----
Input
4
3
0 0 1
4 3 2
5
0 4 8 12 16
2 6 10 14 18
4
0 10 10 10
10 10 10 10
2
0 0
0 0
Output
5
19
17
3
-----Note-----
None | t = int(input())
for _ in range(t):
m = int(input())
r = [list(map(int, input().split())) for i in range(2)]
r[0][0] = -1
L = [0]
R = [[[0], [0]], [[0], [0]]]
for i in range(m):
s = i & 1
L.append(max(L[-1], 2 * (m - i) + max(r[0][i] - s, r[1][i] - 1 + s)))
for j in range(2):
for k in range(2):
R[j][k].append(max(R[j][k][-1], r[j][-i - 1] + i * (2 * k - 1)))
B = m + 1 + max(R[0][1][m], R[1][0][m] - 1)
for i in range(1, m):
s = i & 1
B = min(
B,
max(L[i], m + 1 - i + max(R[0][1 - s][m - i] - s, R[1][s][m - i] - 1 + s)),
)
print(B) | 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 FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST LIST LIST NUMBER LIST NUMBER LIST LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
There is a grid, consisting of $2$ rows and $m$ columns. The rows are numbered from $1$ to $2$ from top to bottom. The columns are numbered from $1$ to $m$ from left to right.
The robot starts in a cell $(1, 1)$. In one second, it can perform either of two actions:
move into a cell adjacent by a side: up, right, down or left;
remain in the same cell.
The robot is not allowed to move outside the grid.
Initially, all cells, except for the cell $(1, 1)$, are locked. Each cell $(i, j)$ contains a value $a_{i,j}$ — the moment that this cell gets unlocked. The robot can only move into a cell $(i, j)$ if at least $a_{i,j}$ seconds have passed before the move.
The robot should visit all cells without entering any cell twice or more (cell $(1, 1)$ is considered entered at the start). It can finish in any cell.
What is the fastest the robot can achieve that?
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases.
The first line of each testcase contains a single integer $m$ ($2 \le m \le 2 \cdot 10^5$) — the number of columns of the grid.
The $i$-th of the next $2$ lines contains $m$ integers $a_{i,1}, a_{i,2}, \dots, a_{i,m}$ ($0 \le a_{i,j} \le 10^9$) — the moment of time each cell gets unlocked. $a_{1,1} = 0$. If $a_{i,j} = 0$, then cell $(i, j)$ is unlocked from the start.
The sum of $m$ over all testcases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.
-----Examples-----
Input
4
3
0 0 1
4 3 2
5
0 4 8 12 16
2 6 10 14 18
4
0 10 10 10
10 10 10 10
2
0 0
0 0
Output
5
19
17
3
-----Note-----
None | INF = 2 * 10**9
for _ in range(int(input())):
m, a = int(input()), [list(map(int, input().split())) for _ in range(2)]
f = [[[(-INF) for _ in range(m + 1)] for _ in range(2)] for _ in range(2)]
for i in range(m - 1, -1, -1):
for j in range(2):
f[j][0][i] = max(f[j][0][i + 1], a[j][i] + i)
f[j][1][i] = max(f[j][1][i + 1], a[j][i] - i)
a[0][0] = -1
s, ans = 0, INF
for i in range(m):
t = i & 1
s = max(s, a[t][i] + 2 * (m - i))
ans = min(ans, max(s, f[t ^ 1][0][i] - i + 1, f[t][1][i + 1] + 2 * m - i))
s = max(s, a[t ^ 1][i] + 2 * (m - i) - 1)
print(ans) | ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
(*a,) = map(int, input().split())
d = {}
mx = 0
for i in range(n):
d[a[i] - i] = d.get(a[i] - i, 0) + a[i]
mx = max(mx, d[a[i] - i])
print(mx) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
B = list(map(int, input().split()))
pp = {}
for i in range(n):
if B[i] - (i + 1) not in pp:
pp[B[i] - (i + 1)] = 0
pp[B[i] - (i + 1)] += B[i]
ans = 0
for c in pp:
ans = max(ans, pp[c])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
a = list(map(int, input().split()))
cnt = [0] * 800001
for i in range(n):
cnt[a[i] - i] += a[i]
print(max(cnt)) | 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 NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
b = list(map(int, input().split()))
numbers = dict()
_max = 0
for i in range(n):
num = b[i] - i
if num not in numbers:
numbers[num] = b[i]
else:
numbers[num] += b[i]
_max = max(_max, numbers[num])
print(_max) | 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 VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(n):
if l[i] - i in d:
d[l[i] - i] += l[i]
else:
d[l[i] - i] = l[i]
print(max(d.values())) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
b = list(map(int, input().split()))
final = {}
for i in range(n):
if i - b[i] not in final:
final[i - b[i]] = b[i]
else:
final[i - b[i]] += b[i]
ans = 0
for i in final:
if final[i] > ans:
ans = final[i]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
dic = {}
for i, a in enumerate(A):
b = a - i
if b in dic:
dic[b] += a
else:
dic[b] = a
print(max(list(dic.values()))) | 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 DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
b = list(map(int, input().split()))
po = [0] * 1000001
ne = [0] * 1000001
for i in range(n):
if i + 1 - b[i] > 0:
po[abs(i + 1 - b[i])] += b[i]
else:
ne[abs(i + 1 - b[i])] += b[i]
print(max(max(po), max(ne))) | 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 NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
a = list(map(int, input().split()))
res = {}
for i in range(n):
res[a[i] - i] = res.get(a[i] - i, 0) + a[i]
print(max(res.items(), key=lambda x: x[1])[1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
b = [i for i in map(int, input().split())]
vis = [(0) for i in range(600010)]
for i in range(n):
vis[b[i] - i] = vis[b[i] - i] + b[i]
print(max(vis)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
x = [0] * (4 * 10**6 + 1)
bi = [*map(int, input().split())]
for i in range(n):
x[bi[i] - i] += bi[i]
print(max(x)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
b = [int(ele) for ele in input().split()]
diff = {}
for i in range(n):
diff[b[i] - i] = 0
for i in range(0, n):
diff[b[i] - i] += b[i]
maxi = 0
for key in diff.keys():
maxi = max(maxi, diff[key])
print(maxi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | input()
d = {}
i = 0
for x in map(int, input().split()):
d[x - i] = d.get(x - i, 0) + x
i += 1
print(max(d.values())) | EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR |
Tanya wants to go on a journey across the cities of Berland. There are n cities situated along the main railroad line of Berland, and these cities are numbered from 1 to n.
Tanya plans her journey as follows. First of all, she will choose some city c_1 to start her journey. She will visit it, and after that go to some other city c_2 > c_1, then to some other city c_3 > c_2, and so on, until she chooses to end her journey in some city c_k > c_{k - 1}. So, the sequence of visited cities [c_1, c_2, ..., c_k] should be strictly increasing.
There are some additional constraints on the sequence of cities Tanya visits. Each city i has a beauty value b_i associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities c_i and c_{i + 1}, the condition c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i} must hold.
For example, if n = 8 and b = [3, 4, 4, 6, 6, 7, 8, 9], there are several three possible ways to plan a journey:
* c = [1, 2, 4];
* c = [3, 5, 6, 8];
* c = [7] (a journey consisting of one city is also valid).
There are some additional ways to plan a journey that are not listed above.
Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey?
Input
The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of cities in Berland.
The second line contains n integers b_1, b_2, ..., b_n (1 ≤ b_i ≤ 4 ⋅ 10^5), where b_i is the beauty value of the i-th city.
Output
Print one integer — the maximum beauty of a journey Tanya can choose.
Examples
Input
6
10 7 1 9 10 15
Output
26
Input
1
400000
Output
400000
Input
7
8 9 26 11 12 29 14
Output
55
Note
The optimal journey plan in the first example is c = [2, 4, 5].
The optimal journey plan in the second example is c = [1].
The optimal journey plan in the third example is c = [3, 6]. | n = int(input())
beauty = list(map(int, input().split()))
norm_beauty = [(b - i) for i, b in enumerate(beauty)]
max_beuty = {}
m = 0
for i, x in enumerate(norm_beauty):
if x in max_beuty:
max_beuty[x] += beauty[i]
else:
max_beuty[x] = beauty[i]
m = max(max_beuty[x], m)
print(m) | 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 VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
dp = [[(0) for y in range(len(A))] for x in range(K)]
add = 0
pref = [(0) for i in range(len(A))]
for x in range(len(A)):
add += A[x]
pref[x] = add
for x in range(len(A)):
dp[0][x] = pref[x] / (x + 1)
for y in range(1, K):
dp[y][0] = A[0]
for x in range(1, K):
for y in range(1, len(A)):
val1 = pref[y] / (y + 1)
maxi = val1
for z in range(y):
val = dp[x - 1][z] + (pref[y] - pref[z]) / (y - z)
maxi = max(maxi, val)
dp[x][y] = maxi
return dp[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
dp = [([0] * n) for _ in range(K)]
for i in range(n):
dp[0][i] = sum(A[: i + 1]) / (i + 1)
for l in range(1, K):
for i in range(l, n):
for j in range(i):
dp[l][i] = max(
dp[l][i], dp[l - 1][j] + sum(A[j + 1 : i + 1]) / (i - j)
)
print(dp)
return dp[K - 1][n - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
self.arr = [[None for i in range(K)] for i in range(len(A))]
res = self.solve(A, 0, K, -1)
return res
def solve(self, arr, i, grpLeft, lastInd):
if i == len(arr):
return 0
if self.arr[lastInd + 1][grpLeft - 1] != None:
return self.arr[lastInd + 1][grpLeft - 1]
if grpLeft == 1:
self.arr[lastInd + 1][0] = sum(arr[lastInd + 1 :]) / (
len(arr) - lastInd - 1
)
return self.arr[i][0]
avg = float(sum(arr[lastInd + 1 : i + 1])) / (i - lastInd)
self.arr[lastInd + 1][grpLeft - 1] = max(
self.solve(arr, i + 1, grpLeft, lastInd),
avg + self.solve(arr, i + 1, grpLeft - 1, i),
)
return self.arr[lastInd + 1][grpLeft - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NONE RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
if K >= n:
return sum(A)
B = [0] * (n + 1)
B[1] = A[0]
for i in range(1, n):
B[i + 1] = B[i] + A[i]
maxSum = 0
@lru_cache(None)
def dp(si, ei, remainK):
if ei == n:
return (B[ei] - B[si]) / (ei - si)
tmp = 0
if remainK > 1:
tmp = max(
tmp, (B[ei] - B[si]) / (ei - si) + dp(ei, ei + 1, remainK - 1)
)
tmp = max(tmp, dp(si, ei + 1, remainK))
return tmp
maxSum = dp(0, 1, K)
print(maxSum)
return maxSum | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A, K: int) -> float:
A = [0] + A
len_a = len(A)
dp = [([0] * (K + 1)) for _ in range(len_a)]
for i in range(1, len_a):
dp[i][0] = -float("inf")
for i in range(1, len_a):
for k in range(1, min(K + 1, i + 1)):
for j in range(k, i + 1):
dp[i][k] = max(
dp[i][k], dp[j - 1][k - 1] + self.helper(A[j : i + 1])
)
return dp[-1][-1]
def helper(self, sub_a):
return sum(sub_a) / len(sub_a) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
l_A = len(A)
memo = {(l_A, 0): 0}
def dfs(idx, rem):
if rem == 0 or l_A - idx < rem:
return 0
if rem == 1:
return sum(A[idx:]) / (l_A - idx)
if (idx, rem) in memo:
return memo[idx, rem]
sum_so_far = 0
avg = 0
best = 0
for i in range(idx, l_A):
sum_so_far += A[i]
avg = sum_so_far / (i - idx + 1)
best = max(best, avg + dfs(i + 1, rem - 1))
memo[idx, rem] = best
return best
return dfs(0, K) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER BIN_OP VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
self.memo = {}
def helper(start, end, K, prefix):
if (start, end, K) in self.memo.keys():
return self.memo[start, end, K]
if K == 0:
return (prefix[end + 1] - prefix[start]) / (end - start + 1)
if end == start:
return prefix[end + 1] - prefix[end]
if end < start:
return 0
maxAvg = 0
for i in range(start, end):
maxAvg = max(
maxAvg,
(prefix[i + 1] - prefix[start]) / (i - start + 1)
+ helper(i + 1, end, K - 1, prefix),
)
self.memo[start, end, K] = maxAvg
return maxAvg
n = len(A)
prefix = [(0) for _ in range(n + 1)]
for i in range(1, n + 1):
prefix[i] = prefix[i - 1] + A[i - 1]
ans = 0
for k in range(K):
ans = max(ans, helper(0, n - 1, k, prefix))
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR FUNC_CALL VAR RETURN VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
accsum = A[:]
l = len(accsum)
for i in range(1, l):
accsum[i] += accsum[i - 1]
cache = {}
def dp(i, k):
prev = accsum[i - 1] if i > 0 else 0
if k == 1:
res = (accsum[l - 1] - prev) / (l - i)
cache[i, k] = res
return res
if (i, k) in cache:
return cache[i, k]
if l - i < k:
return -float("inf")
if l - i == k:
res = accsum[l - 1] - prev
cache[i, k] = res
return res
res = 0
for j in range(i, l - k + 1):
res = max(res, (accsum[j] - prev) / (j - i + 1) + dp(j + 1, k - 1))
cache[i, k] = res
return res
return dp(0, K) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
dp = [([0] * K) for _ in range(n)]
dp[0][0] = A[0]
for i in range(1, n):
dp[i][0] = (dp[i - 1][0] * i + A[i]) / (i + 1)
for k in range(1, K):
for i in range(k, n):
curr_sum = 0
for j in reversed(range(k, i + 1)):
curr_sum += A[j]
dp[i][k] = max(dp[i][k], dp[j - 1][k - 1] + curr_sum / (i + 1 - j))
return dp[n - 1][K - 1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
P = [0]
for x in A:
P.append(P[-1] + x)
def average(i, j):
return (P[j] - P[i]) / float(j - i)
N = len(A)
dp = [average(i, N) for i in range(N)]
print(dp)
for k in range(K - 1):
for i in range(N):
for j in range(i + 1, N):
dp[i] = max(dp[i], average(i, j) + dp[j])
return dp[0] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
prefix = [A[0]]
for i in range(1, len(A)):
prefix.append(prefix[i - 1] + A[i])
dp = [
[(prefix[i] / (i + 1) if k == 1 else 0) for k in range(K + 1)]
for i in range(len(A))
]
for i in range(1, len(A)):
for k in range(2, min(i + 2, K + 1)):
val = float("-inf")
_sum = 0
for j in range(i, 0, -1):
_sum += A[j]
val = max(dp[j - 1][k - 1] + _sum / (i - j + 1), val)
dp[i][k] = val
return dp[len(A) - 1][K] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
suffix_sums = [0] * (len(A) + 1)
for i in range(len(A) - 1, -1, -1):
suffix_sums[i] = A[i] + suffix_sums[i + 1]
memo = {}
def largest_starting_at(i, new_k):
if i == len(A):
return 0
if new_k == 1:
return suffix_sums[i] / (len(A) - i)
if (i, new_k) in memo:
return memo[i, new_k]
best = 0
for size in range(1, len(A) - i):
best = max(
best,
(suffix_sums[i] - suffix_sums[i + size]) / size
+ largest_starting_at(i + size, new_k - 1),
)
memo[i, new_k] = best
return memo[i, new_k]
return largest_starting_at(0, K) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
L = len(A)
dp = [[(0) for _ in range(K + 1)] for _out in range(L)]
prefix_sum = [(0) for _ in range(L + 1)]
for i in range(1, L + 1):
prefix_sum[i] = prefix_sum[i - 1] + A[i - 1]
dp[0][1] = A[0]
for i in range(1, L):
dp[i][1] = (prefix_sum[i] + A[i]) / (i + 1)
for i in range(1, L):
for k in range(2, K + 1):
if k > i + 1:
dp[i][k] = dp[i][k - 1]
else:
for j in range(-1, i):
subarr = A[j + 1 : i + 1]
ave = (prefix_sum[i + 1] - prefix_sum[j + 1]) / (i - j)
if j == -1:
tmp = 0
else:
tmp = dp[j][k - 1]
if ave + tmp > dp[i][k]:
dp[i][k] = ave + tmp
return dp[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
if not A:
return 0
elif len(A) <= K:
return sum(A)
size = len(A)
dp = [([0] * (size + 1)) for _ in range(K + 1)]
preSum = [sum(A[:i]) for i in range(size + 1)]
for j in range(size):
dp[1][j + 1] = preSum[j + 1] / (j + 1)
for k in range(2, K + 1):
for i in range(k, size + 1):
for j in range(i, k - 1, -1):
dp[k][i] = max(
dp[k][i],
dp[k - 1][j - 1] + (preSum[i] - preSum[j - 1]) / (i - j + 1),
)
print(dp)
return max([dp[i][size] for i in range(1, K + 1)]) | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
dp = [([0] * (K + 1)) for i in range(n)]
total = [A[0]]
for i in range(1, n):
total.append(A[i] + total[i - 1])
def solve(start, k):
if dp[start][k] != 0:
return dp[start][k]
if k == 1:
dp[start][k] = (total[n - 1] - total[start] + A[start]) / (n - start)
return dp[start][k]
i = start
while i + k <= n:
temp = (total[i] - total[start] + A[start]) / (i - start + 1) + solve(
i + 1, k - 1
)
dp[start][k] = max(dp[start][k], temp)
i += 1
return dp[start][k]
return solve(0, K) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
if K >= n:
return sum(A)
pre_sum = [0]
for num in A:
pre_sum.append(pre_sum[-1] + num)
if K <= 1:
return pre_sum[-1] / n
dp = [([0] * (K + 1)) for _ in range(n)]
dp[0][1] = A[0]
for i in range(1, n):
dp[i][1] = pre_sum[i + 1] / (1 + i)
for k in range(2, K + 1):
for i in range(k - 1, n):
for j in range(k - 2, i):
dp[i][k] = max(
dp[i][k],
dp[j][k - 1] + (pre_sum[i + 1] - pre_sum[j + 1]) / (i - j),
)
print(dp)
return dp[n - 1][K] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
if not A:
return 0
n = len(A)
sums = [0] * (n + 1)
dp = [([float("-inf")] * (n + 1)) for _ in range(K + 1)]
for i in range(1, n + 1):
sums[i] = sums[i - 1] + A[i - 1]
dp[1][i] = sums[i] / i
for k in range(2, K + 1):
for i in range(k, n + 1):
for j in range(k - 1, i):
ave_i_j = (sums[i] - sums[j]) / (i - j)
dp[k][i] = max(dp[k][i], dp[k - 1][j] + ave_i_j)
return dp[K][n] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
presum = [0] + list(itertools.accumulate(A))
def mean(i, j):
return (presum[j + 1] - presum[i]) / (j - i + 1)
n = len(A)
old = [[(mean(i, j) if i <= j else 0) for j in range(n)] for i in range(n)]
new = [[(0) for j in range(n)] for i in range(n)]
mval = mean(0, n - 1)
for k in range(2, K + 1):
for i in range(n - k + 1):
max_val = -float("inf")
for mid in range(i, n - k + 1):
max_val = max(max_val, mean(i, mid) + old[mid + 1][n - 1])
new[i][n - 1] = max_val
old, new = new, old
mval = max(mval, old[0][-1])
return mval | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER RETURN VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
if K == 1:
return sum(A) / len(A)
memo = [([0] * len(A)) for _ in range(K)]
cumsum = [0] * len(A)
for i in range(len(A)):
cumsum[i] = cumsum[i - 1] + A[i]
memo[0][i] = cumsum[i] / (i + 1)
for i in range(1, K):
for j in range(i, len(A)):
tmp = 0
for k in range(i - 1, j):
tmp = max(tmp, memo[i - 1][k] + (cumsum[j] - cumsum[k]) / (j - k))
memo[i][j] = tmp
return memo[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], k: int) -> float:
n = len(A)
pre = list(itertools.accumulate([0] + A))
dp = {}
def dfs(i, k):
if (i, k) in dp:
return dp[i, k]
if k == 1:
dp[i, k] = (pre[-1] - pre[i]) / (n - i)
return dp[i, k]
ans = -float("inf")
cur = 0
for j in range(i, n - k + 1):
ans = max(ans, (pre[j + 1] - pre[i]) / (j - i + 1) + dfs(j + 1, k - 1))
dp[i, k] = ans
return ans
return dfs(0, k) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
dp = [([-math.inf] * K) for _ in range(n)]
sv, sc = 0, {(-1): 0}
for i, v in enumerate(A):
sv += v
sc[i] = sv
dp[0][0] = A[0]
for r in range(n):
for i in range(0, r):
for k in range(min(K, r + 1)):
if k == 0:
dp[r][k] = (sc[r] - sc[-1]) / (r + 1)
else:
candidate = dp[i][k - 1] + (sc[r] - sc[i]) / (r - i)
dp[r][k] = max(dp[r][k], candidate)
return dp[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER DICT NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
A = [0] + A
dp = [([0] * (K + 1)) for _ in range(n + 1)]
for i in range(1, n + 1):
dp[i][0] = -float("inf")
for i in range(1, n + 1):
for k in range(1, min(i + 1, K + 1)):
sum_s = 0
for j in range(i, k - 1, -1):
sum_s += A[j]
dp[i][k] = max(dp[i][k], dp[j - 1][k - 1] + sum_s / (i - j + 1))
res = 0
for i in range(1, K + 1):
res = max(res, dp[n][i])
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
def ave(list_):
return sum(list_) / len(list_)
dp = {}
def rec(index, k):
if index == len(A):
return 0
if (index, k) in dp:
return dp[index, k]
if k == 1:
return ave(A[index:])
m = 0
for i in range(index + 1, len(A)):
m = max(m, ave(A[index:i]) + rec(i, k - 1))
dp[index, k] = m
return m
return rec(0, K) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
avg = lambda arr: sum(arr) / len(arr)
n = len(A)
dp = [([0] * K) for _ in range(n)]
for i in range(n):
for k in range(K):
if k == 0:
dp[i][k] = avg(A[: i + 1])
else:
if len(A[: i + 1]) < k + 1:
break
for j in range(i):
dp[i][k] = max(dp[j][k - 1] + avg(A[j + 1 : i + 1]), dp[i][k])
print(dp)
return dp[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
p = [0]
for i in range(n):
p.append(p[i] + A[i])
def average(i, j):
return (p[j] - p[i]) / (j - i)
dp = [([0] * n) for _ in range(K)]
for i in range(n):
dp[0][i] = average(i, n)
for k in range(1, K):
for i in range(n):
for j in range(i + 1, n):
dp[k][i] = max(dp[k][i], average(i, j) + dp[k - 1][j])
return dp[K - 1][0] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR BIN_OP VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
@lru_cache(None)
def helper(i, t):
if i >= len(A):
return 0
if t == K - 1:
return sum(A[i:]) / (len(A) - i)
else:
m = -1
for j in range(1, len(A)):
m = max(m, helper(i + j, t + 1) + sum(A[i : i + j]) / j)
if t == 3:
print(m)
return m
return helper(0, 0) | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | import itertools
class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
dp = [([0] * len(A)) for _ in range(K)]
cur_sum = 0
for j in range(len(A)):
cur_sum += A[j]
dp[0][j] = float(cur_sum) / (j + 1)
for i in range(1, K):
for j in range(len(A)):
cur_sum = 0
for k in range(j, i - 1, -1):
cur_sum += A[k]
dp[i][j] = max(
dp[i][j], dp[i - 1][k - 1] + float(cur_sum) / (j - k + 1)
)
return dp[-1][-1] | IMPORT CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
dp = [([0] * (K + 1)) for _ in range(n + 1)]
for i in range(1, n + 1):
dp[i][1] = sum(A[:i]) / i
for i in range(2, n + 1):
for j in range(2, min(K + 1, i + 1)):
dp[i][j] = max(
[(dp[i - t][j - 1] + sum(A[i - t : i]) / t) for t in range(1, i)]
)
return dp[n][K] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
n = len(A)
dp = [([0] * (K + 1)) for _ in range(n)]
s = 0
for i in range(n):
s += A[i]
dp[i][1] = s * 1.0 / (i + 1)
for k in range(2, K + 1):
for i in range(k - 1, n):
s = 0
for m in range(i, k - 2, -1):
s += A[m]
dp[i][k] = max(dp[i][k], dp[m - 1][k - 1] + s * 1.0 / (i - m + 1))
return dp[-1][-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
N = len(A)
@lru_cache(None)
def dfs(index, remain, acc, size):
if index >= N:
return 0 if size == 0 else acc / size
ans = 0
if remain > 0:
ans = max(
ans,
(acc + A[index]) / (size + 1) + dfs(index + 1, remain - 1, 0, 0),
)
ans = max(ans, dfs(index + 1, remain, acc + A[index], size + 1))
return ans
return dfs(0, K - 1, 0, 0) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR NUMBER NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def part(self, A, i, K, cache):
if i < 0:
if K == 0:
return 0
return float("-Inf")
if i == 0:
if K != 1:
return float("-Inf")
return A[i]
key = "{}-{}".format(i, K)
if key in cache:
return cache[key]
res = float("-Inf")
sm = 0
count = 0
for j in range(i, -1, -1):
sm += A[j]
count += 1
avg = float(sm) / float(count)
res = max(res, avg + self.part(A, j - 1, K - 1, cache))
cache[key] = res
return res
def solve(self, A, K):
return self.part(A, len(A) - 1, K, {})
def largestSumOfAverages(self, A: List[int], K: int) -> float:
return self.solve(A, K) | CLASS_DEF FUNC_DEF IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR STRING IF VAR NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR STRING RETURN VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR DICT FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
N = len(A)
P = [0]
for a in A:
P.append(P[-1] + a)
dp = [0] * (N + 1)
for i in range(K):
dp2 = [0] * (N + 1)
for j in range(i, N + 1):
if i == 0 and j != 0:
dp2[j] = P[j] / j
continue
for k in range(i - 1, j):
dp2[j] = max(dp2[j], dp[k] + (P[j] - P[k]) / (j - k))
dp = dp2
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
dp = {}
def a(n, k):
if n < k:
return 0
if (n, k) in dp:
return dp[n, k]
if k == 1:
dp[n, k] = sum(A[:n]) / float(n)
return dp[n, k]
dp[n, k] = 0
for i in range(n - 1, 0, -1):
a(i, k - 1)
dp[n, k] = max(dp[n, k], a(i, k - 1) + sum(A[i:n]) / float(n - i))
a(len(A), K)
return dp[len(A), K] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], K: int) -> float:
N = len(A)
P = [0] * (N + 1)
for i in range(1, N + 1):
P[i] = P[i - 1] + A[i - 1]
Table = [((P[N] - P[i]) / (N - i)) for i in range(N)]
for k in range(2, K + 1):
for i in range(K - k, N - k + 1):
Table[i] = max(
(P[j] - P[i]) / (j - i) + Table[j] for j in range(i + 1, N - k + 2)
)
return Table[0] | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR NUMBER VAR |
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
Example:
Input:
A = [9,1,2,3,9]
K = 3
Output: 20
Explanation:
The best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned A into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
Note:
1 <= A.length <= 100.
1 <= A[i] <= 10000.
1 <= K <= A.length.
Answers within 10^-6 of the correct answer will be accepted as correct. | class Solution:
def largestSumOfAverages(self, A: List[int], k: int) -> float:
n = len(A)
mtr = [[(0) for i in range(n + 1)] for j in range(k + 1)]
def rec(parts, arrIndex):
if mtr[parts][arrIndex] != 0:
return mtr[parts][arrIndex]
if parts == 1:
mtr[parts][arrIndex] = sum(A[: arrIndex + 1]) / (arrIndex + 1)
return mtr[parts][arrIndex]
for x in range(1, arrIndex + 1):
mtr[parts][arrIndex] = max(
mtr[parts][arrIndex],
rec(parts - 1, x - 1)
+ sum(A[x : arrIndex + 1]) / (arrIndex + 1 - x),
)
return mtr[parts][arrIndex]
rec(k, n - 1)
ans = sys.maxsize * -1
for i in range(1, k + 1):
ans = max(ans, mtr[i][-2])
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.