Problem:
H - RLE Replacement
Problem Statement
In JAG Kingdom, ICPC (Intentionally Compressible Programming Code) is one of the common programming languages. Programs in this language only contain uppercase English letters and the same letters often appear repeatedly in ICPC programs. Thus, programmers in JAG Kingdom prefer to compress ICPC programs by
Run Length Encoding
in order to manage very large-scale ICPC programs.
Run Length Encoding (RLE) is a string compression method such that each maximal sequence of the same letters is encoded by a pair of the letter and the length. For example, the string "RRRRLEEE" is represented as "R4L1E3" in RLE.
Now, you manage many ICPC programs encoded by RLE. You are developing an editor for ICPC programs encoded by RLE, and now you would like to implement a replacement function. Given three strings $A$, $B$, and $C$ that are encoded by RLE, your task is to implement a function replacing the first occurrence of the substring $B$ in $A$ with $C$, and outputting the edited string encoded by RLE. If $B$ does not occur in $A$, you must output $A$ encoded by RLE without changes.
Input
The input consists of three lines.
$A$
$B$
$C$
The lines represent strings $A$, $B$, and $C$ that are encoded by RLE, respectively. Each of the lines has the following format:
$c_1$ $l_1$ $c_2$ $l_2$ $\ldots$ $c_n$ $l_n$ \$
Each $c_i$ ($1 \leq i \leq n$) is an uppercase English letter (
A
-
Z
) and $l_i$ ($1 \leq i \leq n$, $1 \leq l_i \leq 10^8$) is an integer which represents the length of the repetition of $c_i$. The number $n$ of the pairs of a letter and an integer satisfies $1 \leq n \leq 10^3$. A terminal symbol
$
indicates the end of a string encoded by RLE. The letters and the integers are separated by a single space. It is guaranteed that $c_i \neq c_{i+1}$ holds for any $1 \leq i \leq n-1$.
Output
Replace the first occurrence of the substring $B$ in $A$ with $C$ if $B$ occurs in $A$, and output the string encoded by RLE. The output must have the following format:
$c_1$ $l_1$ $c_2$ $l_2$ $\ldots$ $c_m$ $l_m$ \$
Here, $c_i \neq c_{i+1}$ for $1 \leq i \leq m-1$ and $l_i \gt 0$ for $1 \leq i \leq m$ must hold.
Sample Input 1
R 100 L 20 E 10 \$
R 5 L 10 \$
X 20 \$
Output for the Sample Input 1
R 95 X 20 L 10 E 10 \$
Sample Input 2
A 3 B 3 A 3 \$
A 1 B 3 A 1 \$
A 2 \$
Output for the Sample Input 2
A 6 \$

Submitted solutions:

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
// const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
// const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<vector<pair<string,int>>> G(3);
  for(int i=0; i<3; i++){
    while(1){
      string S;
      int N;
      cin >> S;
      if(S == "$") break;
      cin >> N;
      G[i].emplace_back(S,N);
    }
  }
  vector<pair<string,int>> ans;
  for(int i=0; i<G[0].size(); i++){
    bool did = false;
    if(i+G[1].size()-1 < G[0].size()){
      if(G[0][i].first == G[1][0].first && G[0][i].second >= G[1][0].second){
        bool can = true;
        for(int j=1; j<G[1].size()-1; j++){
          if(G[0][i+j] != G[1][j]) can = false;
        }
        if(can && G[0][i+G[1].size()-1].first == G[1][G[1].size()-1].first && G[0][i+G[1].size()-1].second >= G[1][G[1].size()-1].second){
          if(G[0][i].second > G[1][0].second){
            ans.emplace_back(G[0][i].first,G[0][i].second-G[1][0].second);
          }
          for(int j=0; j<G[2].size(); j++){
            ans.emplace_back(G[2][j]);
          }
          G[0][i+G[1].size()-1].second -= G[1][G[1].size()-1].second;
          i += G[1].size();
          i -= 2;
          did = true;
        }
      }
    }
    if(!did) ans.emplace_back(G[0][i]);
  }
  vector<pair<string,int>> ANS;
  ANS.emplace_back(ans[0]);
  for(int i=1; i<ans.size(); i++){
    if(ANS.back().first == ans[i].first){
      ANS.back().second += ans[i].second;
    }else{
      ANS.emplace_back(ans[i]);
    }
  }
  for(int i=0; i<ANS.size(); i++){
    cout << ANS[i].first << " " << ANS[i].second << " ";
  }
  cout << "$\n";
  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 16360
final_rank: 16

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h> 
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll INF = 1e18;
constexpr ll LLMAX = 9223372036854775807;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
// const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
// const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
// ---------------------------------------------------------------------------


int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<vector<pair<string,int>>> G(3);
  for(int i=0; i<3; i++){
    while(1){
      string S;
      int N;
      cin >> S;
      if(S == "$") break;
      cin >> N;
      G[i].emplace_back(S,N);
    }
  }
  vector<pair<string,int>> ans;
  for(int i=0; i<G[0].size(); i++){
    bool did = false;
    if(i+G[1].size()-1 < G[0].size()){
      if(G[0][i].first == G[1][0].first && G[0][i].second >= G[1][0].second){
        bool can = true;
        for(int j=1; j<G[1].size()-1; j++){
          if(G[0][i+j] != G[1][j]) can = false;
        }
        if(can && G[0][i+G[1].size()-1].first == G[1][G[1].size()-1].first && G[0][i+G[1].size()-1].second >= G[1][G[1].size()-1].second){
          if(G[0][i].second > G[1][0].second){
            ans.emplace_back(G[0][i].first,G[0][i].second-G[1][0].second);
          }
          for(int j=0; j<G[2].size(); j++){
            ans.emplace_back(G[2][j]);
          }
          if(G[0][i+G[1].size()-1].second > G[1][G[1].size()-1].second){
            G[0][i+G[1].size()-1].second -= G[1][G[1].size()-1].second;
            i += G[1].size();
            i -= 2;
          }else{
            ans.emplace_back(G[1].back());
            i += G[1].size();
            i--;
          }
          did = true;
        }
      }
    }
    if(!did) ans.emplace_back(G[0][i]);
  }
  vector<pair<string,int>> ANS;
  ANS.emplace_back(ans[0]);
  for(int i=1; i<ans.size(); i++){
    if(ANS.back().first == ans[i].first){
      ANS.back().second += ans[i].second;
    }else{
      ANS.emplace_back(ans[i]);
    }
  }
  for(int i=0; i<ANS.size(); i++){
    cout << ANS[i].first << " " << ANS[i].second << " ";
  }
  cout << "$\n";
  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 16216
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

int main(void) {
    using P = pair<char,ll>;
    vector<P> a, b, c;
    while(1) {
        char c;
        cin >> c;
        if(c == '$') break;
        ll l;
        cin >> l;
        a.emplace_back(c, l);
    }
    while(1) {
        char c;
        cin >> c;
        if(c == '$') break;
        ll l;
        cin >> l;
        b.emplace_back(c, l);
    }
    while(1) {
        char cc;
        cin >> cc;
        if(cc == '$') break;
        ll l;
        cin >> l;
        c.emplace_back(cc, l);
    }

    vector<P> ans;
    ll n = a.size(), idx = 0;
    REP(i, n) {
        if(a[i].first == b[idx].first && a[i].second >= b[idx].second) {
            if(b.size() == 1) {
                REP(j, n) {
                    if(j != i) ans.push_back(a[j]);
                    else {
                        for(auto k: c) ans.push_back(k);
                        if(a[i].second-b[0].second>0) {
                            ans.push_back({a[i].first, a[i].second-b[0].second});
                        }
                    }
                }
                continue;
            }

            if(0 < idx && idx < (int)b.size()-1 && a[i].second != b[idx].second) {
                // ng
                idx = 0;
            } else {
                idx++;
                if(idx == (ll)b.size()) {
                    // [idx-b.size(), idx) の部分を消して c に置換する
                    vector<bool> del(a.size());
                    REP(j, b.size()) {
                        a[j+idx-b.size()].second -= b[j].second;
                        if(a[j+idx-b.size()].second == 0) {
                            del[j+idx-b.size()] = true;
                        }
                    }
                    bool first = true;
                    REP(j, a.size()) {
                        if((del[j] && first) || (j>idx-(ll)b.size() && first)) {
                            for(auto k: c) ans.push_back(k);
                            first = false;
                        }
                        if(!del[j]) ans.push_back(a[j]);
                    }
                    break;
                }
            }
        } else {
            // ng
            idx = 0;
        }
    }

    if(ans.size() == 0) ans = a;

    vector<ll> head(ans.size());
    head[0] = 0;
    FOR(i, 1, ans.size()) {
        if(ans[i].first == ans[i-1].first) {
            head[i] = head[i-1];
            ans[head[i]].second += ans[i].second;
            ans[i].second = 0;
        } else {
            head[i] = i;
        }
    }

    dump("b");

    vector<P> nans;
    for(auto i: ans) if(i.second != 0) nans.push_back(i);
    REP(i, nans.size()) cout << nans[i].first << " " << nans[i].second << " ";
    cout << "$" << endl;

    return 0;
}
accuracy: 0.2807017543859649
time_ms: 20
memory_kb: 21888
final_rank: 19

code_snippet: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a*b/gcd(a,b);
}

vp in(){
    vp ret;
    string s;
    while(cin >> s, s != "$"){
        int a;
        cin >> a;
        ret.push_back(pii(s[0],a));
    }
    return ret;
}
void print(vp a){
    cout << endl;
    rep(i,a.size())cout << char(a[i].first) << " " << a[i].second << endl;
}

signed main(void) {
    int i,j;
    vp a,b,c;
    a = in();
    b = in();
    c = in();
    vp t;
    rep(i,a.size()){
        bool p = true;
        rep(j,b.size()){
            if(j == 0 || j == b.size()-1){
                if(a[i+j].first != b[j].first || a[i+j].second < b[j].second){
                    p = false;
                }
            }else{
                if(a[i+j].first != b[j].first || a[i+j].second != b[j].second){
                    p = false;
                }
            }
        }
        if(p){
            a[i+b.size()-1].second -= b.back().second;
            if(b.size() > 1 && a[i].second-b[0].second){
                if(t.size() && t.back().first == a[i].first) t.back().second += a[i].second-b[0].second;
                else t.push_back(pii(a[i].first,a[i].second-b[0].second));
            }
            rep(j,c.size()){
                if(t.size() && t.back().first == c[j].first){
                    t.back().second += c[j].second;
                }else{
                    t.push_back(c[j]);
                }
            }
            i += b.size()-2;
        }else{
            if(t.size() && t.size() && t.back().first == a[i].first){
                t.back().second += a[i].second;
            }else if(a[i].second){
                t.push_back(a[i]);
            }
        }
    }

    rep(i,t.size()){
        cout << char(t[i].first) << " " << t[i].second << " ";
    }
    cout << "$" << endl;

}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 4948
final_rank: 10

code_snippet: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a*b/gcd(a,b);
}

vp in(){
    vp ret;
    string s;
    while(cin >> s, s != "$"){
        int a;
        cin >> a;
        ret.push_back(pii(s[0]-'A',a));
    }
    return ret;
}
void print(vp a){
    cout << endl;
    rep(i,a.size())cout << a[i].first << " " << a[i].second << endl;
}

signed main(void) {
    int i,j;
    vp a,b,c;
    a = in();
    b = in();
    c = in();
    vp ans;
    rep(i,a.size()){
        bool p = true;
        rep(j,b.size()){
            if(j == 0 || j == b.size()-1){
                if(a[i+j].first != b[j].first || a[i+j].second < b[j].second){
                    p = false;
                }
            }else{
                if(a[i+j].first != b[j].first || a[i+j].second != b[j].second){
                    p = false;
                }
            }
        }
        if(p){
            a[i+b.size()-1].second -= b.back().second;
            if(b.size() > 1)ans.push_back(pii(a[i].first,a[i].second-b[0].second));
            rep(j,c.size()){
                if(ans.back().first == c[j].first){
                    ans.back().second += c[j].second;
                }else{
                    ans.push_back(c[j]);
                }
            }
            i += b.size()-2;
        }else{
            if(ans.size() && ans.back().first == a[i].first){
                ans.back().second += a[i].second;
            }else{
                ans.push_back(a[i]);
            }
        }
    }

    rep(i,ans.size()){
        cout << char(ans[i].first+'A') << " " << ans[i].second << " ";
    }
    cout << "$" << endl;

}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 4852
final_rank: 8

code_snippet: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define fi first
#define se second
using namespace std;
typedef pair<string,int> P;
vector<P>v1,v2,v3;
int x;
string s;
main(){
  while(cin>>s,s!="$")cin>>x,v1.push_back(P(s,x));
  while(cin>>s,s!="$")cin>>x,v2.push_back(P(s,x));
  while(cin>>s,s!="$")cin>>x,v3.push_back(P(s,x));
  r(i,v1.size())if(v1[i].fi==v2[0].fi&&v1[i].se>=v2[0].se){
    int flag=0,jj=0,ff=0;
    r(j,(int)v2.size()-2){
      jj=j+1;
      if(i+jj>=v1.size()){flag++;break;}
      if(v2[jj].se!=v1[i+jj].se||v2[jj].fi!=v1[i+jj].fi)flag++;
    }
    jj++;
    if(v2.size()==1)jj++;
    else if(i+jj>=v1.size())flag++;
    else if((v2[v2.size()-1].fi!=v1[i+jj].fi||v2[v2.size()-1].se>v1[i+jj].se))flag++;
    if(v2.size()!=1){
      if(!flag&&v2[jj].fi==v1[i+jj].fi&&v2[jj].se<=v1[i+jj].se){
        v1[i].se-=v2[0].se;
        r(j,v2.size()-2)v1.erase(v1.begin()+i+1);
        v1[i+1].se-=v2[v2.size()-1].se;
        r(j,v3.size())v1.insert(v1.begin()+i+1,v3[(int)v3.size()-1-j]);
      }
      if(flag)goto L;
      ff++;
    }
    else{
      if(flag)goto L;
      v1[i].se-=v2[0].se;
      r(j,v3.size())v1.insert(v1.begin()+i,v3[(int)v3.size()-1-j]);
      ff++;
    }
    r(j,v1.size())if(v1[j].se==0)v1.erase(v1.begin()+j);
    r(j,(int)v1.size()-1)if(v1[j].fi==v1[j+1].fi)
      v1[j].se+=v1[j+1].se,v1.erase(v1.begin()+j+1),j--;
    break;
    L:;
  }
  r(k,v1.size())cout<<v1[k].fi<<' '<<v1[k].se<<' ';
  cout<<'$'<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3344
final_rank: 1

code_snippet: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define fi first
#define se second
using namespace std;
typedef pair<string,int> P;
vector<P>v1,v2,v3;
int x;
string s;
main(){
  while(cin>>s,s!="$")cin>>x,v1.push_back(P(s,x));
  while(cin>>s,s!="$")cin>>x,v2.push_back(P(s,x));
  while(cin>>s,s!="$")cin>>x,v3.push_back(P(s,x));
  r(i,v1.size())if(v1[i].fi==v2[0].fi&&v1[i].se>=v2[0].se){
    int flag=0,jj=0;
    r(j,(int)v2.size()-2){
      jj=j+1;
      if(i+jj>=v1.size()){flag++;break;}
      if(v2[jj].se!=v1[i+jj].se||v2[jj].fi!=v1[i+jj].fi)flag++;
    }
    if(v2.size()!=1){
      jj++;
      if(!flag&&v2[jj].fi==v1[i+jj].fi&&v2[jj].se<=v1[i+jj].se){
        v1[i].se-=v2[0].se;
        r(j,v2.size()-2)v1.erase(v1.begin()+i+1);
        v1[i+1].se-=v2[v2.size()-1].se;
        r(j,v3.size())v1.insert(v1.begin()+i+1,v3[j]);
      }
    }
    else{
      v1[i].se-=v2[0].se;
      r(j,v3.size())v1.insert(v1.begin()+i+1,v3[j]);
    }
    r(j,v1.size())if(v1[j].se==0)v1.erase(v1.begin()+i);
    r(j,(int)v1.size()-1)if(v1[j].fi==v1[j+1].fi)
      v1[j].se+=v1[j+1].se,v1.erase(v1.begin()+j+1),j--;
    r(j,v3.size())i++;
  }
  r(k,v1.size())cout<<v1[k].fi<<' '<<v1[k].se<<' ';
  cout<<'$'<<endl;
}
accuracy: 0.2807017543859649
time_ms: 310
memory_kb: 4932
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char,ll> P;

vector<P>A,B,C;
void input(vector<P> &a){
  int num;
  char ch;
  while(cin>>ch,ch!='$'){
    cin>>num;
    a.push_back(P(ch,num));
  }
}

bool check(int i){
  int n = A.size();
  int m = B.size();
  if(i+m>n) return 0;
  if(A[i].first!=B[0].first||A[i].second<B[0].second)return 0;
  if(m == 1) return 1;

  i++;
  for(int j=1;i<m-1;j++,i++)
    if(A[i].first!=B[j].first||A[i].second!=B[j].second)return 0;
  
  if(A[i].first!=B[m-1].first||A[i].second<B[m-1].second)return 0;
  return 1;
}

void mergePush(vector<P> &res,P a){
  int c = a.second;
  char ch = a.first;
  if(c == 0)return;
  if(res.size() && res.back().first == ch)res.back().second+=c;
  else res.push_back(P(ch,c));
}

vector<P> solve(){
  vector<P> res;
  int n = A.size();
  int m = B.size();
  int l = C.size();
  int flg = 0;
  for(int i=0;i<n;i++){
    if(!check(i)){mergePush(res,A[i]);continue;}
    A[i].second -= B[0].second;
    if(m == 1){
      for(int j=0;j<l;j++) mergePush(res,C[j]);
    }
    else {
      mergePush(res,P(A[i].first,A[i].second));   
      for(int j=0;j<l;j++) mergePush(res,C[j]);
      A[i+m-1].second -= B.back().second;
    }
    i+=m-2;
  }
  return res;
}


int main(){
  input(A),input(B),input(C);
  vector<P> ans = solve();
  for(P a:ans) cout<<a.first<<" "<<a.second<<" ";
  cout<<"$"<<endl;
  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 4888
final_rank: 9

code_snippet: #include<bits/stdc++.h>
#define s second
#define f first
using namespace std;
typedef long long ll;
typedef pair<char,ll> P;
vector<P> A[3],ans;
ll len[3];

int main(){
  char c;
  ll n;
  for(int i=0;i<3;i++){
    while(cin>>c,c!='$')
      cin>>n,A[i].push_back(P(c,n));
    len[i]=A[i].size();
  }
  for(int i=0;i<len[0];i++){
    if(i>len[0]-len[1]){
      ans.push_back(A[0][i]);
      continue;
    }
    int flag=0;
    for(int j=0;j<len[1];j++){
      ll a=A[0][i+j].s,b=A[1][j].s;
      if(A[0][i+j].f!=A[1][j].f)flag=1;
      if(!j&&a<b)flag=1;
      if(j&&j!=len[1]-1&&a!=b)flag=1;
      if(j==len[1]-1&&a<b)flag=1;
    }
    if(!flag){
      if(A[0][i].s>A[1][0].s&&len[1]!=1)
	ans.push_back(P(A[1][0].f,A[0][i].s-A[1][0].s));
      for(int j=0;j<len[2];j++)
	ans.push_back(A[2][j]);
      A[0][i+len[1]-1].s-=A[1][len[1]-1].s;
      i+=len[1]-2;
    }else if(A[0][i].s)ans.push_back(A[0][i]);
  }
  char ch=ans[0].f;
  ll sum=ans[0].s;
  for(int i=1;i<ans.size();i++){
    if(ch==ans[i].f)sum+=ans[i].s;
    else cout<<ch<<' '<<sum<<' ',ch=ans[i].f,sum=ans[i].s;
  }
  cout<<ch<<' '<<sum<<" $"<<endl;
  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 4800
final_rank: 7

code_snippet: #include<bits/stdc++.h>
#define s second
#define f first
#define N 1005
using namespace std;
typedef long long ll;
typedef pair<char,ll> P;
vector<P> A[3],ans;
int len[3];

int main(){
  char c;
  int n;
  for(int i=0;i<3;i++){
    while(cin>>c,c!='$')
      cin>>n,A[i].push_back(P(c,n));
    len[i]=A[i].size();
  }
  for(int i=0;i<len[0];i++){
    if(i>len[0]-len[1]){
      ans.push_back(A[0][i]);
      continue;
    }
    int flag=0;
    for(int j=0;j<len[1];j++){
      ll a=A[0][i+j].s,b=A[1][j].s;
      if(A[0][i+j].f!=A[1][j].f)flag=1;
      if(!j&&a<b)flag=1;
      if(j&&j!=len[1]-1&&a!=b)flag=1;
      if(j==len[1]-1&&a<b)flag=1;
    }
    if(!flag){
      if(A[0][i].s>A[1][0].s)
	ans.push_back(P(A[1][0].f,A[0][i].s-A[1][0].s));
      for(int j=0;j<len[2];j++)
	ans.push_back(A[2][j]);
      A[0][i+len[1]-1].s-=A[1][len[1]-1].s;
      i+=len[1]-2;
    }else if(A[0][i].s)ans.push_back(A[0][i]);
  }
  char ch=ans[0].f;
  ll sum=ans[0].s;
  for(int i=1;i<ans.size();i++){
    if(ch==ans[i].f)sum+=ans[i].s;
    else cout<<ch<<' '<<sum<<' ',ch=ans[i].f,sum=ans[i].s;
  }
  cout<<ch<<' '<<sum<<" $"<<endl;
  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 4752
final_rank: 6

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)

const int inf = 1 << 25;

typedef pair<char, int> P;

void input(vector<P>& v)
{
  char c; int l;
  while(cin >> c, c != '$') {
    cin >> l;
    v.emplace_back(c, l);
  }
}

int main()
{
  vector<P> A; input(A);
  vector<P> B; input(B);
  vector<P> C; input(C);

  int a = A.size();
  int b = B.size();
  int c = C.size();

  rep(i, a-b+1) {
    if(A[i].F == B[0].F && A[i].S >= B[0].S &&
       A[i+b-1].F == B[b-1].F && A[i+b-1].S >= B[b-1].S) {
      bool flag = true;
      reps(j, 1, b-1) flag &= (A[i+j].F == B[j].F && A[i+j].S == B[j].S);
      if(!flag) continue;
      if(b == 1) {
	A[i].S -= B[0].S;
	if(A[i].S == 0)	A.erase(begin(A)+i, begin(A)+i+1);
	A.insert(begin(A)+i, begin(C), end(C));
      } else {
	A[i].S -= B[0].S;
        A[i+b-1].S -= B[b-1].S;
	auto it1 = begin(A)+i;
	auto it2 = it1 + b;
	if(A[i].S != 0) it1++;
	if(A[i+b-1].S != 0) it2--;
	A.erase(it1, it2);
	A.insert(it1, begin(C), end(C));
      }
      for(auto it = begin(A); it != end(A); it++) {
	auto itr = it+1;
	while(itr != end(A) && it->F == itr->F) it->S += itr->S, itr++;
	if(itr != it+1) A.erase(it+1, itr);
      }
    }
  }

  rep(i, A.size()) cout << A[i].F << " " << A[i].S << " ";
  cout << "$" << endl;

  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 3740
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define F first
#define S second
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)

const int inf = 1 << 25;

typedef pair<char, int> P;

void input(vector<P>& v)
{
  char c; int l;
  while(cin >> c, c != '$') {
    cin >> l;
    v.emplace_back(c, l);
  }
}

int main()
{
  vector<P> A; input(A);
  vector<P> B; input(B);
  vector<P> C; input(C);

  int a = A.size();
  int b = B.size();
  int c = C.size();

  rep(i, a-b+1) {
    if(A[i].F == B[0].F && A[i].S >= B[0].S &&
       A[i+b-1].F == B[b-1].F && A[i+b-1].S >= B[b-1].S) {
      bool flag = true;
      reps(j, 1, b-1) flag &= (A[i+j].F == B[j].F && A[i+j].S == B[j].S);
      if(!flag) continue;
      if(b == 1) {
	A[i].S -= B[0].S;
	auto it1 = begin(A)+i;
	if(A[i].S == 0)	A.erase(begin(A)+i, begin(A)+i+1);
	else it1++;
	A.insert(it1, begin(C), end(C));
      } else {
	A[i].S -= B[0].S;
        A[i+b-1].S -= B[b-1].S;
	auto it1 = begin(A)+i;
	auto it2 = it1 + b;
	if(A[i].S != 0) it1++;
	if(A[i+b-1].S != 0) it2--;
	A.erase(it1, it2);
	A.insert(it1, begin(C), end(C));
      }
      for(auto it = begin(A); it != end(A); it++) {
	auto itr = it+1;
	while(itr != end(A) && it->F == itr->F) it->S += itr->S, itr++;
	if(itr != it+1) A.erase(it+1, itr);
      }
    }
  }

  rep(i, A.size()) cout << A[i].F << " " << A[i].S << " ";
  cout << "$" << endl;

  return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 3404
final_rank: 2

code_snippet: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

ifstream ifs("in.txt");

void inp(vector<pair<char, int> > &a){
    while (1){
        char c;
        cin >> c;
        if (c == '$') break;
        int x;
        cin >> x;
        a.push_back(make_pair(c, x));
    }
}

int main() {
    vector<pair<char, int> > a, b, c, res, ans;
    inp(a);
    inp(b);
    inp(c);
    REP(i,(int)a.size() - (int)b.size() + 1){
        bool f = true;
        REP(j,b.size()){
            if (a[i+j].first != b[j].first){
                f = false;
                break;
            }
            if (j == 0 || j == b.size()-1){
                if (a[i+j].second < b[j].second){
                    f = false;
                    break;
                }
            }else{
                if (a[i+j].second != b[j].second){
                    f = false;
                    break;
                }
            }
        }
        if (f){
            REP(j,i) res.push_back(a[j]);
            if (b.size() > 1){
                a[i].second -= b[0].second;
                res.push_back(a[i]);
            }
            REP(j,c.size()) res.push_back(c[j]);
            a[i+b.size()-1].second -= b[b.size()-1].second;
            res.push_back(a[i+b.size()-1]);
            FOR(j,i+b.size(),a.size()-1) res.push_back(a[j]);
        }
    }
    if (res.empty()){
        REP(i,a.size()){
            printf("%c %d ", a[i].first, a[i].second);
        }
        printf("$\n");
    }else{
        REP(i,res.size()){
            if (!ans.empty() && ans[ans.size()-1].first == res[i].first){
                ans[ans.size()-1].second += res[i].second;
            }else if (res[i].second > 0){
                ans.push_back(res[i]);
            }
        }
        REP(i,ans.size()){
            printf("%c %d ", ans[i].first, ans[i].second);
        }
        printf("$\n");
    }
    return 0;
}
accuracy: 0.2807017543859649
time_ms: 20
memory_kb: 11132
final_rank: 14

code_snippet: #include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

ifstream ifs("in.txt");

void inp(vector<pair<char, int> > &a){
    while (1){
        char c;
        cin >> c;
        if (c == '$') break;
        int x;
        cin >> x;
        a.push_back(make_pair(c, x));
    }
}

int main() {
    vector<pair<char, int> > a, b, c, res, ans;
    inp(a);
    inp(b);
    inp(c);
    REP(i,(int)a.size() - (int)b.size() + 1){
        bool f = true;
        REP(j,b.size()){
            if (a[i+j].first != b[j].first){
                f = false;
                break;
            }
            if (j == 0 || j == b.size()-1){
                if (a[i+j].second < b[j].second){
                    f = false;
                    break;
                }
            }else{
                if (a[i+j].second != b[j].second){
                    f = false;
                    break;
                }
            }
        }
        if (f){
            REP(j,i) res.push_back(a[j]);
            a[i].second -= b[0].second;
            res.push_back(a[i]);
            REP(j,c.size()) res.push_back(c[j]);
            if (b.size() > 1){
                a[i+b.size()-1].second -= b[b.size()-1].second;
                res.push_back(a[i+b.size()-1]);
            }
            FOR(j,i+b.size(),a.size()-1) res.push_back(a[j]);
        }
    }
    if (res.empty()){
        REP(i,a.size()){
            printf("%c %d ", a[i].first, a[i].second);
        }
        printf("$\n");
    }else{
        REP(i,res.size()){
            if (!ans.empty() && ans[ans.size()-1].first == res[i].first){
                ans[ans.size()-1].second += res[i].second;
            }else if (res[i].second > 0){
                ans.push_back(res[i]);
            }
        }
        REP(i,ans.size()){
            printf("%c %d ", ans[i].first, ans[i].second);
        }
        printf("$\n");
    }
    return 0;
}
accuracy: 0.2807017543859649
time_ms: 10
memory_kb: 11108
final_rank: 13

code_snippet: #include<iostream>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<char,ll> P;
int main(){
  vector <P> a,b,c;
  ll i,j,k;
  char ch;
  while(cin>>ch,ch!='$'){
    cin >> k;
    a.push_back(P(ch,k));
  }
  while(cin>>ch,ch!='$'){
    cin >> k;
    b.push_back(P(ch,k));
  }
  while(cin>>ch,ch!='$'){
    cin >> k;
    c.push_back(P(ch,k));
  }
  for(i=0;i+b.size()<=a.size();i++){
    if(a[i].first!=b[0].first||a[i].second<b[0].second) continue;
    bool f = true;
    k=b.size()-1;
    for(j=1;j<k;j++){
      if(a[i+j].first!=b[j].first||a[i+j].second!=b[j].second) {
	f=false;
	break;
      }
    }
    if(a[i+k].first!=b[k].first||a[i+k].second<b[k].second) continue;
    if(f){
      //cout << i << endl;
      for(j=0;j<k+1;j++){
	a[i+j].second-=b[j].second;
      }
      a.insert(a.begin()+i+1,c.begin(),c.end());
      i+=b.size()+c.size()-1;
      //break;
    }
  }


  bool ff=true;
  while(ff){
    ff=false;
    k=0;
    while(k<a.size()){
      if(a[k].second==0) {
	a.erase(a.begin()+k);
	ff=true;
      }else k++;
    }
    for(i=0;i<a.size()-1;i++){
      if(a[i].first==a[i+1].first){
	a[i+1].second+=a[i].second;
	a[i].second=0;
      }
    }
  }
  
  for(i=0;i<a.size();i++){
    if(a[i].second==0) continue;
    cout << a[i].first << " " << a[i].second << " ";
  }
  cout << "$" << endl;
}
accuracy: 0.2807017543859649
time_ms: 20
memory_kb: 3112
final_rank: 3

code_snippet: #include<iostream>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<char,ll> P;
int main(){
  vector <P> a,b,c;
  ll i,j,k;
  char ch;
  while(cin>>ch,ch!='$'){
    cin >> k;
    a.push_back(P(ch,k));
  }
  while(cin>>ch,ch!='$'){
    cin >> k;
    b.push_back(P(ch,k));
  }
  while(cin>>ch,ch!='$'){
    cin >> k;
    c.push_back(P(ch,k));
  }
  for(i=0;i+b.size()<=a.size();i++){
    if(a[i].first!=b[0].first||a[i].second<b[0].second) continue;
    bool f = true;
    k=b.size()-1;
    for(j=1;j<k;j++){
      if(a[i+j].first!=b[j].first||a[i+j].second!=b[j].second) {
	f=false;
	break;
      }
    }
    if(a[i+k].first!=b[k].first||a[i+k].second<b[k].second) continue;
    if(f){
      //cout << i << endl;
      for(j=0;j<k+1;j++){
	a[i+j].second-=b[j].second;
      }
      a.insert(a.begin()+i+1,c.begin(),c.end());
      i+=b.size()+c.size();
      //break;
    }
  }


  bool ff=true;
  while(ff){
    ff=false;
    k=0;
    while(k<a.size()){
      if(a[k].second==0) {
	a.erase(a.begin()+k);
	ff=true;
      }else k++;
    }
    for(i=0;i<a.size()-1;i++){
      if(a[i].first==a[i+1].first){
	a[i+1].second+=a[i].second;
	a[i].second=0;
      }
    }
  }
  
  for(i=0;i<a.size();i++){
    if(a[i].second==0) continue;
    cout << a[i].first << " " << a[i].second << " ";
  }
  cout << "$" << endl;
}
accuracy: 0.2807017543859649
time_ms: 20
memory_kb: 3112
final_rank: 3

code_snippet: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>
#include <fstream>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

void change(deque<pair<char, int>>& nv0) {
  if(nv0.size() > 1 && nv0[nv0.size()-2].first == nv0.back().first) {
    int k = nv0.back().second;
    nv0.pop_back();
    nv0.back().second += k;
  }
}

void output(deque<pair<char, int>>& out) {
  rep(i, out.size()) {
    if(i) cout << " ";
    cout << out[i].first << " " << out[i].second;
  }
  cout << " $" << endl;
}

int main() {

  vector<deque<pair<char, int>>> v(3);
  for(auto& u: v) {
    while(1) {
      char c; cin >> c;
      if(c == '$') break;
      int n; cin >> n;
      u.emplace_back(c, n);
    }
  }

  rep(i, (int)v[0].size()-(int)v[1].size()+1) {
    int j = 0;
    vector<int> V;
    while(j < v[1].size() && v[0][i+j].first == v[1][j].first) {
      if(j == 0 || j == (int)v[1].size()-1) {
        if(v[0][i+j].second < v[1][j].second) break;
        V.push_back(v[0][i+j].second - v[1][j].second);
      }
      else {
        if(v[0][i+j].second != v[1][j].second) break;
      }
      j++;
    }

    deque<pair<char, int>> nv0;

    if(j == v[1].size()) {
      rep(k, v[0].size()) {
        if(k < i) {
          nv0.push_back(v[0][k]);
          change(nv0);
        }
        if(k == i) {
          if(V[0] > 0) nv0.emplace_back(v[0][i].first, V[0]);
          rep(l, v[2].size()) {
            nv0.push_back(v[2][l]);
            change(nv0);
          }
          k = i+v[1].size()-1;
        }
        if(k == i+j-1) {
          if(V[1] > 0) nv0.emplace_back(v[0][i+j-1].first, V[1]);
          change(nv0);
        }
        if(i+j <= k) {
          nv0.push_back(v[0][k]);
          change(nv0);
        }
      }
      v[0] = nv0;
      i += (int)v[2].size();
//      i--;
    }
  }

  output(v[0]);

  return 0;
}
accuracy: 0.2807017543859649
time_ms: 80
memory_kb: 4224
final_rank: 11

code_snippet: #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>
#include <fstream>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }

typedef long long ll;
int const inf = 1<<29;

void change(deque<pair<char, int>>& nv0) {
  if(nv0.size() > 1 && nv0[nv0.size()-2].first == nv0.back().first) {
    int k = nv0.back().second;
    nv0.pop_back();
    nv0.back().second += k;
  }
}

void output(deque<pair<char, int>>& out) {
  rep(i, out.size()) {
    if(i) cout << " ";
    cout << out[i].first << " " << out[i].second;
  }
  cout << " $" << endl;
}

int main() {

  vector<deque<pair<char, int>>> v(3);
  for(auto& u: v) {
    while(1) {
      char c; cin >> c;
      if(c == '$') break;
      int n; cin >> n;
      u.emplace_back(c, n);
    }
  }

  rep(i, (int)v[0].size()-(int)v[1].size()+1) {
    int j = 0;
    vector<int> V;
    while(j < v[1].size() && v[0][i+j].first == v[1][j].first) {
      if(j == 0 || j == (int)v[1].size()-1) {
        if(v[0][i+j].second < v[1][j].second) break;
        V.push_back(v[0][i+j].second - v[1][j].second);
      }
      else {
        if(v[0][i+j].second != v[1][j].second) break;
      }
      j++;
    }

    deque<pair<char, int>> nv0;

    if(j == v[1].size()) {
      rep(k, v[0].size()) {
        if(k < i) {
          nv0.push_back(v[0][k]);
          change(nv0);
        }
        if(k == i) {
          if(V[0] > 0) nv0.emplace_back(v[0][i].first, V[0]);
          rep(l, v[2].size()) {
            nv0.push_back(v[2][l]);
            change(nv0);
          }
          k = i+v[1].size()-1;
        }
        if(k == i+j-1) {
          if(V[1] > 0) nv0.emplace_back(v[0][i+j-1].first, V[1]);
          change(nv0);
        }
        if(i+j <= k) {
          nv0.push_back(v[0][k]);
          change(nv0);
        }
      }
      v[0] = nv0;
      i += (int)v[2].size()-1;
//      i--;
    }
  }

  output(v[0]);

  return 0;
}
accuracy: 0.2807017543859649
time_ms: 90
memory_kb: 4616
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
typedef pair<char, ll> pci;
vector<pci> a, b, c;

int main()
{
	char let; ll n;
	while (cin >> let&&let != '$')
	{
		cin >> n;
		a.emplace_back(let, n);
	}
	while (cin >> let&&let != '$')
	{
		cin >> n;
		b.emplace_back(let, n);
	}
	while (cin >> let&&let != '$')
	{
		cin >> n;
		c.emplace_back(let, n);
	}
	vector<pci> res;
	
	REP(i, (int)a.size() - (int)b.size() + 1)
	{
		if (a[i].first == b[0].first&&a[i].second >= b[0].second)
		{
			bool f = true;
			int j;
			for (j = 1; j < b.size(); j++)
			{
				if (j == b.size() - 1)
				{
					if (b[j].first != a[i + j].first || b[j].second > a[i + j].second) f = false;
				}
				else
				{
					if (b[j].first != a[i + j].first || b[j].second != a[i + j].second) f = false;
				}
			}
			if (f)
			{
				if (b.size() == 1)
				{
					REP(k, i) res.emplace_back(a[k]);
					REP(k, c.size()) res.emplace_back(c[k]);
					if (a[i].second > b[0].second) res.emplace_back(a[i].first, a[i].second - b[0].second);
					FOR(k, i + b.size(), a.size()) res.emplace_back(a[k]);
				}
				else
				{
					REP(k, i) res.emplace_back(a[k]);
					if (a[i].second > b[0].second) res.emplace_back(a[i].first, a[i].second - b[0].second);
					REP(k, c.size()) res.emplace_back(c[k]);
					if (a[i + b.size() - 1].second > b.back().second) res.emplace_back(b.back().first, a[i + b.size() - 1].second - b.back().second);
					FOR(k, i + b.size(), a.size()) res.emplace_back(a[k]);
					i = INF;
				}
			}
		}
	}
	vector<pci> ans;
	if (res.size() == 0) ans = a;
	else
	{
		ans.emplace_back(res[0]);
		FOR(i, 1, res.size())
		{
			if (res[i].first == ans.back().first) ans.back().second += res[i].second;
			else ans.emplace_back(res[i]);
		}
	}
	REP(i, ans.size()) cout << ans[i].first << " " << ans[i].second << " ";
	cout << "$\n";
	return 0;
}
accuracy: 0.2807017543859649
time_ms: 30
memory_kb: 19552
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
typedef pair<char, ll> pci;
vector<pci> a, b, c;

int main()
{
	char let; ll n;
	while (cin >> let&&let != '$')
	{
		cin >> n;
		a.emplace_back(let, n);
	}
	while (cin >> let&&let != '$')
	{
		cin >> n;
		b.emplace_back(let, n);
	}
	while (cin >> let&&let != '$')
	{
		cin >> n;
		c.emplace_back(let, n);
	}
	vector<pci> res;
	
	REP(i, (int)a.size() - (int)b.size() + 1)
	{
		if (a[i].first == b[0].first&&a[i].second >= b[0].second)
		{
			bool f = true;
			int j;
			for (j = 1; j < b.size(); j++)
			{
				if (j == b.size() - 1)
				{
					if (b[j].first != a[i + j].first || b[j].second > a[i + j].second) f = false;
				}
				else
				{
					if (b[j].first != a[i + j].first || b[j].second != a[i + j].second) f = false;
				}
			}
			if (f)
			{
				if (b.size() == 1)
				{
					REP(k, i) res.emplace_back(a[k]);
					REP(k, c.size()) res.emplace_back(c[k]);
					if (a[i].second > b[0].second) res.emplace_back(a[i].first, a[i].second - b[0].second);
					FOR(k, i + b.size(), a.size()) res.emplace_back(a[k]);
				}
				else
				{
					REP(k, i) res.emplace_back(a[k]);
					if (a[i].second > b[0].second) res.emplace_back(a[i].first, a[i].second - b[0].second);
					REP(k, c.size()) res.emplace_back(c[k]);
					if (a[i + b.size() - 1].second > b.back().second) res.emplace_back(b.back().first, a[i + b.size() - 1].second - b.back().second);
					FOR(k, i + b.size(), a.size()) res.emplace_back(a[k]);
					i = INF;
				}
			}
		}
	}
	vector<pci> ans;
	if (res.size() == 0) ans = a;
	else
	{
		ans.emplace_back(res[0]);
		FOR(i, 1, res.size())
		{
			if (res[i].first == ans.back().first) ans.back().second += res[i].second;
			else ans.emplace_back(res[i]);
		}
	}
	REP(i, ans.size()) cout << ans[i].first << " " << ans[i].second << (i == ans.size() - 1 ? " $\n" : " ");
	return 0;
}
accuracy: 0.2807017543859649
time_ms: 20
memory_kb: 19508
final_rank: 17