Dcoder icon

Cody's Team @ Dcoder

By Lochard | As a newbie programmer | 10 Aug 2023


Problem: Cody is preparing for a programming competition. He needs k more members for his team to be eligible for the competition. So he organizes a coding test for all his juniors to select the best group. His teacher has instructed him to only select students with consecutive roll numbers. Cody wants to know the total score of the k members selected. Cody is in a hurry and needs your help. You are given the list of marks of N juniors in ascending order of their roll numbers,i.e., the ith number represents the marks of the student with roll no. 'i'. Find k consecutive students with highest total of marks and output the sum of marks of those k students.

Input: The first line contains N, the number of juniors, and k, the number of students needed. 

The second line contains N integers separated by space.

Output: Print the highest total among any k consecutive students.

Constraints: 1 ≤ k ≤ n ≤ 10000

-1000 ≤ marks ≤ 1000

Sample Input: 4 3

2 1 3 4

Sample Output: 

8

n, k = map(int, input().split())
marks = tuple(map(int, input().split()))
highest = 0
for i in range(n - k + 1):
  highest = max(highest, sum(marks[i:i+k]))
print(highest)

How do you rate this article?

1


Lochard
Lochard

Asylum seeker in Lithuania. My short-lifed daily on GitHub https://github.com/locharp/asylum_daily/tree/main/en


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.