CSES Solutions
#1074Sorting and Searching

Stick Lengths

View on CSES

Solution

1074-Stick_Lengths.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4signed main(){
5  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
6  int n; cin>>n;
7  int a[n]; for(auto&x:a) cin>>x;
8  sort(a,a+n);
9  int x=a[n/2],res=0;
10  for(int i=0;i<n;++i){
11    res+=abs(a[i]-x);
12  }
13  cout<<res<<'\n';
14  return 0;
15}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.