#include <stdio.h> #include <string.h> struct node{ int dex; int exp; struct node *next; }a,b; node *Create(int n){ node *hp,*p=hp=new node,*temp; int dex,exp; while(n--){ scanf("%d%d",&dex,&exp); temp = new node; temp->dex=dex; temp->exp=exp; p->next=temp; p=temp; } p->next=NULL; return hp; } node *Add_ab(node *a,node *b){ node *ans,*p=ans=new node,*temp; while(a&&b){ if(a->exp>b->exp){ temp = new node; temp->dex=a->dex; temp->exp=a->exp; p->next=temp; p=temp; a=a->next; } else if(a->exp<b->exp){ temp = new node; temp->dex=b->dex; temp->exp=b->exp; p->next=temp; p=temp; b=b->next; } else if(a->dex+b->dex==0){ a=a->next; b=b->next; } else{ temp = new node; temp->dex=a->dex+b->dex; temp->exp=a->exp; p->next=temp; p=temp; a=a->next; b=b->next; } } while(a){ temp = new node; temp->dex=a->dex; temp->exp=a->exp; p->next=temp; p=temp; a=a->next; } while(b){ temp = new node; temp->dex=b->dex; temp->exp=b->exp; p->next=temp; p=temp; b=b->next; } p->next=NULL; return ans; } int main(){ int n,m; node *ha,*hb,*hc,*temp; scanf("%d%d",&n,&m); ha=Create(n); hb=Create(m); hc=Add_ab(ha->next,hb->next); temp = hc->next; while(temp){ printf("%d %d",temp->dex,temp->exp); temp=temp->next; } return 0; }