#include<stdio.h>
#define gx getchar
#define ll long
#define max1 10001
ll n;
typedef struct data
{
    ll val,idx;
} data;
inline long inp()
{
    ll x=0;
    char c,sign=1;
    while(((c=gx())<'0'||c>'9')&&c!='-');
    if(c=='-')  sign=-1;
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+c-48,c=gx();
    return x*sign;
}
void swap(data *a,data *b)
{
    ll c=a->val;
    a->val=b->val;
    b->val=c;
    c=a->idx;
        a->idx=b->idx;
        b->idx=c;
}
void qsort(data *a,ll l,ll h)
{
    if(l>=h)
    return ;
    ll x=a[h].val,i,j,k;
    for(i=j=l;i<h;i++){
        if(a[i].val<=x){
            swap(a+i,a+j);
            j++;
        }
    }
    swap(a+j,a+h);
        qsort(a,l,j-1);
    qsort(a,j+1,h);
}
int main()
{
    ll i,j=0,k,sum=0,b[max1][2],c;
    n=inp(),k=inp();
    data a[101];
    for(i=0;i<n;i++){
        a[i].val=inp();
        a[i].idx=i+1;
        sum+=a[i].val;
    }
    c=sum%n;
    sum/=n;
    qsort(a,0,n-1);
    while(j<k){
        if(!c){
            i=0;
            while(abs(a[i].val-sum)==0) i++;
            if(i==n)    break;
        }
        else{
            i=0;
            while(abs(a[i].val-sum)<=1) i++;
            if(i==n)    break;
        }
        b[j][0]=a[0].idx,b[j][1]=a[n-1].idx;
        a[0].val++;
        a[n-1].val--;
        j++;
        qsort(a,0,n-1);
    }
    printf("%ld %ld\n",a[n-1].val-a[0].val,j);
    for(i=0;i<j;i++)
    printf("%ld %ld\n",b[i][1],b[i][0]);
    return 0;
}


#include<stdio.h>
#define gx getchar_unlocked
#define ll long long
#define max1 10001
ll n;
typedef struct data
{
    ll val,idx;
} data;
ll inp()
{
    ll x=0;
    char c,sign=1;
    while(((c=gx())<'0'||c>'9')&&c!='-');
    if(c=='-')  sign=-1;
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+c-48,c=gx();
    return x*sign;
}
void swap(data *a,data *b)
{
    ll c=a->val;
    a->val=b->val;
    b->val=c;
    c=a->idx;
        a->idx=b->idx;
        b->idx=c;
}
void qsort(data *a,ll l,ll h)
{
    if(l>=h)
    return ;
    ll x=a[h].val,i,j,k;
    for(i=j=l;i<h;i++){
        if(a[i].val<=x){
            swap(a+i,a+j);
            j++;
        }
    }
    swap(a+j,a+h);
        qsort(a,l,j-1);
    qsort(a,j+1,h);
}
int main()
{
    ll i,j=0,k,sum=0,b[max1][2],c,x;
    n=inp(),k=inp();
    data a[101];
    for(i=0;i<n;i++){
        a[i].val=inp();
        a[i].idx=i+1;
        sum+=a[i].val;
    }
    c=sum%n;
    sum/=n;

    qsort(a,0,n-1);
    while(j<k){
        if(!c){
            i=0;
            while(a[i].val-sum==0)  i++;
            if(i==n)    break;
        }
        else{
            i=0;
            while((x=sum-a[i].val)>=0&&x<=1)    i++;
            if(i==n)    break;
        }
        b[j][0]=a[0].idx,b[j][1]=a[n-1].idx;
        a[0].val++;
        a[n-1].val--;
        j++;
        qsort(a,0,n-1);
    }
    printf("%lld %lld\n",a[n-1].val-a[0].val,j);
    for(i=0;i<j;i++)
    printf("%lld %lld\n",b[i][1],b[i][0]);
    return 0;
}


#include<stdio.h>
#define gx getchar
#define ll long long
#define max1 10001
ll n;
typedef struct data
{
    ll val,idx;
} data;
inline ll inp()
{
    ll x=0;
    char c,sign=1;
    while(((c=gx())<'0'||c>'9')&&c!='-');
    if(c=='-')  sign=-1;
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+c-48,c=gx();
    return x*sign;
}
void swap(data *a,data *b)
{
    ll c=a->val;
    a->val=b->val;
    b->val=c;
    c=a->idx;
        a->idx=b->idx;
        b->idx=c;
}
void qsort(data *a,ll l,ll h)
{
    if(l>=h)
    return ;
    ll x=a[h].val,i,j,k;
    for(i=j=l;i<h;i++){
        if(a[i].val<=x){
            swap(a+i,a+j);
            j++;
        }
    }
    swap(a+j,a+h);
        qsort(a,l,j-1);
    qsort(a,j+1,h);
}
int main()
{
    ll i,j=0,k,sum=0,b[max1][2],c;
    n=inp(),k=inp();
    data a[101];
    for(i=0;i<n;i++){
        a[i].val=inp();
        a[i].idx=i+1;
        sum+=a[i].val;
    }
    c=sum%n;
    sum/=n;
    qsort(a,0,n-1);
    while(j<k){
        if(!c){
            i=0;
            while(abs(a[i].val-sum)==0) i++;
            if(i==n)    break;
        }
        else{
            i=0;
            while(abs(a[i].val-sum)<=1) i++;
            if(i==n)    break;
        }
        b[j][0]=a[0].idx,b[j][1]=a[n-1].idx;
        a[0].val++;
        a[n-1].val--;
        j++;
        qsort(a,0,n-1);
    }
    printf("%lld %lld\n",a[n-1].val-a[0].val,j);
    for(i=0;i<j;i++)
    printf("%lld %lld\n",b[i][1],b[i][0]);
    return 0;
}


#include<stdio.h>
#define gx getchar
#define ll long
#define max1 10001
ll n;
typedef struct data
{
    ll val,idx;
} data;
long inp()
{
    ll x=0;
    char c,sign=1;
    while(((c=gx())<'0'||c>'9')&&c!='-');
    if(c=='-')  sign=-1;
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+c-48,c=gx();
    return x*sign;
}
void swap(data *a,data *b)
{
    ll c=a->val;
    a->val=b->val;
    b->val=c;
    c=a->idx;
        a->idx=b->idx;
        b->idx=c;
}
void qsort(data *a,ll l,ll h)
{
    if(l>=h)
    return ;
    ll x=a[h].val,i,j,k;
    for(i=j=l;i<h;i++){
        if(a[i].val<=x){
            swap(a+i,a+j);
            j++;
        }
    }
    swap(a+j,a+h);
        qsort(a,l,j-1);
    qsort(a,j+1,h);
}
int main()
{
    ll i,j=0,k,sum=0,b[max1][2],c;
    n=inp(),k=inp();
    data a[101];
    for(i=0;i<n;i++){
        a[i].val=inp();
        a[i].idx=i+1;
        sum+=a[i].val;
    }
    c=sum%n;
    sum/=n;
    qsort(a,0,n-1);
    while(j<k){
        if(!c){
            i=0;
            while(abs(a[i].val-sum)==0) i++;
            if(i==n)    break;
        }
        else{
            i=0;
            while(abs(a[i].val-sum)<=1) i++;
            if(i==n)    break;
        }
        b[j][0]=a[0].idx,b[j][1]=a[n-1].idx;
        a[0].val++;
        a[n-1].val--;
        j++;
        qsort(a,0,n-1);
    }
    printf("%ld %ld\n",a[n-1].val-a[0].val,j);
    for(i=0;i<j;i++)
    printf("%ld %ld\n",b[i][1],b[i][0]);
    return 0;
}


#include<stdio.h>
#define gx getchar//_unlocked
#define px putchar//_unlocked
#define ll long long
#define max1 10001
ll n;
typedef struct data
{
    ll val,idx;
} data;
inline ll inp()
{
    ll x=0;
    char c,sign=1;
    while(((c=gx())<'0'||c>'9')&&c!='-');
    if(c=='-')  sign=-1;
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+c-48,c=gx();
    return x*sign;
}
void swap(data *a,data *b)
{
    ll c=a->val;
    a->val=b->val;
    b->val=c;
    c=a->idx;
        a->idx=b->idx;
        b->idx=c;
}
void qsort(data *a,ll l,ll h)
{
    if(l>=h)
    return ;
    ll x=a[h].val,i,j,k;
    for(i=j=l;i<h;i++){
        if(a[i].val<=x){
            swap(a+i,a+j);
            j++;
        }
    }
    swap(a+j,a+h);
        qsort(a,l,j-1);
    qsort(a,j+1,h);
}
int main()
{
    ll i,j=0,k,sum=0,b[max1][2],c;
    n=inp(),k=inp();
    data a[101];
    for(i=0;i<n;i++){
        a[i].val=inp();
        a[i].idx=i+1;
        sum+=a[i].val;
    }
    c=sum%n;
    sum/=n;
    qsort(a,0,n-1);
    while(j<k){
        if(!c){
            i=0;
            while(abs(a[i].val-sum)==0) i++;
            if(i==n)    break;
        }
        else{
            i=0;
            while(abs(a[i].val-sum)<=1) i++;
            if(i==n)    break;
        }
        b[j][0]=a[0].idx,b[j][1]=a[n-1].idx;
        a[0].val++;
        a[n-1].val--;
        j++;
        qsort(a,0,n-1);
    }
    printf("%lld %lld\n",a[n-1].val-a[0].val,j);
    for(i=0;i<j;i++)
    printf("%lld %lld\n",b[i][1],b[i][0]);
    return 0;
}


#include<stdio.h>
#define gx getchar
#define ll long long
#define max1 10001
ll n;
typedef struct data
{
    ll val,idx;
} data;
ll inp()
{
    ll x=0;
    char c,sign=1;
    while(((c=gx())<'0'||c>'9')&&c!='-');
    if(c=='-')  sign=-1;
    while(c>='0'&&c<='9')
    x=(x<<1)+(x<<3)+c-48,c=gx();
    return x*sign;
}
void swap(data *a,data *b)
{
    ll c=a->val;
    a->val=b->val;
    b->val=c;
    c=a->idx;
        a->idx=b->idx;
        b->idx=c;
}
void qsort(data *a,ll l,ll h)
{
    if(l>=h)
    return ;
    ll x=a[h].val,i,j,k;
    for(i=j=l;i<h;i++){
        if(a[i].val<=x){
            swap(a+i,a+j);
            j++;
        }
    }
    swap(a+j,a+h);
        qsort(a,l,j-1);
    qsort(a,j+1,h);
}
int main()
{
    ll i,j=0,k,sum=0,b[max1][2],c,x;
    n=inp(),k=inp();
    data a[101];
    for(i=0;i<n;i++){
        a[i].val=inp();
        a[i].idx=i+1;
        sum+=a[i].val;
    }
    c=sum%n;
    sum/=n;

    qsort(a,0,n-1);
    while(j<k){
        if(!c){
            i=0;
            while(a[i].val-sum==0)  i++;
            if(i==n)    break;
        }
        else{
            i=0;
            while((x=sum-a[i].val)>=0&&x<=1)    i++;
            if(i==n)    break;
        }
        b[j][0]=a[0].idx,b[j][1]=a[n-1].idx;
        a[0].val++;
        a[n-1].val--;
        j++;
        qsort(a,0,n-1);
    }
    printf("%lld %lld\n",a[n-1].val-a[0].val,j);
    for(i=0;i<j;i++)
    printf("%lld %lld\n",b[i][1],b[i][0]);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

typedef struct pomeraj{
    int x;
    int y;
}Pomeraj;

int poz_min(int a[],int n)
{
    int i,pmin=1;
    for (i=2;i<=n;i++)
        if (a[i]<a[pmin])
          pmin=i;
    return pmin;
}

int poz_max(int a[],int n)
{
    int i,pmax=1;
    for (i=2;i<=n;i++)
        if (a[i]>a[pmax])
          pmax=i;
    return pmax;
}

Pomeraj pom[1001];
int a[101];

int main()
{
    int n,k,i;
    scanf("%d%d",&n,&k);
    for (i=1;i<=n;i++)
        scanf("%d",&a[i]);
    int pmin=poz_min(a,n);
    int pmax=poz_max(a,n);
    int p_min=0,raz=0,j;
    int min_raz=a[pmax]-a[pmin];
    pom[0].x=pmax;
    pom[0].y=pmin;
    a[pmax]--;
    a[pmin]++;
    i=0;
    while(i+1<=k)
    {
       // for (j=1;j<=n;j++)
         //  printf("%d ",a[j]);
        pmin=poz_min(a,n);
        pmax=poz_max(a,n);
        if (a[pmax]<=0)
            break;
        pom[i+1].x=pmax;
        pom[i+1].y=pmin;
        raz=abs(a[pmax]-a[pmin]);
       // printf("RAZLIKA JE %d  ",raz);

        if (raz<min_raz)
        {
            min_raz=raz;
            p_min=i+1;
        }

        a[pmax]--;
        a[pmin]++;

      /*  printf("\npoz max je %d poz min je %d\n",pmax,pmin);
        printf("razlika je:%d\n",raz);
        printf("min razlika je%d\n",min_raz);
        printf("do sada je proslo %d poteza\n",i);
        printf("pmin je %d\n\n",p_min);*/
        i++;
    }
    printf("%d %d\n",min_raz,p_min);
    for (i=0;i<p_min;i++)
        printf("%d %d\n",pom[i].x,pom[i].y);
    return 0;
}


#include <stdio.h>
#define MAX(a,b) (a>b?a:b)
int uk_nach=0;
int uk_kon=0;
int max(int max[],int len)
{
	int i,n,imax;
	imax=max[0];
	uk_nach=0;
    for(i=0;i<len;i++)
	{
		if(max[i]>imax)
		{
			imax=max[i];
			uk_nach=i;
		}
	}
	return imax;
}
int min(int max[],int len)
{
	int i,n,imin;
	imin=max[0];
	uk_kon=0;
    for(i=0;i<len;i++)
	{
		if(max[i]<imin)
		{
			imin=max[i];
			uk_kon=i;
		}
	}
	return imin;
}
int main()
{
	int n,k,i=0,j=0;
	int mas[101],result[1001][2],iMax;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
	{
		scanf("%d",&mas[i]);
	}
	if(max(mas,n)==min(mas,n))
	{
		printf("0 0\n");
		return 0;
	}
	j=0;
	for(i=0;i<k;i++)
	{
		if(max(mas,n)-min(mas,n)>1)
		{
			mas[uk_nach]--;
			mas[uk_kon]++;
			result[j][0]=uk_nach+1;
			result[j][1]=uk_kon+1;
			j++;
		}
	}
	printf("%d %d\n",(max(mas,n)-min(mas,n)),j);
	for(i=0;i<j;i++)
	{
		printf("%d %d\n",result[i][0],result[i][1]);
	}
	return 0;
}


#include <stdio.h>

#define N 100
#define K	1000

int main() {
	static int aa[N], ii[K], jj[K];
	int n, k, i, j, max, min, imax, imin;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 0; j < k; j++) {
		max = 0;
		for (i = 0; i < n; i++)
			if (max < aa[i])
				max = aa[i], imax = i;
		min = 10001;
		for (i = 0; i < n; i++)
			if (min > aa[i] && i != imax)
				min = aa[i], imin = i;
		ii[j] = imax;
		jj[j] = imin;
		aa[imax]--, aa[imin]++;
	}
	max = 0;
	for (i = 0; i < n; i++)
		if (max < aa[i])
			max = aa[i], imax = i;
	min = 10001;
	for (i = 0; i < n; i++)
		if (min > aa[i])
			min = aa[i], imin = i;
	printf("%d %d\n", max - min, k);
	for (j = 0; j < k; j++)
		printf("%d %d\n", ii[j] + 1, jj[j] + 1);
	return 0;
}


#include <stdio.h>

#define N 100
#define K	1000

int main() {
	static int aa[N], ii[K], jj[K];
	int n, k, i, j, max, min, imax, imin;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 0; j < k; j++) {
		max = 0;
		for (i = 0; i < n; i++)
			if (max < aa[i])
				max = aa[i], imax = i;
		min = 10001;
		for (i = 0; i < n; i++)
			if (min > aa[i])
				min = aa[i], imin = i;
		ii[j] = imax;
		jj[j] = imin;
		aa[imax]--, aa[imin]++;
	}
	max = 0;
	for (i = 0; i < n; i++)
		if (max < aa[i])
			max = aa[i], imax = i;
	min = 10001;
	for (i = 0; i < n; i++)
		if (min > aa[i])
			min = aa[i], imin = i;
	printf("%d %d\n", max - min, k);
	for (j = 0; j < k; j++)
		printf("%d %d\n", ii[j] + 1, jj[j] + 1);
	return 0;
}


#include <stdio.h>

#define N 100
#define K	1000

int main() {
	static int aa[N], ii[K], jj[K];
	int n, k, i, j, max, min, imax, imin;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 0; j < k; j++) {
		max = 0;
		for (i = 0; i < n; i++)
			if (max < aa[i])
				max = aa[i], imax = i;
		min = 10001;
		for (i = 0; i < n; i++)
			if (min > aa[i])
				min = aa[i], imin = i;
		if (max - min <= 1)
			break;
		ii[j] = imax;
		jj[j] = imin;
		aa[imax]--, aa[imin]++;
	}
	max = 0;
	for (i = 0; i < n; i++)
		if (max < aa[i])
			max = aa[i];
	min = 10001;
	for (i = 0; i < n; i++)
		if (min > aa[i])
			min = aa[i];
	k = j;
	printf("%d %d\n", max - min, k);
	for (j = 0; j < k; j++)
		printf("%d %d\n", ii[j] + 1, jj[j] + 1);
	return 0;
}


#include <stdio.h>

#define N 100
#define K	1000

int main() {
	static int aa[N], ii[K], jj[K];
	int n, k, i, j, max, min, imax, imin;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	for (j = 0; j < k; j++) {
		max = 0;
		for (i = 0; i < n; i++)
			if (max < aa[i])
				max = aa[i], imax = i;
		min = 10001;
		for (i = 0; i < n; i++)
			if (min > aa[i])
				min = aa[i], imin = i;
		if (max - min <= 1)
			break;
		ii[j] = imax;
		jj[j] = imin;
		aa[imax]--, aa[imin]++;
	}
	k = j;
	printf("%d %d\n", max - min, k);
	for (j = 0; j < k; j++)
		printf("%d %d\n", ii[j] + 1, jj[j] + 1);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

/**************
B. Towers

As you know, all the kids in Berland love playing with cubes. Little Petya
has n towers consisting of cubes of the same size. Tower with number i
consists of ai cubes stacked one on top of the other. Petya defines the
instability of a set of towers as a value equal to the difference between the
heights of the highest and the lowest of the towers. For example, if Petya
built five cube towers with heights (8, 3, 2, 6, 3), the instability of this
set is equal to 6 (the highest tower has height 8, the lowest one has
height 2).

The boy wants the instability of his set of towers to be as low as possible.
All he can do is to perform the following operation several times: take
the top cube from some tower and put it on top of some other tower of
his set. Please note that Petya would never put the cube on the same
tower from which it was removed because he thinks it's a waste of time.

Before going to school, the boy will have time to perform no more than
k such operations. Petya does not want to be late for class, so you
have to help him accomplish this task.

Input
The first line contains two space-separated positive integers
n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 1000) — the number of towers in the
given set and the maximum number of operations Petya can perform.
The second line contains n space-separated positive integers a_i
(1 ≤ a_i ≤ 104) — the towers' initial heights.

Output
In the first line print two space-separated non-negative integers
s and m (m ≤ k). The first number is the value of the minimum possible
instability that can be obtained after performing at most k operations,
the second number is the number of operations needed for that.

In the next m lines print the description of each operation as two
positive integers i and j, each of them lies within limits from 1 to n.
They represent that Petya took the top cube from the i-th tower and put
in on the j-th one (i ≠ j). Note that in the process of performing
operations the heights of some towers can become equal to zero.

If there are multiple correct sequences at which the minimum possible
instability is achieved, you are allowed to print any of them.
**************/

// IDEA: reduce instability by shifting blocks from highest
// to lowest tower

typedef struct TOWER
{
  unsigned short NumberOfBlocks;
  unsigned short Id;
} TOWER;

typedef struct SET_OF_TOWERS
{
  struct TOWER *Towers;
  unsigned short NumberOfTowers;
  unsigned short Instability;
  unsigned short NumberOfAvailableMoves;
  unsigned short NumberOfMovesNecessaryToDecreaseInstability;
  unsigned short NumberOfPerformedMoves;
  char * StringOfPerformedMoves;
  unsigned short LengthOfStringOfPerformedMoves;
} SET_OF_TOWERS;

void init_tower(struct TOWER *tower,
                unsigned short NumberOfBlocks,
                unsigned short Id)
{
  tower->NumberOfBlocks = NumberOfBlocks;
  tower->Id = Id;
  return;
}

int TOWER_compare(const void * FirstTower, const void * SecondTower)
{
  int FirstNumberOfBlocks = ((TOWER*) FirstTower)->NumberOfBlocks;
  int SecondNumberOfBlocks = ((TOWER*) SecondTower)->NumberOfBlocks;

  if ( FirstNumberOfBlocks < SecondNumberOfBlocks )
    return - 1;
  if ( FirstNumberOfBlocks > SecondNumberOfBlocks )
    return 1;
  // case (FirstNumberOfBlocks == SecondNumberOfBlocks )
  return 0;
}

unsigned short local_instability_at_left_position_of_set_of_towers(SET_OF_TOWERS *MyTowers, unsigned short LeftPosition)
{
  unsigned short LocalInstability;

  LocalInstability =
  MyTowers->Towers[MyTowers->NumberOfTowers-1-LeftPosition].NumberOfBlocks - \
  MyTowers->Towers[LeftPosition].NumberOfBlocks;

  return LocalInstability;
}

void determine_necessary_number_of_moves_to_decrease_instability_of_set_of_towers( SET_OF_TOWERS* MyTowers)
{
  unsigned short Counter;
  for(Counter = 0;
      MyTowers->Instability ==  local_instability_at_left_position_of_set_of_towers(MyTowers, Counter);
      Counter++);
  MyTowers->NumberOfMovesNecessaryToDecreaseInstability = Counter;
  return;
}

SET_OF_TOWERS init_set_of_towers()
{
  SET_OF_TOWERS MyTowers;
  unsigned short Counter, NumberOfBlocks;

  scanf(
        "%hu %hu",
        &(MyTowers.NumberOfTowers),
        &(MyTowers.NumberOfAvailableMoves)
       );

  if ( ! ( MyTowers.Towers = malloc( MyTowers.NumberOfTowers * sizeof(*(MyTowers.Towers)) ) ) )
  {
    printf("malloc failed for Towers!");
    MyTowers.NumberOfTowers = 0;
  }

  if ( ! ( MyTowers.StringOfPerformedMoves = malloc( (3 + 1 + 3 + 1) * MyTowers.NumberOfAvailableMoves * sizeof(*MyTowers.StringOfPerformedMoves) ) ) )
  {
    printf("malloc failed for StringOfPerformedMoves!");
  }

  MyTowers.StringOfPerformedMoves[0] = '\0';
  MyTowers.LengthOfStringOfPerformedMoves = 0;

  for(Counter = 0; Counter < MyTowers.NumberOfTowers; Counter++)
  {
    scanf( "%hu",&NumberOfBlocks);
    init_tower(MyTowers.Towers + Counter,NumberOfBlocks,Counter+1);
  }

  qsort(MyTowers.Towers,
        MyTowers.NumberOfTowers,
        sizeof(*(MyTowers.Towers)),
        TOWER_compare);

  MyTowers.Instability = local_instability_at_left_position_of_set_of_towers(&MyTowers,0);

  determine_necessary_number_of_moves_to_decrease_instability_of_set_of_towers(&MyTowers);

  MyTowers.NumberOfPerformedMoves = 0;

  return MyTowers;
}

void shift_block_from_right_pos_to_left_pos_using_a_move_and_recording
 (SET_OF_TOWERS* MyTowers,
  unsigned short LeftPosition,
  unsigned short RightPosition)
{
  MyTowers->Towers[LeftPosition].NumberOfBlocks++;
  MyTowers->Towers[RightPosition].NumberOfBlocks--;
  MyTowers->NumberOfAvailableMoves--;
  MyTowers->NumberOfPerformedMoves++;
  MyTowers->LengthOfStringOfPerformedMoves +=
    sprintf(
      MyTowers->StringOfPerformedMoves +
      MyTowers->LengthOfStringOfPerformedMoves,
      "%hu %hu\n",
      MyTowers->Towers[RightPosition].Id,
      MyTowers->Towers[LeftPosition].Id);
  return;
}

unsigned short rightmost_index_with_maximal_value_in_set_of_towers
  (SET_OF_TOWERS* MyTowers)
{
  unsigned short RightIndex = MyTowers->NumberOfTowers - 1;
  while (MyTowers->Towers[MyTowers->NumberOfTowers - 1].NumberOfBlocks ==
    MyTowers->Towers[RightIndex - 1].NumberOfBlocks)
  {
    RightIndex--;
  }
  return RightIndex;
}

unsigned short leftmost_index_with_minimal_value_in_set_of_towers
  (SET_OF_TOWERS* MyTowers)
{
  unsigned short LeftIndex = 0;
  while (MyTowers->Towers[0].NumberOfBlocks ==
    MyTowers->Towers[LeftIndex + 1].NumberOfBlocks)
  {
    LeftIndex++;
  }
  return LeftIndex;
}

void decrease_instability_and_record_move_for_set_of_towers
  (SET_OF_TOWERS* MyTowers)
{
  unsigned short CounterLeft;

  for(CounterLeft = 0;
      CounterLeft < MyTowers->NumberOfMovesNecessaryToDecreaseInstability;
      CounterLeft++)
  {
    shift_block_from_right_pos_to_left_pos_using_a_move_and_recording(
      MyTowers,
      leftmost_index_with_minimal_value_in_set_of_towers(MyTowers),
      rightmost_index_with_maximal_value_in_set_of_towers(MyTowers));
  }

  MyTowers->Instability = local_instability_at_left_position_of_set_of_towers(
    MyTowers,0);
  determine_necessary_number_of_moves_to_decrease_instability_of_set_of_towers(MyTowers);

  return;
}

void free_set_of_towers(SET_OF_TOWERS* MyTowers)
{
  free(MyTowers->Towers);
  free(MyTowers->StringOfPerformedMoves);
  return;
}

void print_set_of_towers(SET_OF_TOWERS* MyTowers)
{
  unsigned short Counter;

  printf("# of Moves available: %hu\n",
          MyTowers->NumberOfAvailableMoves);
  printf("Instability: %hu\n",
          MyTowers->Instability);
  printf("Necessary # of Moves to decrease Instability: %hu\n",
         MyTowers->NumberOfMovesNecessaryToDecreaseInstability);

  for(Counter = 0; Counter < MyTowers->NumberOfTowers; Counter ++)
  {
    printf("ID: %hu, # of Blocks: %hu\n",
           MyTowers->Towers[Counter].Id,
           MyTowers->Towers[Counter].NumberOfBlocks);
  }

  printf("Performed Moves:\n");
  printf("%s\n",MyTowers->StringOfPerformedMoves);
  return;
}

int main()
{
  SET_OF_TOWERS MyTowers = init_set_of_towers();

//   print_set_of_towers(&MyTowers);

  while(MyTowers.Instability > 1 &&
    MyTowers.NumberOfAvailableMoves >= MyTowers.NumberOfMovesNecessaryToDecreaseInstability)
  {
    decrease_instability_and_record_move_for_set_of_towers(&MyTowers);
//     print_set_of_towers(&MyTowers);
  }

  printf("%hu %hu\n", MyTowers.Instability, MyTowers.NumberOfPerformedMoves);
  printf("%s\n", MyTowers.StringOfPerformedMoves);

  free_set_of_towers(&MyTowers);

  return 0;
}


#include <stdio.h>

int main() {
	int n, m, k, K;
	int tw[100];
	int aa[2000][2];
	int min, max;
	int i, j = 0;

	scanf("%d %d", &n, &k);
	for (i = 0; i < n; i++){
		scanf("%d", tw + i);
	}

	while (1) {
		min = max = 0;
		for (i = 1; i < n; i++) {
			if (tw[i] < tw[min])
				min = i;
			if (tw[i] > tw[max])
				max = i;
		}
		if (tw[max] - tw[min] < 2 || j == k) break;

		tw[max]--;
		tw[min]++;
		aa[j][0] = max;
		aa[j][1] = min;
		j++;
	}

	printf("%d %d\n", tw[max] - tw[min], j);
	for (i = 0; i < j; i++)
		printf("%d %d\n", aa[i][0]+1, aa[i][1]+1);

	return 0;
}


#include <stdio.h>
int max,min,imax,imin;
void minmax(int *x);
int main()
{
	int n,k,i,p=0;
	scanf("%d %d",&n,&k);
	int x[100000],temp[100000];
	for(i=0;i<n;i++)
	{
		scanf("%d",&x[i]);
	}
	x[i]=-1;
	for(i=1;i<=k;i++)
	{
		minmax(x);
		if(max-min>1)
		{
			x[imin]=x[imin]+1;
			x[imax]=x[imax]-1;
			temp[p]=imax+1;
			temp[p+1]=imin+1;
			p=p+2;
		}
		else
		{
			break;
		}
	}
	temp[p]=0;
	minmax(x);
	printf("%d %d\n",max-min,i-1);
	for(i=0;temp[i]!=0;i=i+2)
	{
		printf("%d %d\n",temp[i],temp[i+1]);
	}
	return 0;
}
void minmax(int *x)
{
	int i;
	min=*x;
	max=*x;
	imax=0;
	imin=0;
	for(i=1;*(x+i)!=-1;i++)
	{
		if(*(x+i)>max)
		{
			max=*(x+i);
			imax=i;
		}
			if(*(x+i)<min)
		{
			min=*(x+i);
			imin=i;
		}
	}
}


#include <stdio.h>

int from[1000];
int to[10000];

int main(int argc, char const *argv[])
{
    int n;
    int k;
    scanf("%d %d\n", &n, &k);

    int towers[100];
    int i;
    for (i = 0; i < n; i++) {
        scanf("%d", &towers[i]);
    }

    int count = 0;
    int min;
    int max;
    while (1) {
        min = 10001;
        max = 0;
        int min_ind;
        int max_ind;

        for (i = 0; i < n; i++) {
            if (towers[i] < min) {
                min = towers[i];
                min_ind = i;
            }

            if (towers[i] > max) {
                max = towers[i];
                max_ind = i;
            }
        }

        if (count >= k) {
            break;
        }

        if (max - min < 2) {
            break;
        }

        towers[max_ind]--;
        from[count] = max_ind + 1;

        towers[min_ind]++;
        to[count] = min_ind + 1;

        count++;
    }

    // printf("max: %d, min: %d\n", max - 1, min + 1);

    printf("%d %d\n", max - min, count);

    for (i = 0; i < count; i++) {
        printf("%d %d\n", from[i], to[i]);
    }

    return 0;
}


#include <stdio.h>

int min(int *arr, int len);
int max(int *arr, int len);
int nmin(int *arr, int len);
int nmax(int *arr, int len);
void move(int from, int to, int *arr);
int get_sum(int *arr, int len);
int get_unstable(int *arr, int len);

int main(){
	int n, k;
	unsigned int *arr;
	int i;

	int m, j, t, etalon;

	int answer[1000][2];

	scanf("%d %d", &n, &k);
	arr = (int *)calloc(n, sizeof(int));
	for (i = 0; i < n; i++){
		scanf("%d", &arr[i]);
	}
	etalon = (get_sum(arr, n) % n) == 0 ? 0 : 1;
	m = 0;
	do{
		int tmax, tmin;
		tmax = max(arr, n);
		tmin = min(arr, n);
		j = get_unstable(arr, n);

		//printf("%d %d\n", tmax+1, tmin+1);
		answer[m][0] = tmax + 1;
		answer[m][1] = tmin + 1;

		move(tmax, tmin, arr);
		++m;

		t = get_unstable(arr, n);

	} while (j != t && etalon != t && m < k);
	printf("%d %d\n", t, m);
	for (i = 0; i < m; i++)
		printf("%d %d\n", answer[i][0], answer[i][1]);
	system("pause");
	return 0;
}

int min(int *arr, int len){
	int min = 0, i;
	for (i = 1; i < len; i++)
	if (arr[min] > arr[i])
		min = i;
	return min;
}

int max(int *arr, int len){
	int max = 0, i;
	for (i = 1; i < len; i++)
	if (arr[max] < arr[i])
		max = i;
	return max;
}

void move(int from, int to, int *arr){
	arr[from]--;
	arr[to]++;
}

int get_sum(int *arr, int len){
	int i,res = 0;

	for (i = 0; i < len; i++)
		res += arr[i];

	return res;
}

int get_unstable(int *arr, int len){
	return nmax(arr, len) - nmin(arr, len);
}

int nmin(int *arr, int len){
	int min = arr[0], i;
	for (i = 1; i < len; i++)
	if (min > arr[i])
		min = arr[i];
	return min;
}

int nmax(int *arr, int len){
	int max = arr[0], i;
	for (i = 1; i < len; i++)
	if (max < arr[i])
		max = arr[i];
	return max;
}


#include <stdio.h>

int min(int *arr, int len);
int max(int *arr, int len);
int nmin(int *arr, int len);
int nmax(int *arr, int len);
void move(int from, int to, int *arr);
int get_sum(int *arr, int len);
int get_unstable(int *arr, int len);

int main(){
    int n, k;
    unsigned int *arr;
    int i;

    int m, j, t, etalon;

    int answer[1000][2];

    scanf("%d %d", &n, &k);
    arr = (int *)calloc(n, sizeof(int));
    for (i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    etalon = (get_sum(arr, n) % n) == 0 ? 0 : 1;
    m = 0;
    do{
        int tmax, tmin;
        tmax = max(arr, n);
        tmin = min(arr, n);
        j = get_unstable(arr, n);

        //printf("%d %d\n", tmax+1, tmin+1);
        answer[m][0] = tmax + 1;
        answer[m][1] = tmin + 1;

        move(tmax, tmin, arr);
        ++m;

        t = get_unstable(arr, n);

    } while (j != t && etalon != t && m < k);
    printf("%d %d\n", t, m);
    for (i = 0; i < m; i++)
        printf("%d %d\n", answer[i][0], answer[i][1]);
    return 0;
}

int min(int *arr, int len){
    int min = 0, i;
    for (i = 1; i < len; i++)
    if (arr[min] > arr[i])
        min = i;
    return min;
}

int max(int *arr, int len){
    int max = 0, i;
    for (i = 1; i < len; i++)
    if (arr[max] < arr[i])
        max = i;
    return max;
}

void move(int from, int to, int *arr){
    arr[from]--;
    arr[to]++;
}

int get_sum(int *arr, int len){
    int i,res = 0;

    for (i = 0; i < len; i++)
        res += arr[i];

    return res;
}

int get_unstable(int *arr, int len){
    return nmax(arr, len) - nmin(arr, len);
}

int nmin(int *arr, int len){
    int min = arr[0], i;
    for (i = 1; i < len; i++)
    if (min > arr[i])
        min = arr[i];
    return min;
}

int nmax(int *arr, int len){
    int max = arr[0], i;
    for (i = 1; i < len; i++)
    if (max < arr[i])
        max = arr[i];
    return max;
}


#include <stdio.h>

int min(int *arr, int len);
int max(int *arr, int len);
int nmin(int *arr, int len);
int nmax(int *arr, int len);
void move(int from, int to, int *arr);
int get_sum(int *arr, int len);
int get_unstable(int *arr, int len);

int main(){
    int n, k;
    unsigned int *arr;
    int i;

    int m, s, etalon;

    int answer[1000][2] = { 0, 0 };

    scanf("%d %d", &n, &k);
    arr = (int *)calloc(n, sizeof(int));
    for (i = 0; i < n; i++){
        scanf("%d", &arr[i]);
    }
    etalon = (get_sum(arr, n) % n) == 0 ? 0 : 1;
    m = 0;
    do{
        int tmax, tmin;
        tmax = max(arr, n);
        tmin = min(arr, n);

        //printf("%d %d\n", tmax+1, tmin+1);
        if (tmin == tmax){
            printf("0 0");
            return 0;
        }
        answer[m][0] = tmax + 1;
        answer[m][1] = tmin + 1;

        move(tmax, tmin, arr);
        ++m;
        s = get_unstable(arr, n);
    } while (etalon != s && m < k);
    printf("%d %d\n", s, m);
    for (i = 0; i < m; i++)
        printf("%d %d\n", answer[i][0], answer[i][1]);
    //system("pause");
    return 0;
}

int min(int *arr, int len){
    int min = 0, i;
    for (i = 1; i < len; i++)
    if (arr[min] > arr[i])
        min = i;
    return min;
}

int max(int *arr, int len){
    int max = 0, i;
    for (i = 1; i < len; i++)
    if (arr[max] < arr[i])
        max = i;
    return max;
}

void move(int from, int to, int *arr){
    arr[from]--;
    arr[to]++;
}

int get_sum(int *arr, int len){
    int i,res = 0;

    for (i = 0; i < len; i++)
        res += arr[i];

    return res;
}

int get_unstable(int *arr, int len){
    return nmax(arr, len) - nmin(arr, len);
}

int nmin(int *arr, int len){
    int min = arr[0], i;
    for (i = 1; i < len; i++)
    if (min > arr[i])
        min = arr[i];
    return min;
}

int nmax(int *arr, int len){
    int max = arr[0], i;
    for (i = 1; i < len; i++)
    if (max < arr[i])
        max = arr[i];
    return max;
}


#include<stdio.h>
int main()
{
    int n,k,i,j,step=0,bpos,spos,b,s,a[105],steps[10000][2];
    long long int sum=0;
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=1;i<=k;i++)
    {
        b=0;
        s=99999;
        for(j=0;j<n;j++)
        {
            if(a[j]>b)
            {
                b=a[j];
                bpos=j;
            }
            if(a[j]<s)
            {
                s=a[j];
                spos=j;
            }
        }
        if(b-s<2)
            break;
        steps[step][0]=bpos+1;
        steps[step++][1]=spos+1;
        a[bpos]--;
        a[spos]++;
    }
    b=0;
    s=99999;
    for(j=0;j<n;j++)
    {
        if(a[j]>b)
        {
            b=a[j];
            bpos=j;
        }
        if(a[j]<s)
        {
            s=a[j];
            spos=j;
        }
    }
    printf("%d %d\n",b-s,step);
    for(i=0;i<step;i++)
        printf("%d %d\n",steps[i][0],steps[i][1]);
    return 0;
}


   //This program is for practice solve 1 daily scheme
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100

//int comp(const void* a,const void* b){
 //  return *(int*)a - *(int*)b;
//}

typedef struct solution{
   int i;
   int j;
}sol;

int main()
{
   int i,n,k,max,min,ans,steps=0,imax,imin;
   sol answer[1000];
   scanf("%d%d",&n,&k);
   int a[n+1];
   for(i=1;i<=n;i++)
       scanf("%d",&a[i]);
   //input over algorithm part

   /*-------------------------------------------------
    1. drop the cube from max to min number every time.
    2. check whether all  the towers are >=1 height.
    3. repeat.
   --------------------------------------------------*/
   while(steps<=k){
      max = 0;
      min = 100000000;
      for(i=1;i<=n;i++){
         if(a[i]<min){
            min = a[i];
            imin = i;
         }
         if(a[i]>max){
            max = a[i];
            imax = i;
         }
      }
     /* if(a[imax]>1){
         a[imax]--;
         a[imin]++;
      }*/
      ans = max - min;
      answer[steps].i = imax;
      answer[steps].j = imin;
      steps++;
      if(max==min || ans<=1)
        break;
      a[imax]--;
      a[imin]++;

   }
   printf("%d %d\n",ans,steps-1);
   for(i=0;i<steps-1;i++)
      printf("%d %d\n",answer[i].i,answer[i].j);
   return 0;
}


//This program is for practice solve 1 daily scheme
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100

//int comp(const void* a,const void* b){
 //  return *(int*)a - *(int*)b;
//}

typedef struct solution{
   int i;
   int j;
}sol;

int main()
{
   int i,n,k,max,min,ans,steps=0,imax,imin;
   sol answer[1000];
   scanf("%d%d",&n,&k);
   int a[n+1];
   for(i=1;i<=n;i++)
       scanf("%d",&a[i]);
   //input over algorithm part
   /*-------------------------------------------------
    1. drop the cube from max to min number every time.
    2. check whether all  the towers are >=1 height.
    3. repeat.
   --------------------------------------------------*/
   while(steps<=k){
      max = 0;
      min = 100000000;
      for(i=1;i<=n;i++){
         if(a[i]<min){
            min = a[i];
            imin = i;
         }
         if(a[i]>max){
            max = a[i];
            imax = i;
         }
      }
     /* if(a[imax]>1){
         a[imax]--;
         a[imin]++;
      }*/
      ans = max - min;
      answer[steps].i = imax;
      answer[steps].j = imin;
      steps++;
      if(max==min || ans<=1)
        break;
      a[imax]--;
      a[imin]++;

   }
   printf("%d %d\n",ans,steps-1);
   for(i=0;i<steps-1;i++)
      printf("%d %d\n",answer[i].i,answer[i].j);
   return 0;
}


//This program is for practice solve 1 daily scheme
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100

//int comp(const void* a,const void* b){
 //  return *(int*)a - *(int*)b;
//}

typedef struct solution{
   int i;
   int j;
}sol;

int main()
{
   int i,n,k,max,min,ans,steps=0,imax,imin;
   sol answer[1000];
   scanf("%d%d",&n,&k);
   int a[n+1];
   for(i=1;i<=n;i++)
       scanf("%d",&a[i]);
   //input over algorithm part
   /*-------------------------------------------------
    1. drop the cube from max to min number every time.
    2. check whether all  the towers are >=1 height.
    3. repeat.
   --------------------------------------------------*/
   while(steps<=k){
      max = 0;
      min = 2000;
      for(i=1;i<=n;i++){
         if(a[i]<min){
            min = a[i];
            imin = i;
         }
         if(a[i]>max){
            max = a[i];
            imax = i;
         }
      }
      if(a[imax]>1){
         a[imax]--;
         a[imin]++;
      }
      ans = max - min;
      answer[steps].i = imax;
      answer[steps].j = imin;
      steps++;
      if(max==min || ans==1)
        break;
   }
   printf("%d %d\n",ans,steps-1);
   for(i=0;i<steps-1;i++)
      printf("%d %d\n",answer[i].i,answer[i].j);
   return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct A{
    int w;
    int v;
}a[101];

int n,k,i,j,m,x;

int compfunc(const void * a, const void *b){
    const struct A x = *(const struct A*)a;
    const struct A y = *(const struct A*)b;

    return y.v - x.v;
}

int main(){
    scanf("%d", &n);
    scanf("%d", &k);

    struct A xk[k][2];

    for(i=0; i<n; i++){
        a[i].w = i+1;
        scanf("%d", &a[i].v);
    }

    m=0;
    qsort(a,n, sizeof(a[0]), compfunc);
    x = a[0].v - a[n-1].v;

    for(i=0;i<k; i++){
        if(abs((a[0].v-1) - (a[n-1].v+1)) < x){
            x = a[0].v - a[n-1].v;
            xk[m][0] = a[0];
            xk[m][1] = a[n-1];
            a[0].v -= 1;
            a[n-1].v += 1;
            m+=1;
            qsort(a,n, sizeof(a[0]), compfunc);
            x = a[0].v - a[n-1].v;
        }
    }

    printf("%d %d\n", x, m);
    for(i=0; i<m; i++){
        printf("%d %d\n", xk[i][0].w, xk[i][1].w);
    }

}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct A{
    int w;
    int v;
}a[101];

int n,k,i,j,m,x;

int compfunc(const void * a, const void *b){
    const struct A x = *(const struct A*)a;
    const struct A y = *(const struct A*)b;

    return y.v - x.v;
}

int main(){
    scanf("%d", &n);
    scanf("%d", &k);

    struct A xk[k][2];

    for(i=0; i<n; i++){
        a[i].w = i+1;
        scanf("%d", &a[i].v);
    }

    m=0;
    qsort(a,n, sizeof(a[0]), compfunc);
    x = a[0].v - a[n-1].v;

    for(i=0;i<k; i++){
        if(abs((a[0].v-1) - (a[n-1].v+1)) < x){
            x = a[0].v - a[n-1].v;
            xk[m][0] = a[0];
            xk[m][1] = a[1];
            a[0].v -= 1;
            a[n-1].v += 1;
            m+=1;
            qsort(a,n, sizeof(a[0]), compfunc);
            x = a[0].v - a[n-1].v;
        }
    }

    printf("%d %d\n", x, m);
    for(i=0; i<m; i++){
        printf("%d %d\n", xk[i][0].w, xk[i][1].w);
    }

}


#include<stdio.h>

int main()
{
    int n,k,i,count=0,max=0,maxind=0,min=10000,minind=0,temp=0,instability=2,arinstability=-1;
    scanf("%d %d",&n,&k);

    int tower[n],tower2[n];

    for(i=0;i<n;i++)
        scanf("%d",&tower[i]);

    for(i=0;i<n;i++)
        tower2[i]=tower[i];

    while(count<k)
    {
        for(i=0;i<n;i++)
        {
            temp=tower[i];

            if(max<temp)
            {
                max=temp;
                maxind=i;
            }
            if(temp<min)
            {
                min=temp;
                minind=i;
            }

        }

        instability=max-min;

        if(instability>=2)
        {
            tower[maxind]--;
            tower[minind]++;
        }

        if(instability<=1)
        {
            arinstability=instability;
            break;
        }
        min=10000;
        max=0;
        minind=0;
        maxind=0;
        count++;
    }

    for(i=0;i<n;i++)
        {
            temp=tower[i];

            if(max<temp)
            {
                max=temp;
                maxind=i;
            }
            if(temp<min)
            {
                min=temp;
                minind=i;
            }

        }

        instability=max-min;

    printf("\n%d %d \n",instability,count);

    instability=3;
    min=10000;
    max=0;
    minind=0;
    maxind=0;
    count=0;

    while(count<k)
    {
        for(i=0;i<n;i++)
        {
            temp=tower2[i];

            if(max<temp)
            {
                max=temp;
                maxind=i;
            }
            if(temp<min)
            {
                min=temp;
                minind=i;
            }

        }

        instability=max-min;

        if(instability>=2)
        {
            printf("%d %d \n",maxind+1,minind+1);
            tower2[maxind]--;
            tower2[minind]++;
        }

        if(instability<=1)
        {
            break;
        }
        min=10000;
        max=0;
        minind=0;
        maxind=0;
        count++;
    }

    return 0;

}


#include<stdio.h>

int main()
{
    int n,k,i,count=0,max=0,maxind=0,min=10000,minind=0,temp=0,instability=2;
    scanf("%d %d",&n,&k);

    int tower[n],tower2[n];

    for(i=0;i<n;i++)
        scanf("%d",&tower[i]);

    for(i=0;i<n;i++)
        tower2[i]=tower[i];

    while(count<k)
    {
        for(i=0;i<n;i++)
        {
            temp=tower[i];

            if(max<temp)
            {
                max=temp;
                maxind=i;
            }
            if(temp<min)
            {
                min=temp;
                minind=i;
            }

        }

        instability=max-min;

        if(instability>=2)
        {
            tower[maxind]--;
            tower[minind]++;
        }

        if(instability<=1)
        {
            break;
        }
        min=10000;
        max=0;
        minind=0;
        maxind=0;
        count++;
    }

    if(instability==2)
        instability=0;

    printf("\n%d %d \n",instability,count);

    instability=3;
    min=10000;
    max=0;
    minind=0;
    maxind=0;
    count=0;

    while(count<k)
    {
        for(i=0;i<n;i++)
        {
            temp=tower2[i];

            if(max<temp)
            {
                max=temp;
                maxind=i;
            }
            if(temp<min)
            {
                min=temp;
                minind=i;
            }

        }

        instability=max-min;

        if(instability>=2)
        {
            printf("%d %d \n",maxind+1,minind+1);
            tower2[maxind]--;
            tower2[minind]++;
        }

        if(instability<=1)
        {
            break;
        }
        min=10000;
        max=0;
        minind=0;
        maxind=0;
        count++;
    }

    return 0;

}


#include <stdio.h>
#include <string.h>
int maximo(int arreglo[],int n){
    int i,max;
    max=arreglo[0];
    for(i=1;i<n;i++){
        if(arreglo[i]>max){
            max=arreglo[i];
        }
    }
    return max;
}
int ubicar(int arreglo[],int n,int busca){
    int i;
    for(i=0;i<n;i++){
        if(arreglo[i]==busca){
            break;
        }
    }
    return i;
}
int minimo(int arreglo[],int n){
    int i,min;
    min=arreglo[0];
    for(i=1;i<n;i++){
        if(arreglo[i]<min){
            min=arreglo[i];
        }
    }
    return min;
}
int numeros[101];
int numeroscopia[101];
int i,n,k;
int main(int argc, char *argv[]) {
    scanf("%d%d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d", numeros + i);
    }
    for(i=0;i<n;i++){
        numeroscopia[i]=numeros[i];
    }
    for(i=0;i<k;i++){
        if(maximo(numeroscopia,n)-minimo(numeroscopia,n)>1){
            numeroscopia[ubicar(numeroscopia,n,maximo(numeroscopia,n))]--;
            numeroscopia[ubicar(numeroscopia,n,minimo(numeroscopia,n))]++;
        }else{
            break;
        }
    }
    printf("%d %d",maximo(numeroscopia,n)-minimo(numeroscopia,n),i);
    for(i=0;i<k;i++){
        if(maximo(numeros,n)-minimo(numeros,n)>1){
            printf("\n%d %d",ubicar(numeros,n,maximo(numeros,n))+1,ubicar(numeros,n,minimo(numeros,n))+1);
            numeros[ubicar(numeros,n,maximo(numeros,n))]--;
            numeros[ubicar(numeros,n,minimo(numeros,n))]++;
        }else{
            break;
        }
    }

}


#include<stdio.h>
#include<stdlib.h>

int maxarray[3000], minarray[3000], maxTarray[3000], minaTrray[3000], towers[1000];

int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	int min = 0;
	int max = 0;
	int i, j, ans;
	int steps = 0;
	int solution = 0;
	int prev = 0;

	for (i = 0; i < n; i++)
	{
		scanf("%d", &towers[i]);
		if (towers[min] > towers[i])
		{
			min = i;
		}
		if (towers[max] < towers[i])
		{
			max = i;
		}
	}
	ans = towers[max] - towers[min];
	prev = ans;
	solution = 0;
	if((max - min) == 0)
	{
		printf("0 0\n");
	}
	else
	{
		if(k != 0)
		{
			for (i = 0; i < k; ++i)
			{
				maxarray[i] = max;
				minarray[i] = min;
				towers[min]++;
				towers[max]--;
				maxTarray[i] = towers[max];
				minaTrray[i] = towers[min];
				max = 0;
				min = 0;
				for (j = 0; j < n; ++j)
				{
					if (towers[min] > towers[j])
					{
						min = j;
					}
					if (towers[max] < towers[j])
					{
						max = j;
					}
				}
				if(prev == (towers[max] - towers[min]))
				{
					prev = (towers[max] - towers[min]);
					steps++;
				}
				else
				{
					prev = (towers[max] - towers[min]);
					steps++;
					solution = steps;
				}
			}
			for (i = 0; i < n; i++)
			{
				if (towers[min] > towers[i])
				{
					min = i;
				}
				if (towers[max] < towers[i])
				{
					max = i;
				}
				ans = towers[max] - towers[min];
			}
			printf("%d %d\n", ans, solution);
			for (i = 0; i < solution; ++i)
			{
				printf("%d %d\n", (maxarray[i] + 1), (minarray[i] + 1));
			}
		}
	}
	if (k == 0)
	{
		printf("%d %d\n", ans, solution);
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>

int maxarray[3000], minarray[3000], maxTarray[3000], minaTrray[3000], towers[1000];

int main()
{
	int n, k;
	scanf("%d %d", &n, &k);
	int min = 0;
	int max = 0;
	int i, j, ans;
	int steps = 0;
	int solution = 0;
	int prev = 0;

	for (i = 0; i < n; i++)
	{
		scanf("%d", &towers[i]);
		if (towers[min] > towers[i])
		{
			min = i;
		}
		if (towers[max] < towers[i])
		{
			max = i;
		}
	}
	ans = towers[max] - towers[min];
	prev = ans;
	solution = 0;
	if((max - min) == 0)
	{
		printf("0 0\n");
	}
	else
	{
		for (i = 0; i < k; ++i)
		{
			maxarray[i] = max;
			minarray[i] = min;
			towers[min]++;
			towers[max]--;
			maxTarray[i] = towers[max];
			minaTrray[i] = towers[min];
			max = 0;
			min = 0;
			for (j = 0; j < n; ++j)
			{
				if (towers[min] > towers[j])
				{
					min = j;
				}
				if (towers[max] < towers[j])
				{
					max = j;
				}
			}
			if(prev == (towers[max] - towers[min]))
			{
				prev = (towers[max] - towers[min]);
				steps++;
			}
			else
			{
				prev = (towers[max] - towers[min]);
				steps++;
				solution = steps;
			}
		}
		for (i = 0; i < n; i++)
		{
			if (towers[min] > towers[i])
			{
				min = i;
			}
			if (towers[max] < towers[i])
			{
				max = i;
			}
			ans = towers[max] - towers[min];
		}
		printf("%d %d\n", ans, solution);
		int flag = 0;
		for (i = 0; i < solution; ++i)
		{
			if((ans == (maxTarray[i] - minaTrray[i])) && (flag == 1))
			{
				flag = 1;
			}
			else
			{
				printf("%d %d\n", (maxarray[i] + 1), (minarray[i] + 1));
				if((ans == (maxTarray[i] - minaTrray[i])))
					flag = 1;
			}
		}
	}
	if (k == 0)
	{
		printf("%d %d\n", ans, solution);
	}
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define wl(t) while(t--)
#define ll long long int
#define scan(n) scanf("%d", &n)
#define scans(s) scanf("%s", s)
#define scanc(c) scanf("%c", &c)
#define scanp(f) scanf("%f", &f)
#define scanll(l) scanf("%lld", &l)
#define scanllu(u) scanf("%llu", &u)
#define print(n) printf("%d\n", n)
#define prints(s) printf("%s\n", s)
#define printc(c) printf("%c\n", c)
#define printp(f) printf("%f\n", f)
#define printll(l) printf("%lld\n", l)
#define printllu(u) printf("%llu\n", u)
#define nline printf("\n")

int max(int *a,int n)
{
    int i,isave=0,max=0;
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
            isave=i;
        }
    }
    return isave;
}
int min(int *a,int n)
{
    int i,isave=0,min=10000;
    for(i=0;i<n;i++)
     {
        if(a[i]<min)
        {
            min=a[i];
            isave=i;
        }
     }
     return isave;
}
int main()
{
    int n,k,a[100],s,i,j,X[1001],Y[1001],x,y;
    scan(n);
    scan(k);
    for(i=0;i<n;i++)
       scan(a[i]);
    for(i=1;i<=k;i++)
    {
        x=max(a,n);
        y=min(a,n);
        if(a[x]-a[y]<=1)
          break;
        else
          {
            a[x]--;
            X[i]=x+1;
            a[y]++;
            Y[i]=y+1;
          }

    }
    x=max(a,n);
    y=min(a,n);
    s=a[x]-a[y];
    printf("%d %d\n",s,i-1);
    for(j=1;j<i;j++)
    {
        printf("%d %d\n",X[j],Y[j]);
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define wl(t) while(t--)
#define ll long long int
#define scan(n) scanf("%d", &n)
#define scans(s) scanf("%s", s)
#define scanc(c) scanf("%c", &c)
#define scanp(f) scanf("%f", &f)
#define scanll(l) scanf("%lld", &l)
#define scanllu(u) scanf("%llu", &u)
#define print(n) printf("%d\n", n)
#define prints(s) printf("%s\n", s)
#define printc(c) printf("%c\n", c)
#define printp(f) printf("%f\n", f)
#define printll(l) printf("%lld\n", l)
#define printllu(u) printf("%llu\n", u)
#define nline printf("\n")

inline int inp()
{
    int noRead=0;
    char p=getchar_unlocked();
    for(;p<33;)
    {
     p=getchar_unlocked();
    };
    while(p>32)
    {
     noRead = (noRead << 3) + (noRead << 1) + (p - '0');
     p=getchar_unlocked();
    }
    return noRead;
};

int max(int *a,int n)
{
    int i,isave=0,max=0;
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
            isave=i;
        }
    }
    return isave;
}
int min(int *a,int n)
{
    int i,isave=0,min=10000;
    for(i=0;i<n;i++)
     {
        if(a[i]<min)
        {
            min=a[i];
            isave=i;
        }
     }
     return isave;
}
int main()
{
    int n,k,a[100],s,i,j,X[1001],Y[1001],x,y;
    n=inp();
    k=inp();
    for(i=0;i<n;i++)
       a[i]=inp();
    for(i=1;i<=k;i++)
    {
        x=max(a,n);
        y=min(a,n);
        if(a[x]-a[y]<=1)
          break;
        else
          {
            a[x]--;
            X[i]=x+1;
            a[y]++;
            Y[i]=y+1;
          }

    }
    x=max(a,n);
    y=min(a,n);
    s=a[x]-a[y];
    printf("%d %d\n",s,i-1);
    for(j=1;j<i;j++)
    {
        printf("%d %d\n",X[j],Y[j]);
    }
    return 0;
}


#include <stdio.h>

int main()
{
    int N, K, i, Size;
    int minid, maxid, min, max;
    int A[100], Ans[1000][2];
    scanf("%d %d", &N, &K);
    for(i=0; i<N; ++i)
    {
        scanf("%d", &A[i]);
    }
    Size = 0;
    while(K--)
    {
        min = 0x3f3f3f3f;
        max = 0;
        for(i=0; i<N; ++i)
        {
            if(A[i] > max)
            {
                max = A[i];
                maxid = i;
            }
            if(A[i] < min)
            {
                min = A[i];
                minid = i;
            }
        }
        if(max-min < 2)
        {
            break;
        }
        Ans[Size][0] = maxid;
        Ans[Size++][1] = minid;
        --A[maxid];
        ++A[minid];
    }
    min = 0x3f3f3f3f;
    max = 0;
    for(i=0; i<N; ++i)
    {
        if(A[i] > max)
        {
            max = A[i];
            maxid = i;
        }
        if(A[i] < min)
        {
            min = A[i];
            minid = i;
        }
    }
    printf("%d %d\n", max-min, Size);
    for(i=0; i<Size; ++i)
    {
        printf("%d %d\n", Ans[i][0]+1, Ans[i][1]+1);
    }
    return 0;
}


#include<stdio.h>
#define N 101
#define OP 10000
int main() {
	int n,k;
	scanf("%d%d",&n,&k);
	//printf("%d个塔\n",n);
	//printf("最多%d个操作\n",k);
	int a[N];
	int i;
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	/*
	for(i=1;i<=n;i++){
		printf(" %d",a[i]);
	}
	printf("\n");
	*/

	//打印最小不稳定数，
	//打印需要的操作数
	//打印具体的操作过程
	//这里这个塔就是由一个个方块由下向上垒起来的！
	//所以方块的个数就是塔的高度
	//问题一，数据三中为什么要多操作一次？
	int op=0;
	int move[OP];
	int to[OP];
	int instability=0;
	while(1){
		int max=1;
		int min=1;
		for(i=2;i<=n;i++){
			if(a[i]>a[max]){
				max=i;
			}
			if(a[i]<a[min]){
				min=i;
			}
		}
		//printf("%d %d\n",a[min],a[max]);
		instability=a[max]-a[min];
		if(op==k){
			break;
		}
		if(instability==0){
			break;
		}else{
			if(a[max]>0 && a[max]-1>=a[min]+1){
				move[op]=max;
				to[op]=min;
				op++;
				a[max]--;
				a[min]++;
				if(op==OP){
					printf("我申请的空间不够\n");
				}
			}else{
				break;
			}
		}
		/*
		for(i=1;i<=n;i++){
			printf(" %d",a[i]);
		}
		printf("\n");
		*/
	}
	printf("%d %d\n",instability,op);
	for(i=0;i<op;i++){
		printf("%d %d\n",move[i],to[i]);
	}

	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int n,k;
int a[100];
int swap[1000][2];
int min,max;
int s,t;
int p;
int c;

int main(){

	int i,j;

	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);

	for(i=0;i<k;i++){
		max=0;
		min=100000;
		for(j=0;j<n;j++){
			if(min>a[j]){
				min=a[j];
				s=j;
			}
			if(max<a[j]){
				max=a[j];
				t=j;
			}
		}
		if(s==t){
			i++;
			break;
		}

		a[t]--;
		a[s]++;
		swap[p][0]=s<t?s+1:t+1;
		swap[p++][1]=s<t?t+1:s+1;
	}

	for(j=0;j<n;j++){
		if(min>a[j]){
			min=a[j];
			s=j;
		}
		if(max<a[j]){
			max=a[j];
			t=j;
		}
	}

	printf("%d %d\n",max-min,i);
	for(i=0;i<p;i++)
		printf("%d %d\n",swap[i][0],swap[i][1]);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int n,k;
int a[100];
int swap[1000][2];
int min,max;
int s,t;
int p;
int c;

int main(){

	int i,j;

	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);

	for(i=0;i<k;i++){
		max=0;
		min=100000;
		for(j=0;j<n;j++){
			if(min>a[j]){
				min=a[j];
				s=j;
			}
			if(max<a[j]){
				max=a[j];
				t=j;
			}
		}
		if(s==t){
			i++;
			break;
		}
		a[t]--;
		a[s]++;
		swap[p][0]=s<t?s+1:t+1;
		swap[p++][1]=s<t?t+1:s+1;
	}

	max=0;
	min=100000;

	for(j=0;j<n;j++){
		if(min>a[j]){
			min=a[j];
			s=j;
		}
		if(max<a[j]){
			max=a[j];
			t=j;
		}
	}

	printf("%d %d\n",max-min,i);
	for(i=0;i<p;i++)
		printf("%d %d\n",swap[i][0],swap[i][1]);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int n,k;
int a[100];
int swap[1000][2];
int min,max;
int s,t;
int p;
int c;

int main(){

	int i,j;

	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);

	for(i=0;i<k;i++){
		max=0;
		min=100000;
		for(j=0;j<n;j++){
			if(min>a[j]){
				min=a[j];
				s=j;
			}
			if(max<a[j]){
				max=a[j];
				t=j;
			}
		}
		if(s==t)
			break;
		a[t]--;
		a[s]++;
		swap[p][0]=t+1;
		swap[p++][1]=s+1;
	}

	max=0;
	min=100000;

	for(j=0;j<n;j++){
		if(min>a[j]){
			min=a[j];
			s=j;
		}
		if(max<a[j]){
			max=a[j];
			t=j;
		}
	}

	printf("%d %d\n",max-min,i);
	for(i=0;i<p;i++)
		printf("%d %d\n",swap[i][0],swap[i][1]);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int n,k;
int a[100];
int swap[1000][2];
int min,max;
int s,t;
int p;
int c;

int main(){

	int i,j;

	scanf("%d%d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);

	for(i=0;i<k;i++){
		max=0;
		min=100000;
		for(j=0;j<n;j++){
			if(min>a[j]){
				min=a[j];
				s=j;
			}
			if(max<a[j]){
				max=a[j];
				t=j;
			}
		}
		if(s==t){
			i++;
			break;
		}
		a[t]--;
		a[s]++;
		swap[p][0]=t+1;
		swap[p++][1]=s+1;
	}

	max=0;
	min=100000;

	for(j=0;j<n;j++){
		if(min>a[j]){
			min=a[j];
			s=j;
		}
		if(max<a[j]){
			max=a[j];
			t=j;
		}
	}

	printf("%d %d\n",max-min,i);
	for(i=0;i<p;i++)
		printf("%d %d\n",swap[i][0],swap[i][1]);

	return 0;

}


#include <stdio.h>

int main() {
	int t[100];
	int path[1000][2];
	int n,k,i,j,dif,max,min,cnt;
	scanf("%d %d", &n, &k);
	for(i=0; i<n; i++)
		scanf("%d", &t[i]);
	max=min=0;
	for(i=0; i<n; i++)	{
		if(t[max]<t[i]) max=i;
		if(t[min]>t[i]) min=i;
	}
	dif=t[max]-t[min];
	cnt = k;
	for(i=0; i<k; i++) {
		if(max!=min && dif>1) {
			t[max]--;
			t[min]++;
			path[i][0]=max;
			path[i][1]=min;
		} else {
			cnt=i;
			break;
		}
		max=min=0;
		for(j=0; j<n; j++)	{
			if(t[max]<t[j]) max=j;
			if(t[min]>t[j]) min=j;
		}
		dif=t[max]-t[min];
	}
	printf("%d %d\n",dif, cnt);
	for(j=0; j<cnt; j++)
		printf("%d %d\n", path[j][0]+1, path[j][1]+1);
	return 0;
}


#include<stdio.h>
#include<string.h>
long a[111],num[111],give[1111],get[1111];
void swap(long *aaa,long *bbb)
{
    long t;
    t=*aaa;
    *aaa=*bbb;
    *bbb=t;
}
void qsort(long low,long high)
{
    long i=low,j=high,mid=a[(low+high)/2];
    while(i<j)
    {
        while(a[i]<mid)i++;
        while(a[j]>mid)j--;
        if(i<=j)
        {
            swap(&a[i],&a[j]);
            swap(&num[i],&num[j]);
            i++;
            j--;
        }
    }
    if(j>low)qsort(low,j);
    if(i<high)qsort(i,high);
}
int main()
{
    long n,k,i,len,min,minlen,o,o1,pd,he;
    scanf("%ld %ld",&n,&k);
    memset(num,0,sizeof(num));
    pd=1;
    he=0;
    for(i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
        if(i==1)he=a[i];
        else if(he==a[i])
        {
            pd++;
        }
        num[i]=i;
    }
    if(pd==n)
    {
        printf("0 0");
        return 0;
    }
    len=0;
    memset(give,0,sizeof(give));
    memset(get,0,sizeof(get));
    min=0x7fffffff;
    minlen=0;
    qsort(1,n);
    for(i=1;i<=k;i++)
    {
        a[1]++;
        a[n]--;
        o=num[n];
        o1=num[1];
        qsort(1,n);
        if(a[n]-a[1]<min)
        {
            minlen=len+1;
            min=a[n]-a[1];
        }
        give[++len]=o;
        get[len]=o1;
    }
    qsort(1,n);
    if(a[n]-a[1]<min)
    {
        minlen=len;
        min=a[n]-a[1];
    }
    printf("%ld %ld",a[n]-a[1],minlen);
    for(i=1;i<=minlen;i++)
    {
        printf("\n%ld %ld",give[i],get[i]);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
long a[111],num[111],give[1111],get[1111];
void swap(long *aaa,long *bbb)
{
    long t;
    t=*aaa;
    *aaa=*bbb;
    *bbb=t;
}
void qsort(long low,long high)
{
    long i=low,j=high,mid=a[(low+high)/2];
    while(i<j)
    {
        while(a[i]<mid)i++;
        while(a[j]>mid)j--;
        if(i<=j)
        {
            swap(&a[i],&a[j]);
            swap(&num[i],&num[j]);
            i++;
            j--;
        }
    }
    if(j>low)qsort(low,j);
    if(i<high)qsort(i,high);
}
int main()
{
    long n,k,i,len,min,minlen,o,o1;
    scanf("%ld %ld",&n,&k);
    memset(num,0,sizeof(num));
    for(i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
        num[i]=i;
    }
    len=0;
    memset(give,0,sizeof(give));
    memset(get,0,sizeof(get));
    min=0x7fffffff;
    minlen=0;
    qsort(1,n);
    for(i=1;i<=k;i++)
    {
        a[1]++;
        a[n]--;
        o=num[n];
        o1=num[1];
        qsort(1,n);
        if(a[n]-a[1]<min)
        {
            minlen=len+1;
            min=a[n]-a[1];
        }
        give[++len]=o;
        get[len]=o1;
    }
    qsort(1,n);
    if(a[n]-a[1]<min)
    {
        minlen=len;
        min=a[n]-a[1];
    }
    printf("%ld %ld",a[n]-a[1],minlen);
    for(i=1;i<=minlen;i++)
    {
        printf("\n%ld %ld",give[i],get[i]);
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
long a[111],num[111],give[1111],get[1111];
void swap(long *aaa,long *bbb)
{
    long t;
    t=*aaa;
    *aaa=*bbb;
    *bbb=t;
}
void qsort(long low,long high)
{
    long i=low,j=high,mid=a[(low+high)/2];
    while(i<j)
    {
        while(a[i]<mid)i++;
        while(a[j]>mid)j--;
        if(i<=j)
        {
            swap(&a[i],&a[j]);
            swap(&num[i],&num[j]);
            i++;
            j--;
        }
    }
    if(j>low)qsort(low,j);
    if(i<high)qsort(i,high);
}
int main()
{
    long n,k,i,len,min,minlen,o,o1,pd,he;
    scanf("%ld %ld",&n,&k);
    memset(num,0,sizeof(num));
    pd=1;
    he=0;
    for(i=1;i<=n;i++)
    {
        scanf("%ld",&a[i]);
        if(i==1)he=a[i];
        else if(he==a[i])
        {
            pd++;
        }
        num[i]=i;
    }
    if(pd==n)
    {
        printf("0 0");
        return 0;
    }
    len=0;
    memset(give,0,sizeof(give));
    memset(get,0,sizeof(get));
    min=0x7fffffff;
    minlen=0;
    qsort(1,n);
    for(i=1;i<=k;i++)
    {
        a[1]++;
        a[n]--;
        o=num[n];
        o1=num[1];
        qsort(1,n);
        if(a[n]-a[1]<min)
        {
            minlen=len+1;
            min=a[n]-a[1];
        }
        give[++len]=o;
        get[len]=o1;
    }
    qsort(1,n);
    if(a[n]-a[1]<min)
    {
        minlen=len;
        min=a[n]-a[1];
    }
    printf("%ld %ld",min,minlen);
    for(i=1;i<=minlen;i++)
    {
        printf("\n%ld %ld",give[i],get[i]);
    }
    return 0;
}


#include <stdio.h>

int main(void) {
    int n,i,k,arr[102]={0};
    int a[1002][2];
    scanf("%d %d",&n,&k);
    int min=10003,max=-1,mn,mx;
    for(i=1;i<=n;i++)
    {
        scanf("%d",&arr[i]);
        if(arr[i]<min)
        {
         min=arr[i];
         mn=i;
        }
         if(arr[i]>max)
        {
            max=arr[i];
            mx=i;
        }
    }
    //printf("%d %d min max",min,max);
    int cnt=0;
    int j=0;
    while(k!=0 && (max-min)!=0)
    {
        cnt++;
        arr[mn]++;arr[mx]--;
        a[j][0]=mx;a[j][1]=mn;j++;
        min++;max--;
        for(i=1;i<=n;i++)
        {
        if(arr[i]<min)
        {
         min=arr[i];
         mn=i;
        }
        if(arr[i]>max)
        {
            max=arr[i];
            mx=i;
        }
        }
        k--;
    }
    printf("%d %d\n",max-min,cnt);
    for(i=0;i<j;i++)
      printf("%d %d\n",a[i][0],a[i][1]);
    return 0;
}


#include<stdio.h>
int max,min,indmax,indmin;
int n,map[110];
int out[1100][5];
void pros()
{
	int i;
	for(i=1;i<=n;++i) {
		if(map[i] > max) {
			max = map[i];
			indmax = i;
		}
		if(map[i] < min) {
			min = map[i];
			indmin = i;
		}
	}
}
int main()
{
	int k;
	int i;
	int diff,ind;
	while((scanf("%d %d",&n,&k)) != EOF) {
		for(i=1;i<=n;++i) scanf("%d",&map[i]);
		max = 0;
		min = 10001;
		pros();
		diff = max - min;
		ind = 0;
		while((diff > 1) && (k > 0)) {
			--k;
			--map[indmax];
			++map[indmin];
			out[ind][0] = indmax;
			out[ind][1] = indmin;
			++ind;
			max = 0;
			min = 10001;
			pros();
			diff = max - min;
		}
		printf("%d %d\n",diff,ind);
		for(i=0;i<ind;++i) {
			printf("%d %d\n",out[i][0],out[i][1]);
		}

	}
	return 0;
}


#include<stdio.h>

int main()
{
    int n,k,i,a[101],b[1001][2],cnt=0,max,min,maxin,minin,j;
    scanf("%d%d",&n,&k);
    for (i=1;i<=n;i++)scanf("%d",&a[i]);
    for (i=0;i<k;i++)
    {
        min=100000,max=0;
        for (j=1;j<=n;j++)
        {
            if(a[j]<min)
            {
                min=a[j];
                minin=j;
            }
            if (a[j]>max)
            {
                max=a[j];
                maxin=j;
            }
        }
        if (max-min>=2)
        {
            a[maxin]--;
            a[minin]++;
            b[cnt][0]=maxin;
            b[cnt][1]=minin;
            cnt++;
        }
        else
        break;
    }
    min=100000,max=0;
    for (j=1;j<=n;j++)
        {
            if(a[j]<min)
            {
                min=a[j];
                minin=j;
            }
            if (a[j]>max)
            {
                max=a[j];
                maxin=j;
            }
        }
    printf("%d %d\n",max-min,cnt);
    for (i=0;i<cnt;i++)
    {
        printf("%d %d\n",b[i][0],b[i][1]);
    }

}


#include<stdio.h>

int main(){
    int n,k,avg=0,tower[110],ans[1010][2],big,bignum,small,smallnum,time=0,i,j;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&tower[i]);
    }
    for(i=0;i<k;i++){
        big=0;
        small=100;
        for(j=0;j<n;j++){
            if(tower[j]>big){ big=tower[j]; bignum=j; }
            if(tower[j]<small){ small=tower[j]; smallnum=j; }
        }
        if(big-small==0){
            break;
        }
        tower[bignum]--;
        tower[smallnum]++;
        ans[time][0]=bignum+1;
        ans[time][1]=smallnum+1;
        time++;
    }
    big=0;
    small=100;
    for(i=0;i<n;i++){
        if(tower[i]>big){ big=tower[i]; bignum=i; }
        if(tower[i]<small){ small=tower[i]; smallnum=i; }
    }
    printf("%d %d\n",big-small,time);
    for(i=0;i<time;i++){
        printf("%d %d\n",ans[i][0],ans[i][1]);
    }
    return 0;
}


#include<stdio.h>

int main(){
    int n,k,avg=0,tower[110],ans[1010][2],big=0,small=0,time=0,i;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&tower[i]);
        avg+=tower[i];
    }
    for(i=0;i<k;i++){
        while(tower[big]*n<=avg && big<n){ big++; }
        while(tower[small]*n>=avg && small<n){ small++; }
        if(big==n || small==n){
            i++;
            break;
        }
        tower[big]--;
        tower[small]++;
        ans[i][0]=big+1;
        ans[i][1]=small+1;
        time++;
    }
    big=0;
    small=100;
    for(i=0;i<n;i++){
        if(tower[i]>big){ big=tower[i]; }
        if(tower[i]<small){ small=tower[i]; }
    }
    printf("%d %d\n",big-small,time);
    for(i=0;i<time;i++){
        printf("%d %d\n",ans[i][0],ans[i][1]);
    }
    return 0;
}


#include<stdio.h>

int main(){
    int n,k,avg=0,tower[110],ans[1010][2],big,bignum,small,smallnum,time=0,i,j;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&tower[i]);
    }
    for(i=0;i<k;i++){
        big=-1;
        small=2147483647;
        for(j=0;j<n;j++){
            if(tower[j]>big){ big=tower[j]; bignum=j; }
            if(tower[j]<small){ small=tower[j]; smallnum=j; }
        }
        if(big==small){
            break;
        }
        tower[bignum]--;
        tower[smallnum]++;
        ans[time][0]=bignum+1;
        ans[time][1]=smallnum+1;
        time++;
    }
    big=0;
    small=100;
    for(i=0;i<n;i++){
        if(tower[i]>big){ big=tower[i]; bignum=i; }
        if(tower[i]<small){ small=tower[i]; smallnum=i; }
    }
    printf("%d %d\n",big-small,time);
    for(i=0;i<time;i++){
        printf("%d %d\n",ans[i][0],ans[i][1]);
    }
    return 0;
}


#include<stdio.h>

int main(){
    int n,k,avg=0,tower[110],ans[1010][2],big,bignum,small,smallnum,time=0,i,j;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&tower[i]);
    }
    for(i=0;i<k;i++){
        big=0;
        small=100;
        for(j=0;j<n;j++){
            if(tower[j]>big){ big=tower[j]; bignum=j; }
            if(tower[j]<small){ small=tower[j]; smallnum=j; }
        }
        if(big==small){
            break;
        }
        tower[bignum]--;
        tower[smallnum]++;
        ans[time][0]=bignum+1;
        ans[time][1]=smallnum+1;
        time++;
    }
    big=0;
    small=100;
    for(i=0;i<n;i++){
        if(tower[i]>big){ big=tower[i]; bignum=i; }
        if(tower[i]<small){ small=tower[i]; smallnum=i; }
    }
    printf("%d %d\n",big-small,time);
    for(i=0;i<time;i++){
        printf("%d %d\n",ans[i][0],ans[i][1]);
    }
    return 0;
}


#include<stdio.h>

int main(){
    int n,k,avg=0,tower[110],ans[1010][2],big,bignum,small,smallnum,time=0,i,j;
    scanf("%d %d",&n,&k);
    for(i=0;i<n;i++){
        scanf("%d",&tower[i]);
    }
    for(i=0;i<k;i++){
        big=-1;
        small=2147483647;
        for(j=0;j<n;j++){
            if(tower[j]>big){ big=tower[j]; bignum=j; }
            if(tower[j]<small){ small=tower[j]; smallnum=j; }
        }
        if(big==small){
            break;
        }
        tower[bignum]--;
        tower[smallnum]++;
        ans[time][0]=bignum+1;
        ans[time][1]=smallnum+1;
        time++;
    }
    big=-1;
    small=2147483647;
    for(i=0;i<n;i++){
        if(tower[i]>big){ big=tower[i]; bignum=i; }
        if(tower[i]<small){ small=tower[i]; smallnum=i; }
    }
    printf("%d %d\n",big-small,time);
    for(i=0;i<time;i++){
        printf("%d %d\n",ans[i][0],ans[i][1]);
    }
    return 0;
}


#include <stdio.h>
#include <stdlib.h>

#define MAX_N (100)
#define MAX_K (1000)

int ix_max(int * xs, int n) {
  int i,mx;

  for ( mx=0,i=1; i < n; ++i )
    mx = xs[i] > xs[mx] ? i : mx;
  return mx;
}

int ix_min(int * xs, int n) {
  int i,mn;

  for ( mn=0,i=1; i < n; ++i )
    mn = xs[i] < xs[mn] ? i : mn;
  return mn;
}

int main() {
  int xs[MAX_N];
  int opf[MAX_K];
  int opt[MAX_K];
  int i,j,n,k,mx,mn;

  scanf("%d %d\n", &n, &k);
  for ( i=0; i < n; ++i )
    scanf("%d", xs+i);
  mx = ix_max(xs, n);
  mn = ix_min(xs, n);
  for ( i=0; i < k && xs[mx] - xs[mn] > 1; ++i ) {
    xs[mx]--; opf[i] = mx+1;
    xs[mn]++; opt[i] = mn+1;
    mx = ix_max(xs, n);
    mn = ix_min(xs, n);
  }
  printf("%d %d\n", xs[ix_max(xs, n)] - xs[ix_min(xs, n)], i);
  for ( j=0; j < i; ++j )
    printf("%d %d\n", opf[j], opt[j]);
  return EXIT_SUCCESS;
}


#include<stdio.h>
int min(int a[],int n){
int x,k=0,i;
x=a[0];
for(i=0;i<n;i++){
    if(a[i]<x){
        x=a[i];
        k=i;
    }
}
return k;
}
int max(int a[],int n){
int y=0,l=0,i;
for(i=0;i<n;i++){
    if(a[i]>y){
        y=a[i];
        l=i;
    }
}
return l;
}
int main(){
int n,k,i,count=0,steps=0;
scanf("%d %d",&n,&k);
int a[n],b[n],s,m;
for(i=0;i<n;i++){
    scanf("%d",&a[i]);
    b[i]=a[i];
}
//printf("%d %d\n",max(a,n),min(a,n));
s=a[max(a,n)]-a[min(a,n)];
while(steps<k){
      if(s==0||s==1){
        break;
      }
    a[max(a,n)]--;
    a[min(a,n)]++;
    steps++;
    //count++;
    s=a[max(a,n)]-a[min(a,n)];
}
printf("%d %d\n",s,steps);
for(m=0;m<steps;m++){
    printf("%d %d\n",max(b,n)+1,min(b,n)+1);
    b[max(b,n)]--;
    b[min(b,n)]++;
}
return 0;
}


#include<stdio.h>
int top[101];
int res[101];
int max, min;
int n, k;
void find()
{
	int i;
	max=0;
	min=0;
	for(i=0;i<n;i++)
	{
		if(top[max]<top[i])
			max=i;
		if(top[min]>top[i])
			min=i;
	}
}
int main()
{
	int i,count=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&top[i]);
	while(count<k)
	{
		find();
		if(max==min || top[max]-top[min]<=1)
			break;
		res[count]=max*1000+min;
		top[max]--;
		top[min]++;
		count++;
	}
	//find();
	printf("%d %d\n",top[max]-top[min], count);
	for(i=0;i<count;i++)
		printf("%d %d\n",res[i]/1000+1, res[i]%1000+1);
	return 0;
}


#include<stdio.h>
int top[101];
int res[1001];
int n, k;
int find_M()
{
	int i, pos, max=-1;
	for(i=0;i<n;i++)
		if(max<top[i])
		{
			max=top[i];
			pos=i;
		}
	return pos;
}
int find_m()
{
	int i, pos, min=101;
	for(i=0;i<n;i++)
		if(min>top[i])
		{
			min=top[i];
			pos=i;
		}
	return pos;
}
int main()
{
	int i, count=0, start, end;
	scanf("%d %d", &n, &k);
	for(i=0;i<n;i++)
		scanf("%d", &top[i]);
	while(count<k)
	{
		start=find_M();
		end=find_m();
		//printf("s:%d e:%d\n", start, end);
		if(start==end|| top[start]-top[end]==1)
			break;
		top[start]--;
		top[end]++;
		res[count]=start*1000+end;
		count++;
	}
	printf("%d %d\n",top[find_M()]-top[find_m()], count);
	for(i=0;i<count;i++)
		printf("%d %d\n", res[i]/1000+1, res[i]%1000+1);
 	return 0;
}


#include<stdio.h>
int top[101];
int res[101];
int max, min;
int n, k;
void find()
{
	int i;
	max=0;
	min=0;
	for(i=1;i<n;i++)
	{
		if(top[max]<top[i])
			max=i;
		if(top[min]>top[i])
			min=i;
	}
}
int main()
{
	int i,count=0;
	scanf("%d %d",&n,&k);
	for(i=0;i<n;i++)
		scanf("%d",&top[i]);
	while(count<k)
	{
		find();
		if(max==min || top[max]-top[min]<=1)
			break;
		res[count]=max*1000+min;
		top[max]--;
		top[min]++;
		count++;
	}
	find();
	printf("%d %d\n",top[max]-top[min], count);
	for(i=0;i<count;i++)
		printf("%d %d\n",res[i]/1000+1, res[i]%1000+1);
	return 0;
}


#include<stdio.h>
int top[101];
int res[1001];
int n, k, start, end;
void find()
{
	int i;
	start = 0;
	end = 0;
	for(i=1;i<n;i++)
	{
		if(top[start]<top[i])
		{
				start=i;
		}
		if(top[end]>top[i])
		{
				end=i;
		}
	}
}
int main()
{
	int i, count=0;
	scanf("%d %d", &n, &k);
	for(i=0;i<n;i++)
		scanf("%d", &top[i]);
	while(count<k)
	{
		find();
		//printf("s:%d e:%d\n", start, end);
		if(start==end|| top[start]-top[end]==1)
			break;
		res[count]=start*1000+end;
		top[start]--;
		top[end]++;
		count++;
	}
	find();
	printf("%d %d\n",top[start]-top[end], count);
	for(i=0;i<count;i++)
		printf("%d %d\n", res[i]/1000+1, res[i]%1000+1);
 	return 0;
}


#include<stdio.h>
int top[101];
int val[101];
int res[1001];
int n, k;
int find(int num)
{
	int i;
	for(i=0;i<n;i++)
		if(num==top[i])
			return i;
	return 0;
}
int find_M()
{
	int i, pos, max=-1;
	for(i=0;i<n;i++)
		if(max<top[i])
		{
			max=top[i];
			pos=i;
		}
	return pos;
}
int find_m()
{
	int i, pos, min=101;
	for(i=0;i<n;i++)
		if(min>top[i])
		{
			min=top[i];
			pos=i;
		}
	return pos;
}
int main()
{
	int i, count=0, start, end;
	scanf("%d %d", &n, &k);
	for(i=0;i<n;i++)
	{
		scanf("%d", &top[i]);
		val[top[i]]++;
	}
	start=find_M();
	end=find_m();
	while(count<k)
	{

		//printf("s:%d e:%d\n", start, end);
		if(start==end|| top[start]-top[end]==1)
			break;
		res[count]=start*1000+end;
		val[top[start]]--;
		top[start]--;
		val[top[start]]++;
		if(val[top[start]+1]>0)
			start=find(top[start]+1);
		val[top[end]]--;
		top[end]++;
		val[top[end]]++;
		if(val[top[end]-1]>0)
			end=find(top[end]-1);
		count++;
	}
	printf("%d %d\n",top[find_M()]-top[find_m()], count);
	for(i=0;i<count;i++)
		printf("%d %d\n", res[i]/1000+1, res[i]%1000+1);
 	return 0;
}


#include<iostream>
using namespace std;

void main()
{
	int sum=0,x;
	cin>>x;

	for (int i=0 ;i<x ;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		if (a+b+c>+2)
			sum++;
	}
	cout<<sum;

}


#include<iostream>
using namespace std;

void main()
{
	int sum=0,x;
	cin>>x;

	for (int i=0 ;i<x ;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		if (a+b+c>=2)
			sum++;
	}
	cout<<sum+1;

}


#include<iostream>
using namespace std;

void main()
{
	int sum=0,x;
	cin>>x;

	for (int i=0 ;i<x ;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		if (a+b+c>=2)
			sum++;
	}
	cout<<sum;

}


#include<iostream>
using namespace std;

void main()
{
	int sum=0,x;
	cin>>x;

	for (int i=0 ;i<x ;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		if (a+b+c>+2)
			sum++;
	}
	cout<<sum+1;

			system("pause");

}


#include <iostream>
using namespace std;
void khalid(int p)
{
    switch(p)
    {
    case 111: cout << 3<<endl;
    return;
    case 110: cout << 2<<endl;
    return;
    case 100: cout << 1<<endl;
    return;
    }
}
int main()
{
   int n;
   cin>>n;
   int q;
   for(int x=0;x<n;x++)
   {
       cin>>q;
       khalid(q);

   }

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
int n,m,l,z,x,s=0;
cin>>x;
while(x>=1)
{
cin>>n>>m>>l;
if((n+m+l)>=2)
++s;
--x;
}
cout<<s;
}


#include <iostream>
using namespace std;
void khalid(int p)
{
    if(p==1)
    cout << "1";
    else
    cout<<"2";
}
int main()
{
   int n;
   cin>>n;
   int q;
   for(int x=0;x<n;x++)
   {
       cin>>q;
       khalid(q);

   }

    return 0;
}


#include <iostream>
using namespace std;
void khalid(int p)
{
    if(q==1)
    cout << "1" << std::endl;
    else
    cout<<"2"
}
int main()
{
   int n;
   cin>>n;
   int q;
   for(int x=0;x<n;x++)
   {
       cin>>q;
       khalid(q);

   }

    return 0;
}


#include <iostream>
using namespace std;
void khalid(int p)
{
    if(p<=1)
   if(p<=10)
   if(p<=100)
   cout<<"1";
   else
   cout<<"2";
}
int main()
{
   int n;
   cin>>n;
   int q;
   for(int x=0;x<n;x++)
   {
       cin>>q;
       khalid(q);

   }

    return 0;
}


#include <iostream>
using std::cout;
using std::cin;

void main()
{
	int n, k;
	bool x, y, z;

	for (int i = 1; i <= n; i++)
	{
		cin >> x >> y >> z;
		if ((x || y) && (x || z) && (y || z)) k++;
	}
	cout << k;
}


#include <iostream>
using std::cout;
using std::cin;

void main()
{
	int n, k;
	bool x, y, z;

	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> x >> y >> z;
		if ((x || y) && (x || z) && (y || z)) k++;
	}
	cout << k;
}


#include <iostream>
using std::cout;
using std::cin;

void main()
{
	int n, k = 0;
	bool x, y, z;

	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> x >> y >> z;
		if ((x || y) && (x || z) && (y || z)) k++;
	}
	cout << k;
}


#include <iostream>
using namespace std;

void main()
{
	int n, i, k, x, y, z;
	i = 1;
	cin >> n;

	for (; i <= n; i++)
	{
		cin >> x >> y >> z;
		if (!((x + y + z) <= 1)) k++;
	}
	cout << k;
}


#include <iostream>
//#include <bitset>
using namespace std;

void main()
{
	int n, k = 0;
	cin >> n;
	//bitset <3> bit;
	bool x, y ,z;

	for (int i = 1; i <= n; i++)
	{
		cin >> x >> y >> z;
		if (!((x + y + z) <= 1)) k++;
	}
	cout << k;
}


#include <iostream>
#include <bitset>
using namespace std;

void main()
{
	int n, k = 0, kk = 0;
	cin >> n; cout << endl;
	//bitset <3> bit;
	bool f;

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cin >> f; cout << " ";
			//bit[j] = f;
			if (f) k++;
		}
		if (k >= 2) kk++;
		k = 0;
	}
	cout << kk;
}


#include "iostream"
using namespace std;
int main (void){
    int n,mat[10][3],c=0 ,d=0 ;cin >>n;
    for (int i=0 ; i<n ; j++)
    for (int j=0 ; j<3 ;j++){
        c+=mat[i][j];
        if (c>=2)
        d+=1;
        c=0;
}
    cout<< d<<endl;
}


#include <iostream>
using namespace std;
int main (){
    int n , ans = 0;cin>>n;
    for (int i = 0;i<n;i++){
    int x,y,z ;cin >>x>>y>>z;
    ans+=(x+y+z>=2);
}
    cout << ans <<endl;
    return 0;
}


#include "stdafx"
#include "iostream"
using namespace std;
int main (void){
    int n,mat[10][3],c=0 ,d=0 ;cin >>n;
    for (int i=0 ; i<n ; j++){
    for (int j=0 ; j<3 ;j++){
        c+=mat[i][j];}
        if (c>=2)
        d+=1;
        c=0;
}
    cout<< d<<endl;
}


#include "stdafx.h"
#include "iostream"
using namespace std;
int main (void){
    int n,mat[10][3],c=0 ,d=0 ;cin >>n;
    for (int i=0 ; i<n ; j++)
    for (int j=0 ; j<3 ;j++){
        c+=mat[i][j];
        if (c>=2)
        d+=1;
        c=0;
}
    cout<< d<<endl;
}


#include "iostream"
using namespace std;
int main (void){
    int n,mat[10][3],c=0 ,d=0 ,i,j;cin >>n;
    for ( i=0 ; i<n ; j++)
    for ( j=0 ; j<3 ;j++){
        c+=mat[i][j];
        if (c>=2)
        d+=1;
        c=0;
}
    cout<< d<<endl;
}


#include <stdafx>
#include <iostream>
using namespace std;
int main (void){
    int n,mat[10][3],c=0 ,d=0 ;cin >>n;
    for (int i=0 ; i<n ; j++)
    for (int j=0 ; j<3 ;j++){
        c+=mat[i][j];
        if (c>=2)
        d+=1;
        c=0;
}
    cout<< d<<endl;
}


#include "stdafx.h"
#include "iostream"
using namespace std;

int main(void)
{
	int n , mat[10][3],c=0,d=0;cin >> n;

	for (int i=0 ;i<n ;i++)
		for (int j=0 ;j<3 ;j++)
			cin >> mat[i][j];

	for (int i=0 ;i<n ;i++){
		for (int j=0 ;j<3 ;j++){
			c+=mat [i][j];}
		if(c>=2)
			d+=1;
		c=0;
}
	cout<< d << endl;
}


#include <iostream>
using namespace std;
int main (){
    int n , ans =0;cin>>n;
    for (int i =0;i<n;i++){
    int x,y,z ;cin >>x>>y>>z;
    ans+ =(x+y+z >=2);
}
    cout << ans <<endl;
    return 0;
}


#include <iostream>
using namespace std;
void main()
{
	int num, f, t, s, count=0;
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		cin >> f >> s >> t;
		if (f == 1 && s == 1)
		{
			count ++ ;

		}
		else if (f == 1 && t == 1)
		{
			count++;
		}
		else if (s == 1 && t == 1)
		{
			count++;
		}

	}

	cout << count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count=0;
	cin >> num;
	for (int i = 0;i< num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1  )
	    {
	    count++;

	    }else if(f==1 && t==1)
	    {
             count++;
       }
	    else if(s==1 && t==1){
count++;
}

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1|| f==1 && t==1 ||s==1 && t==1)
	    {
	    count++;

	    }
	}
cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1|| f==1 && s==1 ||s==1 && t==1)
	    {
	    count++;

	    }

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1  )
	    {
	    count++;

	    }else if(f==1 && t==1)
	    {
	        count++;

	    }
	    else if(s==1 && t==1)

	    {
	        count++;

	    }

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count=0;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1  )
	    {
	    count++;

	    }else if(f==1 && t==1)
	    {
count++;
}
	    else if(s==1 && t==1){
count++;
}

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num ;
	for (int i = 0;i < = num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1&&s==1)
	    {
	    count++;
	    }else if (f==1&&s==1)
	    {
	          count++;
	    }
	        else if (s==1&&t==1)
	        {

	            count ++;

	        }

	    }

	}

cout << count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1)
	    {
	    count++;
	    }
else if (f==1 && t==1)
{
    count ++;

}
else if(s==1 && t==1)
{
    count ++;

}
	    }

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1)
	    {
	    count++;
else if (f==1 && t==1)
{
    count ++;

}
else if(s==1 && t==1)
{
    count ++;

}
	    }

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1|| f==1 && t==1 ||s==1 && t==1)
	    {
	    count++;

	    }

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1  )
	    {
	    count++;

	    }else if(f==1 && t==1)
	    { count++;}
	    else if(s==1 && t==1){ count++;}

	}

cout <<count;

}


#include <iostream>
using namespace std;
void main()
{
	int num,f,t,s,count=0;
	cin >> num;
	for (int i = 0;i<=num ;i++)
	{
	    cin >>f>>s>>t;
	    if (f==1 && s==1  )
	    {
	    count++;

	    }else if(f==1 && t==1)
	    {
             count++;
       }
	    else if(s==1 && t==1){
count++;
}

	}

cout <<count;

}


#include <iostream>
#include <iomanip>

using namespace std;

int main(){

	int kol_ex;
	int** arr;
	int count = 0, sum = 0;

	cin >> kol_ex;
	arr = new int*[kol_ex];
	for(int i = 0; i <= kol_ex; i++)
		arr[i] = new int[3];

	 for ( int i = 0; i < kol_ex; i++) // ввод
        for (int j = 0; j < 3; j++)
        {
            cin >> arr[i][j];
        }

       for ( int i = 0; i < kol_ex; i++) {
            for (int j = 0; j < 3; j++)
            {
                if( arr[i][j] == 1) count +=1;

            }
			if (count >= 2) sum +=1;
			count = 0;
	   }
            cout << sum << endl;

}


#include <iostream>
#include <iomanip>

using namespace std;

int main(){

	int kol_ex;
	int** arr;
	int count = 0, sum = 0;

	cin >> kol_ex;
	arr = new int*[kol_ex];
	for(int i = 0; i <= kol_ex; i++)
		arr[i] = new int[3];

	 for ( int i = 0; i < kol_ex; i++) // ввод
        for (int j = 0; j < 3; j++)
        {
            cin >> arr[i][j];
        }

	cout << endl; // вывод
       for ( int i = 0; i < kol_ex; i++) {
            for (int j = 0; j < 3; j++)
            {
                if( arr[i][j] == 1) count +=1;
            }
			if (count >= 2) sum +=1;
			count = 0;
	   }
            cout << sum << endl;

}


#include <iostream>
#include <iomanip>

using namespace std;

int main(){

	int kol_ex;
	int** arr;
	int count = 0, sum = 0;

	cin >> kol_ex;
	arr = new int*[kol_ex];
	for(int i = 0; i <= kol_ex; i++)
		arr[i] = new int[3];

	 for ( int i = 0; i < kol_ex; i++)
        for (int j = 0; j < 3; j++)
        {
            cin >> arr[i][j];
        }

       for ( int i = 0; i < kol_ex; i++) {
            for (int j = 0; j < 3; j++)
            {
                if( arr[i][j] == 1) count +=1;
			}
			if (count >= 2) sum +=1;
			count = 0;
	   }
            cout << sum << endl;

}


#include <iostream>
#include <iomanip>

using namespace std;

int main(){

	int kol_ex;
	int** arr;
	int count = 0, sum = 0;

	cin >> kol_ex;
	arr = new int*[kol_ex];
	for(int i = 0; i <= kol_ex; i++)
		arr[i] = new int[3];

	 for ( int i = 0; i < kol_ex; i++)
        for (int j = 0; j < 3; j++)
        {
            cin >> arr[i][j];
        }

       for ( int i = 0; i < kol_ex; i++) {
            for (int j = 0; j < 3; j++)
            {
                if( arr[i][j] == 1) count +=1;
			}
			if (count >= 2) sum +=1;
			count = 0;
	   }
            cout << sum << endl;
		system ("pause");
}


#include <iostream>
#include <iomanip>

using namespace std;

int main(){

	int kol_ex;
	int** arr;
	int count = 0, sum = 0;

	cin >> kol_ex;
	arr = new int*[kol_ex];
	for(int i = 0; i < kol_ex; i++)
		arr[i] = new int[3];

	 for ( int i = 0; i < kol_ex; i++)
        for (int j = 0; j < 3; j++)
        {
            cin >> arr[i][j];
        }

       for ( int i = 0; i < kol_ex; i++) {
            for (int j = 0; j < 3; j++)
            {
                if( arr[i][j] == 1) count +=1;
			}
			if (count >= 2) sum +=1;
			count = 0;
	   }
            cout << sum << endl;

}


# include <iostream>
using namespace std;
int main()
{
       int n,d=0;

       cin>>n;
       for(int s=0;s<n;n++)
       { int a,b,c;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))
       d++;

       }
       cout<<d;
       return 0;
}


# include <iostream>
using namespace std;
int main()
{
       int n,d=0;

       cin>>n;
       for(int s=0;s<n;s++)
       { int a,b,c;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))
       d++;

       }
       cout<<d;
       return 0;
}


# include <iostream>
using namespace std;
int main()
{
       int n;
   int a,b,c;
       cin>>n;
       for(ints=0;s<n;n++)
       { int a,b,c,d;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))
       d++;

       }
       cout<<d;
       return 0;
}


# include <iostream>
using namespace std;
int main()
{
       int n;
   int a,b,c;
       cin>>n;
       for(int c=0;c<n;n++)
       { int a,b,c;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))

       }
       return 0;
}


# include <iostream>
using namespace std;
int main()
{
       int n;

       cin>>n;
       for(int s=0;s<n;n++)
       { int a,b,c,d;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))
       d++;

       }
       cout<<d;
       return 0;
}


# include <iostream>
using namespace std;
int main()
{
       int n;
   int a,b,c;
       cin>>n;
       for(int s=0;c<n;n++)
       { int a,b,c;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))

       }
       return 0;
}


# include <iostream>
using namespace std;
int main()
{
       int n;
   int a,b,c;
       cin>>n;
       for(int c=0;c<n;n++)
       { int a,b,c,d;
       cin>>a>>b>>c;
       if((a==1&&b==1)||(b==1&&c==1)||(a==1&&c==1))
       d++;

       }
       return 0;
}


#include <iostream>

int main ()
{
    int number;
    std::cin >> number;
    int count;
    int x;
    int y;
    int z;
    for (int i=0; i<number; i++)
        std::cin >> x;
        std::cin >> y;
        std::cin >> z;
        int sum = x+y+z;
        if (sum >= 2)
            count += 1;

    std::cout << count;
    return 0;
}


#include <iostream>

int main ()
{
    int number;
    std::cin >> number;
    int count;
    int x;
    int y;
    int z;
    for (int i=0; i<number; i++)
        std::cin.ignore(1, ' ') >> x;
        std::cin.ignore(1, ' ') >> y;
        std::cin.ignore(1, '\n') >> z;
        int sum = x+y+z;
        if (sum >= 2)
            count += 1;

    std::cout << count;
    return 0;
}


#include <iostream>

int main ()
{
    int number;
    std::cin >> number;
    int count;
    int x;
    int y;
    int z;
    for (int i=0; i<number; i++){
        std::cin >> x;
        std::cin >> y;
        std::cin >> z;
        int sum = x+y+z;
        if (sum >= 2)
            count += 1;
    }
    std::cout << count;
    return 0;
}


#include <iostream>

int main ()
{
    int number;
    std::cin >> number;
    int count;
    int x;
    int y;
    int z;
    for (int i=0; i<number; i++)
        std::cin.ignore(1, " ") >> x;
        std::cin.ignore(1, " ") >> y;
        std::cin.ignore(1, "\n") >> z;
        int sum = x+y+z;
        if (sum >= 2)
            count += 1;

    std::cout << count;
    return 0;
}


#include <iostream>

int main ()
{
    int number;
    std::cin >> number;
    int count;
    int x;
    int y;
    int z;
    for (int i=0; i<number; i++)
        std::cin.ignore(1) >> x;
        std::cin.ignore(1) >> y;
        std::cin.ignore(1) >> z;
        int sum = x+y+z;
        if (sum >= 2)
            count += 1;

    std::cout << count;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,P,V,T,r=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{ cin>>P>>V>>T;
		r+=(P+V+T)>=2;
		cout<<r<<endl;

	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,P,V,T,r=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{ cin>>P>>V>>T;
		r+=(P+V+T)>=2;
		}

	cout<<r<<endl;

	return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n, petya, vasya, tonya, counter = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> petya >> vasya >> tonya;
        if (petya + vasya + tonya >= 2)
        {
            counter++;
        }
    }
    cout << counter;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n, ans=0;
	cin>> n;
	for(int i=0;i<n;i++)
	{
		int x, y, z;
		cin>> x>>y>> z;
		//ans+=(x+y+z>=2);
		if(x+y+z>=2)
		ans++;
	cout<< ans<< endl;
	}

return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n, P, V, T, r=0;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
	    cin >> P >> V >>T;
		r+=(P+V+T)>=2;

	}

	cout << r<< endl;

	return 0;
}


#include<stdio.h>
int main()
{
    int x,y,z;
    int n,sum;
    while(scanf("%d",&n)!=EOF)
    {
        sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            sum+=x+y+z>=2?1:0;
        }
        printf("%d\n",sum);
    }
    return 0;
}


#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
using namespace std;
int main() {
	int a[1005], b[1005], c[1005], n, i, x;
	cin >> n; x = 0;
	for(i=1;i<=n;i++){
		cin >> a[i] >> b[i] >> c[i];
	}
	for (i = 1; i <= n; i++) {
		if ((a[i] + b[i] + c[i]) >= 2)x++;
	}
	cout << x;
	return 0;
}


#include<stdio.h>
void main(void)
{
	int a , b , c , d ,e ;
	scanf("%d",&a);
	e=0;
		while(a--){
			scanf("%d %d %d",&d,&b,&c);
			if(d+b+c>=2)
				e+=1;
		}
		printf("%d",e);
}


#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main()
{
	int a,b,c,d,e=0,f;
	scanf("%d",&a);
	while(a--)
	{f=0;
		scanf("%d%d%d",&b,&c,&d);
		if(b==1)
			f++;
		if(c==1)
			f++;
		if(d==1)
			f++;
		if(f>=2)
			e++;
	}
	printf("%d\n",e);
}


#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
void main()
{
	 int i,j,k,s1,s2;
	 int m,n,z,t,x=0;
	 int a[5];
	 scanf("%d",&t);
	 for(i=1;i<=t;++i)
	 {
		 z=0;
	    for(j=0;j<3;++j)
		{
			scanf("%d",&a[j]);
				if(a[j]==1)
					z++;
		}
		if(z>=2)
			x++;
	 }
	 printf("%d\n",x);
}


#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	int n,k=0;
	cin>>n;

	for (int i=0; i<n; i++)
	{
		int k1=0;
		int s;
		for (int j=0;j<3;j++) { cin>>s; k1+=s; }
		if (k1>1) k++;
	}
	cout<<k;
}


// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);

	int n,k=0;
	cin>>n;

	for (int i=0; i<n; i++)
	{
		int k1=0;
		int s;
		for (int j=0;j<3;j++) { cin>>s; k1+=s; }
		if (k1>1) k++;
	}
	cout<<k;
}


#include <iostream>
using namespace std;

int main()
{
	int n,k=0;
	cin>>n;

	for (int i=0; i<n; i++)
	{
		int k1=0;
		int s;
		for (int j=0;j<3;j++) { cin>>s; k1+=s; }
		if (k1>1) k++;
	}
	cout<<k;
}


#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
	int n,k=0;
	cin>>n;

	for (int i=0; i<n; i++)
	{
		int k1=0;
		int s;
		for (int j=0;j<3;j++) { cin>>s; k1+=s; }
		if (k1>1) k++;
	}
	cout<<k;
}


#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{

	int n,k=0;
	cin>>n;

	for (int i=0; i<n; i++)
	{
		int k1=0;
		int s;
		for (int j=0;j<3;j++) { cin>>s; k1+=s; }
		if (k1>1) k++;
	}
	cout<<k;
}


#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
		int n;
		int l = 0;
		int sum = 0;
		int sum1 = 0;
		int a[100000];
		cin >> n;

		for (int i = 0; i < n * 3; i++)

			{
				cin >> l;
				a[i] = l;
			}
		for (int i = 0; i < n * 3; i++)

		{
			if (a[i] == 1)
				sum1++;
			if (i % 2 == 0 && i != 0)
			{
				if (sum1 > 1)
					sum++;
				sum1 = 0;
			}
		}

		cout << sum;

		return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
		int n;
		int l = 0;
		int sum = 0;
		int sum1 = 0;
		int a[10];
		cin >> n;

		for (int i = 0; i < n * 3; i++)

			{
				cin >> l;
				a[i] = l;
			}
		for (int i = 0; i < n * 3; i++)

		{
			if (a[i] == 1)
				sum1++;
			if (i % (n - 1) == 0)
			{
				if (sum1 > 1)
					sum++;
				sum1 = 0;
			}
		}

		cout << sum;
		return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
		int n;
		int l = 0;
		int sum = 0;
		int sum1 = 0;
		int a[10];
		cin >> n;

		for (int i = 0; i < n * 3; i++)

			{
				cin >> l;
				a[i] = l;
			}
		for (int i = 0; i < n * 3; i++)

		{
			if (a[i] == 1)
				sum1++;
			if (i % 2 == 0)
			{
				if (sum1 > 1)
					sum++;
				sum1 = 0;
			}
		}

		cout << sum;
		return 0;
}


#include <iostream>
#include <string>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
		int n;
		int l = 0;
		int sum = 0;
		int sum1 = 0;
		int a[1000];
		cin >> n;

		for (int i = 1; i <= n * 3; i++)

			{
				cin >> l;
				a[i] = l;
			}
		for (int i = 1; i <= n * 3; i++)

		{
			if (a[i] == 1)
				sum1++;
			if (i % 3 == 0 && i != 0)
			{
				if (sum1 > 1)
					sum++;
				sum1 = 0;
			}
		}

		cout << sum;

		return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
		int n;
		int l = 0;
		int sum = 0;
		int sum1 = 0;
		int a[100000];
		cin >> n;

		for (int i = 1; i <= n * 3; i++)

			{
				cin >> l;
				a[i] = l;
			}
		for (int i = 1; i <= n * 3; i++)

		{
			if (a[i] == 1)
				sum1++;

			if (i % 3 == 0)
			{
				if (sum1 > 1)
					sum++;
				sum1 = 0;
			}
		}

		cout << sum;

		return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctype.h>
#include <algorithm>

using namespace std;

int main() {
		int n;
		int l = 0;
		int sum = 0;
		int sum1 = 0;
		int a[1000000];
		cin >> n;

		for (int i = 0; i < n * 3; i++)

			{
				cin >> l;
				a[i] = l;
			}
		for (int i = 0; i < n * 3; i++)

		{
			if (a[i] == 1)
				sum1++;
			if (i % 2 == 0 && i != 0)
			{
				if (sum1 > 1)
					sum++;
				sum1 = 0;
			}
		}

		cout << sum;
		system("pause");
		return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
using namespace std;

int main() {

    int p;
    cin>>p;
    int a,b,c;
    int count=0;
    for(int i=0; i<p; i++)
    {
        cin>>a>>b>>c;

        if((a+b+c)>1)
            count++;
    }

    cout<<count;

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    string choices;
    int n;
    int count=0;

    cin>>n;

    for (int i=0; i<n; i++)
    {
        cin>>choices;
        if((choices.at(0)=='1') && (choices.at(2)=='1') && (choices.at(4)=='1'))
            count++;
    }
    cout<<count;

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    string choices;
    int N;
    int count=0;

    cin>>N;

    for (int i=0; i<N; i++)
    {
        cin>>choices;
        if((choices.at(0)=='1') && (choices.at(1)=='1') && (choices.at(2)=='1'))
            count++;
    }
    cout<<count;
        return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    string choices;
    int n;
    int count=0;

    cin>>N;

    for (int i=0; i<n; i++)
    {
        cin>>choices;
        if((choices.at(0)=='1') && (choices.at(2)=='1') && (choices.at(4)=='1'))
            count++;
    }
    cout<<count;

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
	string choices;
	int N;
	int count=0;

	cin>>N;

	for (int i=0; i<N; i++)
	{
		cin>>choices;
		if((choices.at(0)=='1') && (choices.at(1)=='1') && (choices.at(2)=='1'))
			count++;
	}
	cout<<count;
	return 0;
}


#include <iostream>
#include <string>
using namespace std;

int main()
{
    string choices;
    int n;
    int count=0;
    cin>>n;
    for (int i=0; i<n; i++)
    {
    cin>>choices;
    if((choices.at(0)=='1') && (choices.at(2)=='1') && (choices.at(4)=='1'))
    count++;
    }
    return 0;
}


#include <iostream>
using namespace std;
int main(){
	int n,a=0,b=0,answer;
	cin>>n;
	for(int i=1; i<=n; i++)
	{
		a=0;
		for(int j=1; j<=3; j++){
			cin>>answer;
			if(answer==1)
				a++;
		}
		if(a>=2)
			b++;
	}
	cout<<b;
	return 0;
}


#include<iostream>
#include<conio.h>
using namespace std;
int main(){
	int n,a=0,b=0,g;
	cin>>n;
	for(int i=0;i<n;i++)
	{
	for(int j=0;j<3;j++){
		cin>>g;
	if(g==1)
		a++;
	}
	if(a>2)
		b++;
	}
	cout<<b;
	_getch();

}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n;
    cin>>n;
    int arr[n+2];
    for(int i =1 ; i<=n ; i++)cin>>arr[i];
    arr[0]=arr[n];
    arr[n+1]=arr[1];
    for(int i =1 ; i<=n ; i++)
    {

        if (arr[i]>arr[i+1]&&arr[i]<arr[i-1])return cout<<-1,0;
    }int res=0;
    for(int i =n ; i>1 ; i-- )
    {
        if (arr[i]<arr[i-1])res++;
    }
    cout<<res;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n ;
    cin>>n;
    int arr[n];
    for(int i =0 ; i<n ; i++)
    {
        cin>>arr[i];
    }
    if (n==2)
    {
        if (arr[0]>arr[1])return cout<<1,0;
        else return cout<<0,0;
    }
    bool aa=1,bb=0;
    int b=0;

    int a=0;
    int h=n-1;
    for( int i =0 ; i<n-1 ; i++)
    {

         if (arr[i]>arr[i+1])
        {
            //cout<<arr[i]<<" "<<arr[i+1]<<endl;
            h=i;
        }

    }
    for(int i =0 ; i <h ; i++)
    {
        if (arr[i]>arr[i+1])return cout<<-1,0;
    }
    for(int i =h+1 ; i<n-1  ;i++)
    if (arr[i]>arr[i+1])return cout<<-1,0;
    if (arr[n-1]>arr[0])return cout<<-1,0;

cout<<n-h-1;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n ;
    cin>>n;
    int arr[n];
    for(int i =0 ; i<n ; i++)
    {
        cin>>arr[i];
    }
    if (n==2)
    {
        if (arr[0]>arr[1])return cout<<1,0;
        else return cout<<0,0;
    }
    bool aa=1,bb=0;
    int b=0;
    int i;
    int a=0;
    int h=n-1;
    for( i =0 ; i<n-1 ; i++)
    {
        if (arr[i]<arr[i+1])
        {
            if (bb)
            a++;
            aa=1;bb=0;
        }
        else if (arr[i]>arr[i+1])
        {
            //cout<<arr[i]<<" "<<arr[i+1]<<endl;
            if (aa){b++;
            h=i;
            }
            bb=1;aa=0;
        }

    }
     if (arr[i]<arr[0])
        {
            if (bb&&a)return cout<<-1,0;
        }
    if (arr[i]>arr[0])
        {
           // cout<<arr[i]<<" "<<arr[0]<<endl;
           b++;

        }
       // cout<<b<<endl;
    if (b>1)return cout<<-1,0;

cout<<n-h-1;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

int main()
{
    int n;
    cin>>n;
    int arr[n+2];
    for(int i =1 ; i<=n ; i++)cin>>arr[i];
    arr[0]=arr[n];
    arr[n+1]=arr[1];
    int res;
    for(int i =1 ; i<=n ; i++)
    {

        if (arr[i]>arr[i+1])res=i;
        if (arr[i]>arr[i+1]&&arr[i]<arr[i-1])return cout<<-1,0;

    }
    cout<<n-res;

    return 0;
}