Submission

Status:

[PPPPPPPPPP]

Subtask/Task Score:

{100/100}

Score: 100

User: KantaponZ

Problemset: composite tree

Language: cpp

Time: 0.002 second

Submitted On: 2026-03-10 11:29:59

#include <bits/stdc++.h>
using namespace std;

const int nx = 1e3 + 5;

map<int,int> tree1, tree2, res;

void solve1(int idx) {
    if (idx == 1) {
        char c; cin >> c;
        tree1[idx] = c - '0';
        cin >> c;
        solve1(idx*2);
        cin >> c;
        solve1(idx*2+1);
        return;
    }

    char c; cin >> c;
    if (c >= '1' && c <= '9') {
        tree1[idx] = c - '0';
        cin >> c;
        solve1(idx*2);
        cin >> c;
        solve1(idx*2+1);
        cin >> c;
        if (c == ')') return;
    }
}

void solve2(int idx) {
    if (idx == 1) {
        char c; cin >> c;
        tree2[idx] = c - '0';
        cin >> c;
        solve2(idx*2);
        cin >> c;
        solve2(idx*2+1);
        return;
    }

    char c; cin >> c;
    if (c >= '1' && c <= '9') {
        tree2[idx] = c - '0';
        cin >> c;
        solve2(idx*2);
        cin >> c;
        solve2(idx*2+1);
        cin >> c;
        if (c == ')') return;
    }
}

void ans(int idx) {
    if (res[idx] == 0) return;
    cout << res[idx];
    cout << '(';
    ans(idx*2);
    cout << ')';
    cout << '(';
    ans(idx*2+1);
    cout << ')';
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    solve1(1);
    solve2(1);
    for (auto [k , v] : tree1) {
        if (v == 0) continue;
        if (tree2.find(k) == tree2.end()) continue;
        res[k] = v + tree2[k];
    }
    ans(1);
}