commit 28ffabcb2a8daab7a39fdf887afc3d42a45c8b21 parent e8a9416671aa1aae8b2bfacc6fc0258a4fc17033 Author: superpozycja <anna@superpozycja.net> Date: Sun, 27 Oct 2024 22:58:28 +0100 solve stick lengths Diffstat:
A | sorting_and_searching/stick_lengths.cpp | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 41 insertions(+), 0 deletions(-)
diff --git a/sorting_and_searching/stick_lengths.cpp b/sorting_and_searching/stick_lengths.cpp @@ -0,0 +1,41 @@ +#include <bits/stdc++.h> + +using namespace std; + +using ui = unsigned int; +using l = long; +using ul = unsigned long; +using ll = long long; +using ull = unsigned long long; + +using vi = vector<int>; +using vui = vector<ui>; +using vl = vector<l>; +using vul = vector<ul>; +using vll = vector<ll>; +using vull = vector<ull>; + +void solve() +{ + int n; + cin >> n; + int p[n]; + for (int i = 0; i < n; i++) { + cin >> p[i]; + } + + sort(p, p+n); + + long res = 0; + for (int i = 0; i < n; i++) { + res += abs(p[i] - p[n/2]); + } + cout << res << "\n"; +} + +int main() +{ + ios::sync_with_stdio(0); + cin.tie(0); + solve(); +}