### Saratov State University :: Online Contester

::Go
- home
- news
- register
 - update personal info
 - problemset archive
- submit
- status online
- standing
- contests
- virtual contests
- forum
- statistic
- FAQ
- projects

 ::Poll Are you registered on Codeforces?YesNoWhat is it???[results]

::webboard

Author: Night Fox
ID: 060233
Problem: 138
Contest: 0
Date: 2013-11-10 18:36:40

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

#define F first
#define S second

const int MAXN=10000+10;

int n;
int t;
int x;
pair <int,int> a[MAXN];
int b[MAXN][2];

void input()
{
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i].F;
a[i].S=i;
t+=a[i].F;
}
t/=2;
for(int i=0;i<t;i++)
b[i][1]=-1;
}

void solve()
{
sort(a,a+n);
int s=0,p=0;
for(int i=n-1;i>=0;i--){
if(s<t && a[i].F!=1){
while(s<t && a[i].F>1){
b[s][0]=a[i].S;
a[i].F--;
s++;
}
if(a[i].F==1 && s<t){
b[s][1]=a[i].S;
a[i].F--;
}
else{
while(a[i].F>0){
if(b[x][1]==-1){
b[x][1]=a[i].S;
a[i].F--;
}
x++;
}
}
}
else
while(a[i].F>0){
if(b[p][1]==-1){
b[p][1]=a[i].S;
a[i].F--;
}
p++;
}
}
cout<<t<<endl;
for(int i=0;i<t;i++)
cout<<b[i][0]+1<<" "<<b[i][1]+1<<endl;
}

int main()
{
input();
solve();
return 0;
}

4
3 1 1 1

i think you should change it