Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * author: raghuvanshraj
- * created: 10.04.2020 11:39:49 PM
- **/
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #define MOD 1000000007
- #define SET_ARR(arr,n,val) for (int i = 0; i < n; ++i) arr[i] = val
- #define SET_ARR2D(arr,n,m,val) for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) arr[i][j] = val
- #define INPUT_ARR(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i];
- #define INPUT_ARR2D(arr,n,m) for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> arr[i][j];
- #define PRINT_ARR(arr) for (auto x : arr) cout << x << ' '; cout << endl
- #define PRINT_ARR2D(arr) for (auto x : arr) { for (auto y : x) cout << y << ' '; cout << endl; }
- #define ALL(v) v.begin(), v.end()
- #define RALL(v) v.rbegin(), v.rend()
- using namespace std;
- using namespace __gnu_pbds;
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- template <typename T>
- using indexed_set = tree<T, null_type,less<T>, rb_tree_tag, tree_order_statistics_node_update>;
- template <typename T>
- using max_heap = priority_queue<T>;
- template <typename T>
- using min_heap = priority_queue<T, vector<T>, greater<T>>;
- template <
- typename T,
- typename = typename enable_if<is_arithmetic<T>::value, T>::type,
- typename U,
- typename = typename enable_if<is_arithmetic<U>::value, U>::type
- >
- pair<T,U> operator+(const pair<T,U> &a, const pair<T,U> &b) {
- return {a.first + b.first, a.second + b.second};
- }
- template <
- typename T,
- typename = typename enable_if<is_arithmetic<T>::value, T>::type,
- typename U,
- typename = typename enable_if<is_arithmetic<U>::value, U>::type
- >
- pair<T,U> operator-(const pair<T,U> &a, const pair<T,U> &b) {
- return {a.first - b.first, a.second - b.second};
- }
- template <
- typename T,
- typename = typename enable_if<is_arithmetic<T>::value, T>::type,
- typename U,
- typename = typename enable_if<is_arithmetic<U>::value, U>::type,
- typename V,
- typename = typename enable_if<is_arithmetic<V>::value, V>::type
- >
- pair<T,U> operator*(const V &a, const pair<T,U> &b) {
- return {a * b.first, a * b.second};
- }
- int main(int argc, char const *argv[]) {
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n,k;
- cin >> n >> k;
- vector<int> arr(n);
- INPUT_ARR(arr,n);
- sort(ALL(arr));
- k = min(k, n-k);
- vector<int> mask(n);
- int i,j;
- if (k%2) {
- i = j = n/2;
- } else {
- i = n/2 - 1;
- j = n/2 + 1;
- }
- while (k > 0) {
- if (i == j) {
- k--;
- } else {
- k -= 2;
- }
- mask[i] = mask[j] = 1;
- i -= 2;
- j += 2;
- }
- vector<int> set1, set2;
- for (int i = 0; i < n; ++i) {
- if (mask[i]) {
- set1.push_back(arr[i]);
- } else {
- set2.push_back(arr[i]);
- }
- }
- ll ans = 0;
- for (int x : set1) {
- for (int y : set2) {
- ans += (ll)abs(x-y);
- }
- }
- cout << ans;
- return 0;
- }
Add Comment
Please, Sign In to add comment