第一行 n (1 <= n <= 105), k (0 <= k <= 105) ,表示这堂课持续多少分钟,以及叫醒小易一次使他能够保持清醒的时间。
第二行 n 个数,a1, a2, ... , an(1 <= ai <= 104) 表示小易对每分钟知识点的感兴趣评分。
第三行 n 个数,t1, t2, ... , tn 表示每分钟小易是否清醒, 1表示清醒。
小易这堂课听到的知识点的最大兴趣值。
6 3 1 3 5 2 5 4 1 1 0 1 0 0
16
def getMaxInterest(i,w,count): if len(i) == 0: return 0 m = [] maxNum = 0 countNum = 0 for k in range(count): if w[k] == 0: countNum += i[k] m.append(countNum) for k in range(len(w)): if k + count >= len(w): break if w[k] == 0: countNum -= i[k] if w[k + count] == 0: countNum += i[k + count] m.append(countNum) for y in range(len(w)): if w[y] == 1: maxNum += i[y] return max(m) + maxNum if __name__ == '__main__': a,b = map(int,input().split()) i = list(map(int,input().split())) w = list(map(int,input().split())) num = getMaxInterest(i,w,b) print(num)
n, k = map(int, input().split()) interest_list = list(map(int, input().split())) clear_list = list(map(int, input().split())) interest_point = 0 gets = [] for i in range(n): if clear_list[i] == 1: interest_point += interest_list[i] gets.append(0) else: gets.append(interest_list[i]) get = sum(gets[:k]) max_get = get for i in range(k, n): get += gets[i] - gets[i-k] if get > max_get: max_get = get print(interest_point + max_get)
nk = input().split() n, k = int(nk[0]), int(nk[1]) a = [int(i) for i in input().split()] # 评分 t = [int(i) for i in input().split()] # 是否清醒 def getVal(i, res, flag): if i > n-1: return 0 if t[i] == 1 and (flag == k or flag <= 0): res = res + a[i] + getVal(i+1, res, flag) else: if flag == k: # 可选择是否被叫醒 res = res + max(getVal(i+1, res, flag-1)+a[i], getVal(i+1, res, flag)) elif 0 < flag < k: # 说明被叫醒,一直保持加分 res = res + a[i] + getVal(i+1, res, flag-1) else: # 说明叫醒次数用完 res = res + getVal(i+1, res, flag) return res print(getVal(0, 0, k))
# 请检查是否存在语法错误或者数组越界非法访问等情况 # case通过率为40.00%想求大佬举出反例,说明代码哪里出错了,求指导
""" base_score 为清醒时对兴趣度a的求和 max_score 为叫醒一次,k时间内最大的兴趣度 """ import sys if __name__ == "__main__": # sys.stdin = open('input.txt', 'r') n, k = list(map(int, input().strip().split())) a = list(map(int, input().strip().split())) t = list(map(int, input().strip().split())) base_score = 0 for i in range(n): if t[i]: base_score += a[i] a[i] = 0 max_score = tmp = sum(a[:k]) for i in range(k, n): tmp = tmp + a[i] - a[i - k] max_score = max(max_score, tmp) ans = base_score + max_score print(ans)