Problem:
Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
#define mod 1000000007
#define mod2 998244353 
#define PI 3.14159265
#define ll long long
#define ld long double
#define pi pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define sz size()
#define setbits(x)  __builtin_popcount(x)
#define fi first
#define se second
#define lz length()
#define all(x) (x).begin(),(x).end()
#define scf(x) scanf("%lld",&x)
#define rep(i,n) for(ll i=0;i<n;i++)
const int INF = (int)1e9;
const ll IINF=1LL<<62;
const int maxn=10000005;

//int divr[maxn];
/**/
/*void divisor()
{for(int i=2;i<sqrt(maxn);i++)
     {
          if(!divr[i])
          {
               for(int j=i*i;j<maxn;j+=i)if(!divr[j])divr[j]=i;     
          }
     }
     for(int i=1;i<maxn;i++)if(!divr[i])divr[i]=i;
}*/
 
/*const int N=1000005;
ll n;
struct line {
    long long m, c;
    long long eval(long long x) { return m * x + c; }
    long double intersectX(line l) { return (long double) (c - l.c) / (l.m - m); }
};
vector<pair<pi,ll>> rect;
deque<line> dq;
*/
/*ll modexpo(ll a,ll b)
{
     ll ans=1LL;
     while(b>0)
     {
          if(b&1)ans=(ans*a)%mod;
          a=(a*a)%mod;
          b>>=1;
     }
     return ans;
}
*/
int main(){
     int n;
     cin>>n;
     int a[n];
     rep(i,n)cin>>a[i];
     if(n==1){cout<<a[0];return 0;}
     sort(a,a+n);
     vector<pair<int,int>> vec;
     int ac=0;
     if(a[0]>=0){
          ac=a[0];
          for(int i=1;i<n-1;i++){
               vec.pb({ac,a[i]});
               ac-=a[i];
          }
          vec.pb({a[n-1],ac});
     }
     else if(a[n-1]<=0){
          ac=a[n-1];
          for(int i=n-2;i>=0;i--){
               vec.pb({ac,a[i]});
               ac-=a[i];

          }
     }
     else{
          ac=a[0];
          for(int i=1;i<n-1;i++){
               if(a[i]>=0){
                    vec.pb({ac,a[i]});
                    ac-=a[i];
               }
          }
          vec.pb({a[n-1],ac});ac=a[n-1]-ac;
          for(int i=1;i<n-1;i++){
               if(a[i]<0){
                    vec.pb({ac,a[i]});
                    ac=ac-a[i];
               }
          }
     }
     cout<<ac<<endl;
     for(auto it:vec)cout<<it.fi<<" "<<it.se<<"\n";


}