#K10376. Maximum Sum of Consecutive Digits
Maximum Sum of Consecutive Digits
Maximum Sum of Consecutive Digits
You are given a string s
consisting of digits and a positive integer k. Your task is to compute the maximum sum obtainable by summing exactly k consecutive digits in the string.
If k is greater than the length of s
, the output should be 0.
The problem can be formulated as follows:
Given a string \( s \) and a number \( k \), find \[ \max_{0 \leq i \leq |s|-k} \sum_{j=i}^{i+k-1} s_j \] where \(s_j\) denotes the digit at position \(j\) in the string. If \(k > |s|\), output 0.
Input is read from standard input and output is printed to standard output.
inputFormat
The input is provided via standard input in the following format:
- The first line contains a non-empty string
s
consisting only of digits. - The second line contains an integer k which represents the number of consecutive digits to sum.
outputFormat
Output a single integer representing the maximum sum of any k consecutive digits in the string s
. If k is greater than the length of s
, output 0.
123456789
3
24