Luck Balance

Problem Statement :

Lena is preparing for an important coding competition that is preceded by a number of sequential preliminary contests. Initially, her luck balance is 0. She believes in "saving luck", and wants to check her theory. Each contest is described by two integers, L[ i ] and T[ i ]:

L[ i ] is the amount of luck associated with a contest. If Lena wins the contest, her luck balance will decrease by ; if she loses it, her luck balance will increase by L[ i ].
T[ i ] denotes the contest's importance rating. It's equal to 1 if the contest is important, and it's equal to 0 if it's unimportant.

If Lena loses no more than k important contests, what is the maximum amount of luck she can have after competing in all the preliminary contests? This value may be negative.

Function Description

Complete the luckBalance function in the editor below.

luckBalance has the following parameter(s):

int k: the number of important contests Lena can lose
int contests[n][2]: a 2D array of integers where each contests[ i ] contains two integers that represent the luck balance and importance of the ith contest

int: the maximum luck balance achievable
Input Format

The first line contains two space-separated integers n and k, the number of preliminary contests and the maximum number of important contests Lena can lose.
Each of the next n lines contains two space-separated integers, L[ i ] and T[ i ], the contest's luck balance and its importance rating.

Sample Input

STDIN       Function
-----       --------
6 3         n = 6, k = 3
5 1         contests = [[5, 1], [2, 1], [1, 1], [8, 1], [10, 0], [5, 0]]
2 1
1 1
8 1
10 0
5 0
Sample Output


Solution :


                            Solution in C :

In  C :

 long int sort(int a[] ,int size,int k){
	int i,min,j,sum=0,t; 
return sum;	
int main(){
	int n,k,i,t;
	long int l,sum1=0,sum2;
	int ar[100],arc=0;
	scanf("%d %d",&n,&k);
		scanf("%ld %d",&l,&t);
	//	printf("sum1=%d\n",sum1);
	printf("%ld",(sum1- 2*(sum2)));
	return 0;

                        Solution in C++ :

In  C++ :

using namespace std;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int inf = 1e9 + 5;
const int nax = 1e6 + 5;

int main() {
	int n, k;
	scanf("%d%d", &n, &k);
	int ans = 0;
	vi w;
	REP(i, n) {
		int a, b;
		scanf("%d%d", &a, &b);
		if(b == 0) ans += a;
		else w.pb(a);
	sort(w.rbegin(), w.rend());
	REP(i, (int) w.size()) {
		if(i < k) ans += w[i];
		else ans -= w[i];
	printf("%d\n", ans);
	return 0;

                        Solution in Java :

In  Java :

import java.util.Arrays;
import java.util.Scanner;
public class LuckBalance {

    static class Contest implements Comparable<Contest> {
        int l,t;

        public int compareTo(Contest o) {
            if(t == o.t) {
                return (this.l - o.l);
            }else {
                return -(this.t - o.t);
    public static void main(String[] args) {
        int N, K;
        Scanner scanner = new Scanner(;
        N = scanner.nextInt();
        K = scanner.nextInt();

        int total = 0;
        int t,l;
        Contest[] contests = new Contest[N];
        int importCnt = 0;
        for (int i = 0; i < N; i++) {

            l = scanner.nextInt();
            t = scanner.nextInt();
            total += l;
            if(t == 1) {
            Contest contest = new Contest();
            contest.l = l;
            contest.t = t;

            contests[i] = contest;


        int winCnt = importCnt - K;
        if(winCnt <= 0) {

        for (int i = 0; i < winCnt; i++) {
            total -= 2 * contests[i].l;


                        Solution in Python : 
In Python3 :

N, K = map(int, input().strip().split())

luck = 0
important = []

for i in range(N):
    L, T = list(map(int, input().strip().split()))
    if T == 0:
        luck += L
for i in sorted(important, reverse=True):
    if K > 0:
        luck += i
        K -= 1
        luck -= i


View More Similar Problems

Truck Tour

Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr

View Solution →

Queries with Fixed Length

Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon

View Solution →


This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element

View Solution →

Jesse and Cookies

Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t

View Solution →

Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

View Solution →

Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

View Solution →