#include <iostream>
using namespace std;
#define MAX 100001
struct node {
int ID, team;
node *prev,*next;
} val [MAX];
struct linklist {
node h,t;
} team [6][6];
void connect (node *a, node *b){
a->next = b;
b->prev = a;
}
void merge (linklist *flist, linklist *mergelist){
if (mergelist->h.next == &mergelist ->t) return;
connect (flist->t.prev , mergelist->h.next);
connect (mergelist->t.prev,&flist->t);
connect (&mergelist->h,&mergelist->t);
}
int newSore (int score){
if (score >= 5) return 5;
else if (score <= 1) return 1;
return score;
}
void init(){
for (int i = 1; i <= 5; i++)
{
for (int j = 1 ; j <=5 ; j++)
{
connect (&team [i][j].h,&team [i][j].t);
}
}
}
void hire(int mID, int mTeam, int mScore){
val [mID].ID = mID;
val [mID].team = mTeam;
connect(&val[mID], team [mTeam][mScore].h.next);
connect(&team [mTeam][mScore].h, &val [mID]);
}
void fire(int mID){
connect (val [mID].prev, val[mID].next);
}
void updateSoldier(int mID, int mScore){
int t = val [mID].team;
fire (mID);
hire (mID,t,mScore);
}
void updateTeam(int mTeam, int mChangeScore){
if (mChangeScore > 0){
for (int i = 4 ; i >= 1 ; i--){
merge (&team [mTeam][newSore (i + mChangeScore)],&team[mTeam][i]);
}
}
else if (mChangeScore < 0){
for (int i = 2 ; i <= 5 ; i++){
merge (&team [mTeam][newSore (i + mChangeScore)],&team[mTeam][i]);
}
}
}
int bestSoldier(int mTeam){
int score = 5;
while (score > 0){
linklist *te = & team[mTeam][score--];
int maxID = 0;
node *tmp = te ->h.next;
while (tmp != &te ->t){
if (maxID < tmp->ID){
maxID = tmp->ID;
}
tmp = tmp->next;
}
if (maxID !=0) return maxID;
}
}