We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Functional Programming
  3. Functional Structures
  4. Order exercises

Order exercises

Problem
Submissions
Leaderboard
Discussions

During a math class, a teacher wanted to practice ordering with students. He gave an array of integers, to the students along with following definitions:

  • Subarray is a contiguous segment of array. For example is a subarray, where

  • We say that a sum of a subarray is a sum of elements in this subarray

  • We say that subarray is greater than subarray if and only if:

    • has a greater sum than
    • and has the same sum and begins earlier
    • and has the same sum, they start in the same place and the length of is smaller than the length of

Since the teacher doesn't like number 0, there is no 0 in the array . Other than array , the teacher also gave an integer . The task is to lists as many as possible, but not more than , subarrays with a positive sum in the following order.

  • The first subarray is the greatest subarray in the array according to above definition.

  • The subarray is the greatest subarray disjoint to any of the subarray, where (disjoint means that they have no common elements).

Of course in order to win with others, you have to solve the problem first.

Input
In the first line there are two integers and separated by a single space.
In the second line there are integers separated by single space denoting the array .

Output
Print no more than lines. In the line print the sum of the sequence in the above order.

Constraints


Sample Input 00

5 3
2 4 -10 2 -2

Sample Output 00

6
2

Explanation

Subarray has sum 6 and this is the greatest value in the whole array. Next disjoint greatest subarray is with sum = 2. There are no more subsequences with a positive value disjoint with the first and the second subsequence.

Sample Input 01

4 2
-2 5 -1 -8

Sample Output 01

5    

Explanation

Subarray has sum 5 and this is the greatest value in the whole array. There are no more subsequences with a positive value disjoint with the first one, so even if K = 2, we print out just one value.


Tested by Abhiranjan

Author

pkacprzak

Difficulty

Advanced

Max Score

100

Submitted By

600

Need Help?


View discussions
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website

Join us

Create a HackerRank account

Be part of a 26 million-strong community of developers

Please signup or login in order to view this challenge

or
Already have an account?Log in