Maximise the Subsequence Sum Problem Code: SIGNFLIP Codechef Solution

Maximise the Subsequence Sum Problem Code: SIGNFLIP




Solution In C++


#include<bits/stdc++.h>

#include<cstring>

using namespace std;

#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

 #define Mod 1000000007

#define ll long long

#define vi vector<ll>

#define pb push_back

#define F first

#define S second

#define all(v) (v).begin(),(v).end()

#define pii pair<ll,ll>

#define vii vector<pii>

#define calc_fact(n) tgamma(n+1)

#define inf LONG_LONG_MAX

#define mod 3e5

#define ar array

#define mp make_pair;


ll power(ll x, ll y)

{     


    ll res = 1; 

    while (y)

    { 

        if (y % 2 == 1)

            res = (res * x);


        y = y >> 1;


        x = (x * x);

    }

    return res;

}

int digitCount(ll n)

{

  int count = 0;

  while(n)

  {

    count++;

    n = n/10;

  }

  return count;

}

ll is_prime(ll n) {

  for(ll i=2; i*i <= n; i++)

  {

    if(n%i == 0)

      return false;

  }

  return true;

}


long long int lastDigit(ll n)

{

   

    return (n % 10);

}

void solve() {

  ll n,k;

  cin>>n>>k;

  vector<ll> a(n);

  for (int i = 0; i < n; ++i)

  {

    cin>>a[i];

  }

  sort(a.begin(), a.end());

  for (int i = 0; i < k; ++i)

  {

    if(a[i] < 0)

    {

      a[i]*=-1;

    }

    

  }

  ll sum=0;

  for (int i = 0; i < n; ++i)

  {

    if(a[i]>0)

    {

      sum+=a[i];

    }

  }

  cout<<sum<<endl;

}

int main() 

{

      

   FastIO;


   

  ll t=1;

  cin>>t;

  while(t--)

  {

      solve();

  }

  return 0;

}

Comments

Popular posts from this blog

Bhutan : The Happiest Place On Earth.

Airline Restrictions Solution September Challenge 2021 (Rated for Div 3)

Minimize Digit Sum Solution | Codechef September long challenge 2021

The Old Saint And Three Questions Problem Code: THREEQ

THE TOP 10 YOUTUBERS IN 2020

2-D Point Meeting Solution | Codechef September long challenge 2021

10 things you need to know about SpaceX

THE TOP 10 PAID BLOGGERS IN 2020

Travel Pass | Codechef September long challenge 2021 | Travel Pass Solution