SarthakKeshari / CPP-Questions-and-Solutions

This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
MIT License
46 stars 132 forks source link

Maximise the Subsequence Sum #162

Closed Fury-MadMax closed 2 years ago

Fury-MadMax commented 2 years ago

Enter your question -

Chef has an array A containing N integers. The integers of the array can be positive, negative, or even zero.

Chef allows you to choose at most K elements of the array and multiply them by −1.

Find the maximum sum of a subsequence you can obtain if you choose the elements of the subsequence optimally.

Note: A sequence a is a subsequence of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements. For example, [3,1] is a subsequence of [3,2,1] and [4,3,1], but not a subsequence of [1,3,3,7] and [3,10,4]. Note that empty sequence is also a subsequence.

Input Format The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains two space-separated integers N,K. The second line of each test case contains N space-separated integers A1,A2,...,AN

Output Format For each test case, print a single line containing one integer - the maximum sum of a subsequence you can obtain.

Enter link to the question(if question belongs to any online platform) -

https://www.codechef.com/START10C/problems/SIGNFLIP

Tags for the question(eg - Array, Basic, Stack, etc.) -

Greedy, Array and implementation

Fury-MadMax commented 2 years ago

@SarthakKeshari Sir, Please assign me this question for Hacktoberfest 2021. Thank You.

SarthakKeshari commented 2 years ago

@Fury-MadMax, Kindly add your solution to "codechef" folder Deadline - 03/10/2021