Files
cses/CSES - CSES Problem Set/Stick_Lengths.cpp
DarianTr 3bd1aab755 unordered_map sometimes better than map:
got TLE with map on Collecting_Numbers_II.cpp but AC with unordered_map
-unordered is good for a lot of requests and a lot of data
2024-06-04 16:41:13 +02:00

25 lines
455 B
C++

// Stick Lengths
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n; cin >> n;
vector<ll> in(n);
for (auto &e: in) cin >> e;
std::sort(in.begin(), in.end());
ll target = in[n/2];
ll target2 = in[n/2 + 1];
ll count = 0, count2 = 0;
for (auto e: in) {
count += abs(target - e);
count2 += abs(target2 - e);
}
cout << min(count, count2) << endl;
return 0;
}