#include "bits/stdc++.h" #define _CRT_SECURE_NO_WARNINGS #define rep(i,n) for(int i = 0;i < n;i++) #define REP(i,n,k) for(int i = n;i < k;i++) #define P(p) cout << (p) << endl; #define sP(p) cout << setprecision(15) << fixed << p << endl; #define vi vector #define mp(a,b) make_pair(a,b) using namespace std;typedef long long ll;typedef unsigned long long ull;int dx[] = { 1, -1 , 0 , 0};int dy[] = { 0, 0, 1, -1}; void solve() { int b,n; cin >> b >> n; vi c(n); rep(i,n)cin >> c[i]; ll left = 0,right = 1e15; while(right-left>2){ ll l = (2*left + right)/3; ll r = (left + 2*right)/3; ll sl = 0,sr = 0,sbl = 0,sbr = 0; rep(i,n){ sl += l-c[i]; sr += r-c[i]; sbl += abs(l-c[i]); sbr += abs(r-c[i]); } if(sl > b){ right = l; }else if(sr > b){ right = r; }else if(sbl >= sbr){ left = l; }else{ right = r; } } ll sum = 0; rep(i,n){ sum += abs((left+right)/2 - c[i]); } P(sum); } int main() { solve(); return 0; }