Stick Lengths Solution CSES with Explanation | CSES Problem Set

Stick Lengths

There are n sticks with some lengths. Your task is to modify the sticks so that each stick has the same length.

You can either lengthen and shorten each stick. Both operations cost x where x is the difference between the new and original length.

What is the minimum total cost?

Input

The first input line contains an integer n: the number of sticks.

Then there are n integers: p1,p2,,pn: the lengths of the sticks.

Output

Print one integer: the minimum total cost.

Constraints
  • 1n2105
  • 1pi109
Example

Input:
5
2 3 1 5 2


Output:
5

#include <bits/stdc++.h>

using namespace std;

#define int            long long int
#define F              first
#define S              second
#define pb             push_back
#define si             set <int>
#define vi             vector <int>
#define pii            pair <int, int>
#define vpi            vector <pii>
#define vpp            vector <pair<int, pii>>
#define mii            map <int, int>
#define mpi            map <pii, int>
#define spi            set <pii>
#define endl           "\n"
#define sz(x)          ((int) x.size())
#define all(p)         p.begin(), p.end()
#define double         long double
#define que_max        priority_queue <int>
#define que_min        priority_queue <int, vi, greater<int>>
#define bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)
#define print(a)       for(auto x : a) cout << x << " "; cout << endl
#define print1(a)      for(auto x : a) cout << x.F << " " << x.S << endl
#define print2(a,x,y)  for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl

inline int power(int a, int b)
{
  int x = 1;
  while (b)
  {
    if (b & 1) x *= a;
    a *= a;
    b >>= 1;
  }
  return x;
}

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
  const char* comma = strchr (names + 1, ',');
  cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

const int N = 200005;

int helper(vector<int>&v,int n){
    int tot=0;
    for (int i = 0; i < v.size(); i++)
    {
        tot+=abs(v[i]-n);
    }
    return tot;
    
}

void solve() {


// n is the size of the array
int n;
cin>>n;
// vector v to store the elements 
vi v(n);
// taking the inputs
for (int i = 0; i < n; i++)
{
    cin>>v[i];
}
// sorting the elements
sort(v.begin(),v.end());
// now we will find the median of the array and will find our answer
// if odd then the median is the middle element
if(n%2==1){
    cout<<helper(v,v[n/2]);
}
// else we will calculate our answer for both the middle elements
else{
    cout<<min(helper(v,v[n/2]),helper(v,v[(n/2)+1]));
}






}

int32_t main()
{
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);


  clock_t z = clock();

  int t = 1;
//   cin >> t; 
  while (t--) solve();



  return 0;
}

 

Post a Comment

0 Comments