// Solution to : http://codeforces.com/contest/13/problem/E
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cmath>
#include <iterator>
#include <map>
#include <set>
#ifndef ONLINE_JUDGE
#include <prettyprint/prettyprint.hpp>
#endif

#define FOR(i, n) for(int i = 0; i < n; ++i)
#define TEST int T; std::cin >> T; while(T--)
#define ALL(v) v.begin(), v.end()

using ll = long long;
using ull = unsigned long long;
using vi = std::vector<int>;
using vll = std::vector<ll>;
using vull = std::vector<ull>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

using namespace std;

int N, M;
int num_blocks, block_size;

int power[100000];

int jumps[320][320];
int exits[320][320];

int get_block(int i) {
	return i / block_size;
}
int get_block_idx(int i) {
	return i % block_size;
}

void init() {
	block_size = ceil(sqrt(N));
	num_blocks = ceil((float)N / block_size);
	for (int i = N - 1; i >= 0; --i) {
		int cur_block = get_block(i), cur_idx = get_block_idx(i);
		int limit = min(cur_block + block_size - 1, N - 1);
		int next = i + power[i];
		if(next > limit) {
			exits[cur_block][cur_idx] = (next < N) ? next : i;
			jumps[cur_block][cur_idx] = (next < N) ? 1 : 0;
		} else {
			exits[cur_block][cur_idx] = exits[cur_block][get_block_idx(next)];
			jumps[cur_block][cur_idx] = jumps[cur_block][get_block_idx(next)] + 1;
		}
	}
}

void show() {
	cout << "Jumps : " << endl;
	for(int i = 0; i < num_blocks; ++i) {
		for(int j = 0; j < block_size; ++j) {
			cout << jumps[i][j] << " ";
		}
		cout << endl;
	}
	cout << "Exits : " << endl;
	for(int i = 0; i < num_blocks; ++i) {
		for(int j = 0; j < block_size; ++j) {
			cout << exits[i][j] << " ";
		}
		cout << endl;
	}
}

void update(int x) {
	int cur_block = get_block(x);
	int start_idx = cur_block * block_size;
	int limit = min(start_idx + block_size - 1, N - 1), cur_idx = block_size - 1;
	for (int i = limit; i >= start_idx; --i) {
		int next = i + power[i];
		if(next > limit) {
			exits[cur_block][cur_idx] = (next < N) ? next : i;
			jumps[cur_block][cur_idx] = (next < N) ? 1 : 0;
		} else {
			exits[cur_block][cur_idx] = exits[cur_block][get_block_idx(next)];
			jumps[cur_block][cur_idx] = jumps[cur_block][get_block_idx(next)] + 1;
		}
		--cur_idx;
	}
}

pii query(int x) {
	int total_jumps = 0, prev = x + 1;

	while(x != prev && x + power[x] < N) {
		total_jumps += jumps[get_block(x)][get_block_idx(x)];
		prev = x;
		x = exits[get_block(x)][get_block_idx(x)];
	}
	if(x == prev) {
		x = x + power[x];
		++total_jumps;
	}
	return make_pair(x + 1, total_jumps + 1);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
#ifndef ONLINE_JUDGE
	ifstream fin("in.txt");
	ofstream fout("out.txt");
	auto cinbuf = cin.rdbuf(fin.rdbuf());    // save and redirect
	auto coutbuf = cout.rdbuf(fout.rdbuf()); // save and redirect
#endif
	cin >> N >> M;

	FOR(i, N) {
		cin >> power[i];
	}

	init();
	// show();

	vector<pair<int, int>> buffer;
	// Lazy updates?
	set<int> up_idx;
	FOR(i, M) {
		int t, a, b;
		cin >> t;
		if(t == 0) {
			cin >> a >> b;
			--a;
			power[a] = b;
			up_idx.insert(a);
		} else {
			cin >> a;
			--a;
			// Only update once for each block.
			int last_block = -1;
			auto start_it = up_idx.lower_bound(a);
			for(auto it = start_it; it != up_idx.end(); ++it) {
				if(*it / block_size != last_block) update(*it);
				last_block = *it / block_size;
			}
			up_idx.erase(start_it, up_idx.end());

			buffer.emplace_back(query(a));
		}
	}
	for(auto e : buffer) {
		cout << e.first << " " << e.second << "\n";
	}
	// cout << "Power : ";
	// copy(power, power + N, ostream_iterator<int>(cout, " "));
#ifndef ONLINE_JUDGE
	cin.rdbuf(cinbuf);    // restore
	cout.rdbuf(coutbuf); // restore
#endif
}


// Solution to : http://codeforces.com/contest/13/problem/E
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cmath>
#include <iterator>
#include <map>
#include <set>
#ifndef ONLINE_JUDGE
#include <prettyprint/prettyprint.hpp>
#endif

#define FOR(i, n) for(int i = 0; i < n; ++i)
#define TEST int T; std::cin >> T; while(T--)
#define ALL(v) v.begin(), v.end()

using ll = long long;
using ull = unsigned long long;
using vi = std::vector<int>;
using vll = std::vector<ll>;
using vull = std::vector<ull>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

using namespace std;

int N, M;
int num_blocks, block_size;
int power[100000];

int jumps[320][320];
int exits[320][320];

int get_block(int i) {
	return i / block_size;
}
int get_block_idx(int i) {
	return i % block_size;
}

void init() {
	block_size = ceil(sqrt(N));
	num_blocks = ceil((float)N / block_size);

	for (int i = 0; i < N; ++i) {
		int cur = i, prev, limit = min(get_block(i) * block_size + block_size - 1, N - 1);

		int cnt = 0;
		while(cur <= limit) {
			++cnt;
			prev = cur;
			cur += power[cur];
		}
		exits[get_block(i)][get_block_idx(i)] = (cur < N) ? (cur) : (prev);
		jumps[get_block(i)][get_block_idx(i)] = cnt;
	}
}

void show() {
	cout << "Jumps : " << endl;
	for(int i = 0; i < num_blocks; ++i) {
		for(int j = 0; j < block_size; ++j) {
			cout << jumps[i][j] << " ";
		}
		cout << endl;
	}
	cout << "Exits : " << endl;
	for(int i = 0; i < num_blocks; ++i) {
		for(int j = 0; j < block_size; ++j) {
			cout << exits[i][j] << " ";
		}
		cout << endl;
	}
}

void update(int x, int newval) {
	power[x] = newval;

	int start_idx = get_block(x) * block_size;
	int limit = min(start_idx + block_size - 1, N - 1);

	for (int i = start_idx; i <= limit; ++i) {
		int cur = i, prev;
		int cnt = 0;
		while(cur <= limit) {
			++cnt;
			prev = cur;
			cur += power[cur];
		}
		exits[get_block(i)][get_block_idx(i)] = (cur < N) ? (cur) : (prev);
		jumps[get_block(i)][get_block_idx(i)] = cnt;
	}
}

pii query(int x) {
	int total_jumps = 0, prev;

	while(x + power[x] < N) {
		total_jumps += jumps[get_block(x)][get_block_idx(x)];
		prev = x;
		x = exits[get_block(x)][get_block_idx(x)];
	}
	return make_pair(x, total_jumps + 1);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
#ifndef ONLINE_JUDGE
	ifstream fin("in.txt");
	ofstream fout("out.txt");
	auto cinbuf = cin.rdbuf(fin.rdbuf());    // save and redirect
	auto coutbuf = cout.rdbuf(fout.rdbuf()); // save and redirect
#endif
	cin >> N >> M;

	FOR(i, N) {
		cin >> power[i];
	}

	init();
	// show();
	FOR(i, M) {
		int t, a, b;
		cin >> t;
		if(t == 0) {
			cin >> a >> b;
			--a;
			update(a, b);
		} else {
			cin >> a;
			--a;
			auto ans = query(a);
			cout << ans.first + 1 << " " << ans.second << endl;
		}
	}
	// cout << "Power : ";
	// copy(power, power + N, ostream_iterator<int>(cout, " "));
#ifndef ONLINE_JUDGE
	cin.rdbuf(cinbuf);    // restore
	cout.rdbuf(coutbuf); // restore
#endif
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <memory.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <deque>

using namespace std;

typedef long long ll;

const int N = 200001;

int a[N];
int in[N];
int out[N];
int lvl[N];
int timer;
int go[N];
int rt[N];
int len[N];
vector<int> ch;
int root;
vector<int> g[N];

void dfs(int v, int level)
{
    lvl[v] = level;
    rt[v] = root;
    in[v] = timer++;
    for (int i = 0; i < g[v].size(); i++)
        dfs(g[v][i], level + 1);
    out[v] = timer++;
}
int n;
bool par(int pr, int v)
{
    return pr >= 0 && pr < n && v >= 0 && v < n && in[pr] <= in[v] && out[pr] >= out[v] && rt[v] == rt[pr];
}

int main()
{
//    freopen("in.txt", "r", stdin);
    int m;
    //cin >> n >> m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]), a[i] += i;
    int T = 250;//max(1, (int)sqrt(m) * 2);
    for (int i = 0; i < m; i++)
    {
        if (i % T == 0)
        {
            for (int i = 0; i < n; i++)
                g[i].clear();
            for (int i = 0; i < n; i++)
                if (a[i] < n)
                    g[a[i]].push_back(i);
            for (int i = 0; i < n; i++)
                if (a[i] >= n)
                {
                    timer = 0;
                    root = i;
                    dfs(i, 0);
                }
            for (int i = 0; i < n; i++)
                go[i] = -1;
            ch.clear();
        }
        int type, v;
       // cin >> type >> v;
        scanf("%d %d", &type, &v);
        v--;
        if (type == 0)
        {
            int b;
            //cin >> b;
            scanf("%d", &b);
            a[v] = v + b;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, a[v]) && (best == -1 || par(best, u)))
                    best = u;
            }
            go[v] = -1;
            if (best != -1)
            {
                go[v] = best;
                len[v] = lvl[a[v]] - lvl[best] + 1;
            }
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (u == v)
                    continue;
                if (par(v, a[u]) && (go[u] == -1 || par(go[u], v)))
                {
                    go[u] = v;
                    len[u] = lvl[a[u]] - lvl[v] + 1;
                }
            }
            ch.push_back(v);
        }
        else
        {
            int ans = 0;
            int best = rt[v];
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, v) && par(best, u))
                    best = u;
            }
            ans += lvl[v] - lvl[best];
            v = best;
            while (go[v] != -1)
            {
                ans += len[v];
                 v = go[v];
            }
            if (a[v] < n)
            {
                ans += lvl[a[v]] - lvl[rt[a[v]]] + 1;
                v = rt[a[v]];
            }
            cout << v + 1 << " " << ans + 1 << "\n";
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <memory.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <deque>

using namespace std;

typedef long long ll;

const int N = 100001;

int a[N];
int in[N];
int out[N];
int lvl[N];
int timer;
int go[N];
int rt[N];
int len[N];
vector<int> ch;
int root;
vector<int> g[N];

void dfs(int v, int level)
{
    lvl[v] = level;
    rt[v] = root;
    in[v] = timer++;
    for (int i = 0; i < g[v].size(); i++)
        dfs(g[v][i], level + 1);
    out[v] = timer++;
}

bool par(int pr, int v)
{
    return in[pr] <= in[v] && out[pr] >= out[v];
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> a[i], a[i] += i;
    int T = max(1, (int)sqrt(m));
    for (int i = 0; i < m; i++)
    {
        if (i % T == 0)
        {
            for (int i = 0; i < n; i++)
                g[i].clear();
            for (int i = 0; i < n; i++)
                if (a[i] < n)
                    g[a[i]].push_back(i);
            for (int i = 0; i < n; i++)
                if (a[i] >= n)
                {
                    timer = 0;
                    root = i;
                    dfs(i, 0);
                }
            for (int i = 0; i < n; i++)
                go[i] = -1;
        }
        int type, v;
        cin >> type >> v;
        v--;
        if (type == 0)
        {
            int b;
            cin >> b;
            a[v] = v + b;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, v) && (best == -1 || par(best, u)))
                    best = u;
            }
            if (best != -1)
            {
                go[v] = best;
                len[v] = lvl[a[v]] - lvl[best] + 1;
            }
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(v, u) && (go[u] == -1 || par(go[u], v)))
                {
                    go[u] = v;
                    len[u] = lvl[v] - lvl[u];
                }
            }
            ch.push_back(v);
        }
        else
        {
            int ans = 0;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, v) && (best == -1 || par(best, u)))
                    best = u;
            }
            if (best == -1)
            {
                ans = lvl[v] - lvl[rt[v]];
                v = rt[v];
            }
            else
            {
                ans += lvl[v] - lvl[best];
                v = best;
                while (go[v] != -1)
                {
                    ans += len[v];
                    v = go[v];
                }
                ans += lvl[v] - lvl[rt[v]];
                v = rt[v];
            }
            cout << v + 1 << " " << ans + 1 << "\n";
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <memory.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <deque>

using namespace std;

typedef long long ll;

const int N = 200001;

int a[N];
int in[N];
int out[N];
int lvl[N];
int timer;
int go[N];
int rt[N];
int len[N];
vector<int> ch;
int root;
vector<int> g[N];

void dfs(int v, int level)
{
    lvl[v] = level;
    rt[v] = root;
    in[v] = timer++;
    for (int i = 0; i < g[v].size(); i++)
        dfs(g[v][i], level + 1);
    out[v] = timer++;
}
int n;
bool par(int pr, int v)
{
    return pr >= 0 && pr < n && v >= 0 && v < n && in[pr] <= in[v] && out[pr] >= out[v] && rt[v] == rt[pr];
}

int main()
{
//    freopen("in.txt", "r", stdin);
    int m;
    //cin >> n >> m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]), a[i] += i;
    int T = max(1, (int)sqrt(m) / 10);
    for (int i = 0; i < m; i++)
    {
        if (i % T == 0)
        {
            for (int i = 0; i < n; i++)
                g[i].clear();
            for (int i = 0; i < n; i++)
                if (a[i] < n)
                    g[a[i]].push_back(i);
            for (int i = 0; i < n; i++)
                if (a[i] >= n)
                {
                    timer = 0;
                    root = i;
                    dfs(i, 0);
                }
            for (int i = 0; i < n; i++)
                go[i] = -1;
            ch.clear();
        }
        int type, v;
       // cin >> type >> v;
        scanf("%d %d", &type, &v);
        v--;
        if (type == 0)
        {
            int b;
            //cin >> b;
            scanf("%d", &b);
            a[v] = v + b;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, a[v]) && (best == -1 || par(best, u)))
                    best = u;
            }
            go[v] = -1;
            if (best != -1)
            {
                go[v] = best;
                len[v] = lvl[a[v]] - lvl[best] + 1;
            }
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (u == v)
                    continue;
                if (par(v, a[u]) && (go[u] == -1 || par(go[u], v)))
                {
                    go[u] = v;
                    len[u] = lvl[a[u]] - lvl[v] + 1;
                }
            }
            ch.push_back(v);
        }
        else
        {
            int ans = 0;
            int best = rt[v];
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, v) && par(best, u))
                    best = u;
            }
            ans += lvl[v] - lvl[best];
            v = best;
            while (go[v] != -1)
            {
                ans += len[v];
                 v = go[v];
            }
            if (a[v] < n)
            {
                ans += lvl[a[v]] - lvl[rt[a[v]]] + 1;
                v = rt[a[v]];
            }
            cout << v + 1 << " " << ans + 1 << "\n";
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <memory.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <deque>

using namespace std;

typedef long long ll;

const int N = 200001;

int a[N];
int in[N];
int out[N];
int lvl[N];
int timer;
int go[N];
int rt[N];
int len[N];
vector<int> ch;
int root;
vector<int> g[N];

void dfs(int v, int level)
{
    lvl[v] = level;
    rt[v] = root;
    in[v] = timer++;
    for (int i = 0; i < g[v].size(); i++)
        dfs(g[v][i], level + 1);
    out[v] = timer++;
}
int n;
bool par(int pr, int v)
{
    return pr >= 0 && pr < n && v >= 0 && v < n && in[pr] <= in[v] && out[pr] >= out[v] && rt[v] == rt[pr];
}

int main()
{
//    freopen("in.txt", "r", stdin);
    int m;
    //cin >> n >> m;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]), a[i] += i;
    int T = max(1, (int)sqrt(m) / 2);
    for (int i = 0; i < m; i++)
    {
        if (i % T == 0)
        {
            for (int i = 0; i < n; i++)
                g[i].clear();
            for (int i = 0; i < n; i++)
                if (a[i] < n)
                    g[a[i]].push_back(i);
            for (int i = 0; i < n; i++)
                if (a[i] >= n)
                {
                    timer = 0;
                    root = i;
                    dfs(i, 0);
                }
            for (int i = 0; i < n; i++)
                go[i] = -1;
            ch.clear();
        }
        int type, v;
       // cin >> type >> v;
        scanf("%d %d", &type, &v);
        v--;
        if (type == 0)
        {
            int b;
            //cin >> b;
            scanf("%d", &b);
            a[v] = v + b;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, a[v]) && (best == -1 || par(best, u)))
                    best = u;
            }
            go[v] = -1;
            if (best != -1)
            {
                go[v] = best;
                len[v] = lvl[a[v]] - lvl[best] + 1;
            }
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (u == v)
                    continue;
                if (par(v, a[u]) && (go[u] == -1 || par(go[u], v)))
                {
                    go[u] = v;
                    len[u] = lvl[a[u]] - lvl[v] + 1;
                }
            }
            ch.push_back(v);
        }
        else
        {
            int ans = 0;
            int best = rt[v];
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, v) && par(best, u))
                    best = u;
            }
            ans += lvl[v] - lvl[best];
            v = best;
            while (go[v] != -1)
            {
                ans += len[v];
                 v = go[v];
            }
            if (a[v] < n)
            {
                ans += lvl[a[v]] - lvl[rt[a[v]]] + 1;
                v = rt[a[v]];
            }
            cout << v + 1 << " " << ans + 1 << "\n";
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <set>
#include <memory.h>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <deque>

using namespace std;

typedef long long ll;

const int N = 200001;

int a[N];
int in[N];
int out[N];
int lvl[N];
int timer;
int go[N];
int rt[N];
int len[N];
vector<int> ch;
int root;
vector<int> g[N];

void dfs(int v, int level)
{
    lvl[v] = level;
    rt[v] = root;
    in[v] = timer++;
    for (int i = 0; i < g[v].size(); i++)
        dfs(g[v][i], level + 1);
    out[v] = timer++;
}

bool par(int pr, int v)
{
    return in[pr] <= in[v] && out[pr] >= out[v] && rt[v] == rt[pr];
}

int main()
{
   // freopen("in.txt", "r", stdin);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        cin >> a[i], a[i] += i;
    int T = max(1, (int)sqrt(m));
    for (int i = 0; i < m; i++)
    {
        if (i % T == 0)
        {
            for (int i = 0; i < n; i++)
                g[i].clear();
            for (int i = 0; i < n; i++)
                if (a[i] < n)
                    g[a[i]].push_back(i);
            for (int i = 0; i < n; i++)
                if (a[i] >= n)
                {
                    timer = 0;
                    root = i;
                    dfs(i, 0);
                }
            for (int i = 0; i < n; i++)
                go[i] = -1;
        }
        int type, v;
        cin >> type >> v;
        v--;
        if (type == 0)
        {
            int b;
            cin >> b;
            a[v] = v + b;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (u == v)
                    continue;
                if (par(u, v) && (best == -1 || par(best, u)))
                    best = u;
            }
            if (best != -1)
            {
                go[v] = best;
                len[v] = lvl[a[v]] - lvl[best] + 1;
            }
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (u == v)
                    continue;
                if (par(v, u) && (go[u] == -1 || par(go[u], v)))
                {
                    go[u] = v;
                    len[u] = lvl[v] - lvl[u];
                }
            }
            ch.push_back(v);
        }
        else
        {
            int ans = 0;
            int best = -1;
            for (int i = 0; i < ch.size(); i++)
            {
                int u = ch[i];
                if (par(u, v) && (best == -1 || par(best, u)))
                    best = u;
            }
            if (best == -1)
            {
                ans = lvl[v] - lvl[rt[v]];
                v = rt[v];
            }
            else
            {
                ans += lvl[v] - lvl[best];
                v = best;
            }
            while (go[v] != -1)
            {
                ans += len[v];
                 v = go[v];
            }
            ans += lvl[v] - lvl[rt[v]];
            v = rt[v];
            cout << v + 1 << " " << ans + 1 << "\n";
        }
    }
    return 0;
}


#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <algorithm>
#include <queue>

#define PII pair<int, int>
using namespace std;
const int MAXLEN = 5e4;
char str[MAXLEN+5];
char outstr[MAXLEN+5];
int main()
{

    cin.sync_with_stdio(false);
    cin >> str;
    int len = strlen(str);
    outstr[len] = '\0';
    long ans = 0, c = 0;
    int a, b;
    bool flag = true;
    priority_queue<PII > q;
    for(int i=0;i < len;i++){
        if(str[i] == '('){
            c++;
            outstr[i] = '(';
        }
        else if(str[i] == ')'){
            outstr[i] = ')';
            c--;
            if(c < 0){
                if(!q.empty()){
                    PII p = q.top();
                    q.pop();
                    ans -= p.first;
                    outstr[p.second] = '(';
                    c += 2;
                }
                else{
                    flag = false;
                }
            }
        }
        else{
            outstr[i] = ')';
            cin >> a >> b;
            ans += b;
            PII p1(b-a, i);
            q.push(p1);
            c--;
            if(c < 0){
                PII p = q.top();
                q.pop();
                ans -= p.first;
                outstr[p.second] = '(';
                c += 2;
            }
        }
        //outstr[i+1] = '\0';
        //cout<< outstr<<endl;
    }
    if(c == 0 && flag){
        cout << ans << endl;
        cout << outstr << endl;
    }
    else{
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <algorithm>
#include <queue>

#define PII pair<int, int>
using namespace std;
const int MAXLEN = 5e4;
char str[MAXLEN+5];
char outstr[MAXLEN+5];
int main()
{

    cin.sync_with_stdio(false);
    cin >> str;
    int len = strlen(str);
    outstr[len] = '\0';
    long long ans = 0;
    int c = 0;
    int a, b;
    bool flag = true;
    priority_queue<PII > q;
    for(int i=0;i < len;i++){
        if(str[i] == '('){
            c++;
            outstr[i] = '(';
        }
        else if(str[i] == ')'){
            outstr[i] = ')';
            c--;
            if(c < 0){
                if(!q.empty()){
                    PII p = q.top();
                    q.pop();
                    ans -= p.first;
                    outstr[p.second] = '(';
                    c += 2;
                }
                else{
                    flag = false;
                }
            }
        }
        else{
            outstr[i] = ')';
            cin >> a >> b;
            ans += b;
            PII p1(b-a, i);
            q.push(p1);
            c--;
            if(c < 0){
                PII p = q.top();
                q.pop();
                ans -= p.first;
                outstr[p.second] = '(';
                c += 2;
            }
        }
        //outstr[i+1] = '\0';
        //cout<< outstr<<endl;
    }
    if(c == 0 && flag){
        cout << ans << endl;
        cout << outstr << endl;
    }
    else{
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <algorithm>
#include <queue>

#define PII pair<int, int>
using namespace std;
const int MAXLEN = 5e4;
char str[MAXLEN+5];
char outstr[MAXLEN+5];
int main()
{

    cin.sync_with_stdio(false);
    cin >> str;
    int len = strlen(str);
    outstr[len] = '\0';
    int ans = 0, c = 0;
    int a, b;
    bool flag = true;
    priority_queue<PII > q;
    for(int i=0;i < len;i++){
        if(str[i] == '('){
            c++;
            outstr[i] = '(';
        }
        else if(str[i] == ')'){
            outstr[i] = ')';
            c--;
            if(c < 0){
                if(!q.empty()){
                    PII p = q.top();
                    ans -= p.first;
                    outstr[p.second] = ')';
                    c += 2;
                }
                else{
                    flag = false;
                }
            }
        }
        else{
            outstr[i] = ')';
            cin >> a >> b;
            ans += b;
            PII p1(b-a, i);
            q.push(p1);
            c--;
            if(c < 0){
                PII p = q.top();
                ans -= p.first;
                outstr[p.second] = '(';
                c += 2;
            }
        }
    }
    if(c == 0 && flag){
        cout << ans << endl;
        cout << outstr << endl;
    }
    else{
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <algorithm>
#include <queue>

#define PII pair<int, int>
using namespace std;
const int MAXLEN = 5e4;
char str[MAXLEN+5];
char outstr[MAXLEN+5];
int main()
{

    cin.sync_with_stdio(false);
    cin >> str;
    int len = strlen(str);
    outstr[len] = '\0';
    int ans = 0, c = 0;
    int a, b;
    bool flag = true;
    priority_queue<PII > q;
    for(int i=0;i < len;i++){
        if(str[i] == '('){
            c++;
            outstr[i] = '(';
        }
        else if(str[i] == ')'){
            outstr[i] = ')';
            c--;
            if(c < 0){
                if(!q.empty()){
                    PII p = q.top();
                    q.pop();
                    ans -= p.first;
                    outstr[p.second] = '(';
                    c += 2;
                }
                else{
                    flag = false;
                }
            }
        }
        else{
            outstr[i] = ')';
            cin >> a >> b;
            ans += b;
            PII p1(b-a, i);
            q.push(p1);
            c--;
            if(c < 0){
                PII p = q.top();
                q.pop();
                ans -= p.first;
                outstr[p.second] = '(';
                c += 2;
            }
        }
        //outstr[i+1] = '\0';
        //cout<< outstr<<endl;
    }
    if(c == 0 && flag){
        cout << ans << endl;
        cout << outstr << endl;
    }
    else{
        cout << -1 << endl;
    }

    return 0;
}


#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

const int maxn = 5e+4;
int a[maxn], b[maxn];
char p[maxn];
priority_queue<tuple<int,int>> pq;

int main() {
    string s;
    cin>>s;
    int paren=0, score=0;
    for(int i=0;i<s.size();i++){
        switch(s[i]){
            case '?':
                cin>>a[i]>>b[i];
                pq.push(make_tuple(b[i]-a[i],i));
                p[i]=')';
                paren--;
                score+=b[i];
                break;
            case '(':
                p[i]='(';
                paren++;
                break;
            case ')':
                p[i]=')';
                paren--;
                break;
        }
        if(paren<0){
            if(pq.empty()){
                cout<<"-1"<<endl;
                return 0;
            }
            auto a=pq.top();
            pq.pop();
            score-=get<0>(a);
            paren+=2;
            p[get<1>(a)]='(';
        }
    }
    if(paren){
        cout<<"-1"<<endl;
        return 0;
    }else{
        cout<<score<<endl;
        for(int i=0;i<s.size();i++) cout<<p[i];
        cout<<endl;
        return 0;
    }
}


#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

const int maxn = 5e+4;
int a[maxn], b[maxn];
char p[maxn];
priority_queue<tuple<int,int>> pq;

int main() {
    string s;
    cin>>s;
    int paren=0, score=0;
    for(int i=0;i<s.size();i++){
        switch(s[i]){
            case '?':
                cin>>a[i]>>b[i];
                pq.push(make_tuple(b[i]-a[i],i));
                p[i]=')';
                paren--;
                score+=b[i];
                break;
            case '(':
                p[i]='(';
                paren++;
                break;
            case ')':
                p[i]=')';
                paren--;
                break;
        }
        if(paren<0){
            if(pq.empty()){
                cout<<"-1"<<endl;
                cout<<"impossible!"<<endl;
                return 0;
            }
            auto a=pq.top();
            pq.pop();
            score-=get<0>(a);
            paren+=2;
            p[get<1>(a)]='(';
        }
    }
    if(paren){
        cout<<"-1"<<endl;
        cout<<paren<<endl;
        return 0;
    }else{
        cout<<score<<endl;
        for(int i=0;i<s.size();i++) cout<<p[i];
        cout<<endl;
        return 0;
    }
}


#include <iostream>
#include <queue>
#include <tuple>

using namespace std;

const int maxn = 5e+4;
int a[maxn], b[maxn];
char p[maxn];
priority_queue<tuple<int,int>> pq;

int main() {
    string s;
    cin>>s;
    long long paren=0, score=0;
    for(int i=0;i<s.size();i++){
        switch(s[i]){
            case '?':
                cin>>a[i]>>b[i];
                pq.push(make_tuple(b[i]-a[i],i));
                p[i]=')';
                paren--;
                score+=b[i];
                break;
            case '(':
                p[i]='(';
                paren++;
                break;
            case ')':
                p[i]=')';
                paren--;
                break;
        }
        if(paren<0){
            if(pq.empty()){
                cout<<"-1"<<endl;
                return 0;
            }
            auto a=pq.top();
            pq.pop();
            score-=get<0>(a);
            paren+=2;
            p[get<1>(a)]='(';
        }
    }
    if(paren){
        cout<<"-1"<<endl;
        return 0;
    }else{
        cout<<score<<endl;
        for(int i=0;i<s.size();i++) cout<<p[i];
        cout<<endl;
        return 0;
    }
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#define ll long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
void read(int &x) {
    x = 0; int flag = 1; char ch = ' ';
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
void read(ll &x) {
    x = 0; int flag = 1; char ch = ' ';
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
using namespace std;
const int maxn = 50010;
priority_queue < pair <int, int> > pq;
char s[maxn];
int n, b[maxn], a[maxn];
ll ans;
int main()
{
    scanf("%s", s + 1);
    n = strlen(s + 1);
    rep(i, 1, n)
        if(s[i] == '?') read(a[i]), read(b[i]);
    int cur = 0;
    rep(i, 1, n) {
        if(s[i] == '(') cur++;
            else cur--;
        if(s[i] == '?') {
            s[i] = ')';
            pq.push(make_pair(b[i] - a[i], i));
            ans += b[i];
        }
        if(cur < 0) {
            if(pq.empty()) {
                puts("-1");
                return 0;
            }
            pair <int, int> tmp = pq.top();
            pq.pop();
            s[tmp.second] = '(';
            ans -= tmp.first;
            cur += 2;
        }
    }
    if(cur != 0) {
        puts("-1");
        return 0;
    }
    printf("%lld\n", ans);
    rep(i, 1, n) putchar(s[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#define ll long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
void read(int &x) {
    x = 0; int flag = 1; char ch = ' ';
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
void read(ll &x) {
    x = 0; int flag = 1; char ch = ' ';
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
using namespace std;
const int maxn = 50010;
priority_queue < pair <int, int> > pq;
char s[maxn];
int n, b[maxn], a[maxn];
ll ans;
int main()
{
    scanf("%s", s + 1);
    n = strlen(s + 1);
    rep(i, 1, n)
        if(s[i] == '?') read(a[i]), read(b[i]);
    int cur = 0;
    rep(i, 1, n) {
        if(s[i] == '(') cur++;
            else cur--;
        if(s[i] == '?') {
            s[i] = ')';
            pq.push(make_pair(b[i] - a[i], i));
            ans += b[i];
        }
        if(cur < 0) {
            if(pq.empty()) {
                puts("-1");
                return 0;
            }
            pair <int, int> tmp = pq.top();
            pq.pop();
            s[tmp.second] = '(';
            ans -= tmp.first;
            cur += 2;
        }
    }
    printf("%lld\n", ans);
    rep(i, 1, n) putchar(s[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#define ll long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
void read(int &x) {
    x = 0; int flag = 1; char ch = ' ';
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
void read(ll &x) {
    x = 0; int flag = 1; char ch = ' ';
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
using namespace std;
const int maxn = 50010;
priority_queue < pair <int, int> > pq;
char s[maxn];
int n, ans, b[maxn], a[maxn];
int main()
{
    scanf("%s", s + 1);
    n = strlen(s + 1);
    rep(i, 1, n)
        if(s[i] == '?') read(a[i]), read(b[i]);
    int cur = 0;
    rep(i, 1, n) {
        if(s[i] == '(') cur++;
            else cur--;
        if(s[i] == '?') {
            s[i] = ')';
            pq.push(make_pair(b[i] - a[i], i));
            ans += b[i];
        }
        if(cur < 0) {
            if(pq.empty()) {
                puts("-1");
                return 0;
            }
            pair <int, int> tmp = pq.top();
            pq.pop();
            s[tmp.second] = '(';
            ans -= tmp.first;
            cur += 2;
        }
    }
    printf("%d\n", ans);
    rep(i, 1, n) putchar(s[i]);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

char shit[55555];
string s;
int c=0;
int pos[55555];
pii pnt[55555];
int ans=0;

bool cmp(pii a,pii b) {
	return a.first<b.first;
}

int main() {
	ios::sync_with_stdio(false);
	scanf("%s",shit);
	s=shit;
	int n=s.size();
	FOR(i,0,n) {
		if(s[i]=='?') {
			pos[c]=pnt[c].second=i;
			int a,b;
			cin>>a>>b;
			pnt[c].first=a-b;
			c++;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0;
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
			if(cnt<0) {
				int bound=upper_bound(pos,pos+c,i)-pos;
				if(bound==0) {
					printf("-1\n");
					return 0;
				}
				sort(pnt,pnt+bound,cmp);
				int p=pnt[0].second,cst=pnt[0].first;
				if(cst==inf) {
					printf("-1\n");
					return 0;
				}
				cnt+=2;
				ans+=cst;
				s[p]='(';
				pnt[0].first=inf;
			}
		}
	}
	if(cnt!=0) {
		printf("-1\n");
		return 0;
	}
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
			if(cnt<0) {
				printf("-1\n");
				return 0;
			}
		}
	}
	printf("%d\n",ans);
	printf("%s\n",s.c_str());

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int val[55555];
bool is[55555];
ll ans=0;
bool operator<(pii a,pii b){
	return a.first<b.first;
}
priority_queue<pii,vector<pii>,greater<pii> >q;
int cst;

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	FOR(i,0,s.size()) {
		if(s[i]=='?') {
			int a,b;
			cin>>a>>b;
			is[i]=1;
			val[i]=a-b;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0,p;
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			if(is[i])q.push({val[i],i});
			cnt--;
			if(cnt<0) {
				if(q.size()==0) {
					cout<<-1<<endl;
					return 0;
				}
				cst=q.top().first;
				p=q.top().second;
				q.pop();
				cnt+=2;
				ans+=cst;
				s[p]='(';
			}
		}
	}
	if(cnt!=0) {
		cout<<-1<<endl;
		return 0;
	}
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<ans<<endl;
	cout<<s<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
pii cost[55555];
int cnt[55555];
int rcnt[55555];
int num=0;
int qcnt[55555];
int rqcnt[55555];
ll ans=linf;
string anss;
int ct=0;
ll cst=0;
void dfs(int i){
	if(i==s.size())if(ct==0){
		if(ans>cst){
			ans=cst;
			anss=s;
		}
		return;
	}
	if(s[i]!='?'){
		dfs(i+1);
		return;
	}
	int a,b;
	a=cost[i].first;
	b=cost[i].second;
	{
		ct++;
		cst+=a;
		s[i]='(';
		if(rcnt[i]-(num-ct)<=0)
		if(cnt[i]+ct>=0)
		dfs(i+1);
		s[i]='?';
		cst-=a;
		ct--;

  }
	{
		ct--;
		cst+=b;
		s[i]=')';
		if(rcnt[i]-(num-ct)<=0)
		if(cnt[i]+ct>=0)
		dfs(i+1);
		s[i]='?';
		cst-=b;
		ct++;

  }
}

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	FOR(i,0,s.size()){
		if(s[i]=='?'){
			cin>>cost[i].first>>cost[i].second;
		}
	}
	for(int i=s.size()-1;i>=0;i--){
		rqcnt[i]=rqcnt[i+1]+(s[i]=='?');
	}
	for(int i=s.size()-1;i>=0;i--){
	  rcnt[i]=(s[i]=='('?1:(s[i]==')'?-1:0));
		rcnt[i]+=rcnt[i+1];
		if(rcnt[i]-rqcnt[i]>0){
			cout<<-1<<endl;
			return 0;
		}
	}
	FOR(i,0,s.size()){
		cnt[i]=(s[i]=='('?1:(s[i]==')'?-1:0));
		cnt[i]+=(i==0?0:cnt[i-1]);
		qcnt[i]=(s[i]=='?'?1:0);
		qcnt[i]+=(i==0?0:qcnt[i-1]);
		if(qcnt[i]+cnt[i]<0){
			cout<<-1<<endl;
			return 0;
		}
	}
	dfs(0);
	cout<<ans<<endl;
	cout<<anss<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int is[55555];
int ans=0;
priority_queue<pii,vector<pii>,greater<pii> >q;
int cst;

int main() {
	ios::sync_with_stdio(false);
	memset(is,-1,sizeof(is));
	cin>>s;
	FOR(i,0,s.size()) {
		if(s[i]=='?') {
			int a,b;
			cin>>a>>b;
			is[i]=a-b;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0,p;
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			if(is[i]!=-1)q.push({is[i],i});
			cnt--;
			if(cnt<0) {
				if(q.size()==0) {
					cout<<-1<<endl;
					return 0;
				}
				cst=q.top().first;
				p=q.top().second;
				q.pop();
				cnt+=2;
				ans+=cst;
				s[p]='(';
			}
		}
	}
	if(cnt!=0) {
		cout<<-1<<endl;
		return 0;
	}
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<ans<<endl;
	cout<<s<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int pos[55555];
pii pnt[55555];
int ans=0;
char cc[55555];

bool cmp(pii a,pii b){
    return a.first<b.first;
}

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	int n=s.size();
	int a,b;
	FOR(i,0,n) {
		if(s[i]=='?') {
			pos[c]=pnt[c].second=i;
			cin>>a>>b;
			pnt[c].first=a-b;
			c++;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0;
	int bound=0;
	int p,cst;
	pos[c]=pos[c+1]=pos[c+2]=inf;
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
			if(cnt<0) {
			    while(pos[bound]<=i)bound++;
				if(bound==0) {
				    cout<<-1<<endl;
					return 0;
				}
				sort(pnt,pnt+bound,cmp);
				p=pnt[0].second;cst=pnt[0].first;
				if(cst==inf) {
				    cout<<-1<<endl;
					return 0;
				}
				cnt+=2;
				ans+=cst;
				s[p]='(';
				pnt[0].first=inf;
			}
		}
	}
	if(cnt!=0) {
	    cout<<-1<<endl;
		return 0;
	}
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<ans<<endl<<s<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int pos[55555];
pii pnt[55555];
int ans=0;

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	FOR(i,0,s.size()) {
		if(s[i]=='?') {
			pos[c]=pnt[c].second=i;
			int a,b;
			cin>>a>>b;
			pnt[c].first=a-b;
			c++;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0;
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
			if(cnt<0) {
				int bound=upper_bound(pos,pos+c,i)-pos;
				if(bound==0) {
					cout<<-1<<endl;
					return 0;
				}
				sort(pnt,pnt+bound);
				int p=pnt[0].second,cst=pnt[0].first;
				if(cst==inf) {
					cout<<-1<<endl;
					return 0;
				}
				cnt+=2;
				ans+=cst;
				s[p]='(';
				pnt[0].first=inf;
			}
		}
	}
	if(cnt!=0) {
		cout<<-1<<endl;
		return 0;
	}
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<ans<<endl;
	cout<<s<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
pii cost[55555];
int cnt[55555];
int rcnt[55555];
int num=0;
int qcnt[55555];
int rqcnt[55555];
ll ans=linf;
string anss;
int ct=0;
ll cst=0;
void dfs(int i) {
	if(i==s.size()){
		if(ct+cnt[s.size()-1]==0)
			if(ans>cst) {
				ans=cst;
				anss=s;
			}
			return;
	}
	if(s[i]!='?') {
		dfs(i+1);
		return;
	}
	int a,b;
	a=cost[i].first;
	b=cost[i].second;
	{
		int x;
		x=ct+1;
		x+=cnt[s.size()-1];
		x-=rqcnt[i+1];
		if(x<=0) {
			ct++;
			cst+=a;
			s[i]='(';
			dfs(i+1);
			s[i]='?';
			cst-=a;
			ct--;
		}
	}
	{
		int x;
		x=ct-1;
		x+=cnt[s.size()-1];
		x+=rqcnt[i+1];
		if(x>=0) {
			ct--;
			cst+=b;
			s[i]=')';
			dfs(i+1);
			s[i]='?';
			cst-=b;
			ct++;
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	FOR(i,0,s.size()) {
		if(s[i]=='?'){
			cin>>cost[i].first>>cost[i].second;
		}
	}
	for(int i=s.size()-1; i>=0; i--) {
		rqcnt[i]=rqcnt[i+1]+(s[i]=='?');
	}
	for(int i=s.size()-1; i>=0; i--) {
		rcnt[i]=(s[i]=='('?1:(s[i]==')'?-1:0));
		rcnt[i]+=rcnt[i+1];
		if(rcnt[i]-rqcnt[i]>0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	FOR(i,0,s.size()) {
		cnt[i]=(s[i]=='('?1:(s[i]==')'?-1:0));
		cnt[i]+=(i==0?0:cnt[i-1]);
		qcnt[i]=(s[i]=='?'?1:0);
		qcnt[i]+=(i==0?0:qcnt[i-1]);
		if(qcnt[i]+cnt[i]<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	dfs(0);
	cout<<ans<<endl;
	cout<<anss<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int pos[55555];
pii pnt[55555];
int ans=0;
char cc[55555];

bool cmp(pii a,pii b){
    return a.first<b.first;
}

int main() {
	ios::sync_with_stdio(false);
	scanf("%s",cc);
	s=cc;
	int n=s.size();
	FOR(i,0,n) {
		if(s[i]=='?') {
			pos[c]=pnt[c].second=i;
			int a,b;
			scanf("%d%d",&a,&b);
			pnt[c].first=a-b;
			c++;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0;
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
			if(cnt<0) {
				int bound=upper_bound(pos,pos+c,i)-pos;
				if(bound==0) {
				    printf("-1\n");
					return 0;
				}
				sort(pnt,pnt+bound,cmp);
				int p=pnt[0].second,cst=pnt[0].first;
				if(cst==inf) {
				    printf("-1\n");
					return 0;
				}
				cnt+=2;
				ans+=cst;
				s[p]='(';
				pnt[0].first=inf;
			}
		}
	}
	if(cnt!=0) {
		printf("-1\n");
		return 0;
	}
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {

			printf("-1\n");
			return 0;
		}
	}
	printf("%d\n",ans);
	printf("%s\n",s.c_str());

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int val[55555];
bool is[55555];
int ans=0;
bool operator<(pii a,pii b){
	return a.first<b.first;
}
priority_queue<pii,vector<pii>,greater<pii> >q;
int cst;

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	FOR(i,0,s.size()) {
		if(s[i]=='?') {
			int a,b;
			cin>>a>>b;
			is[i]=1;
			val[i]=a-b;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0,p;
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			if(is[i])q.push({val[i],i});
			cnt--;
			if(cnt<0) {
				if(q.size()==0) {
					cout<<-1<<endl;
					return 0;
				}
				cst=q.top().first;
				p=q.top().second;
				q.pop();
				cnt+=2;
				ans+=cst;
				s[p]='(';
			}
		}
	}
	if(cnt!=0) {
		cout<<-1<<endl;
		return 0;
	}
	FOR(i,0,s.size()) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<ans<<endl;
	cout<<s<<endl;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;
typedef vector<vi> mat;

inline ll read() {
	ll Hashimoto=0,Kanna=1;
	char I_Love=getchar();
	while(I_Love<'0'||I_Love>'9') {
		if(I_Love=='-')Kanna=-1;
		I_Love=getchar();
	}
	while(I_Love>='0'&&I_Love<='9') {
		Hashimoto=Hashimoto*10+I_Love-'0';
		I_Love=getchar();
	}
	return Hashimoto*Kanna;
}

#define I_Love_Hashimoto_Kanna main
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define filein(s) freopen(s,"r",stdin);
#define fileout(s) freopen(s,"w",stdout);
#define file freopen("I_Love_Hashimoto_Kanna.out","w",stdout);
#define RE cout<<"I_Love_Hashimoto_Kanna"<<endl;
#define Tone(Kanna) cout<<Kanna<<endl;
#define Tall(Hashimoto,Kanna) for(int I_Love=0;I_Love<Kanna;I_Love++)cout<<Hashimoto[I_Love]<<(I_Love==Kanna-1?"\n":" ");
#define FOR(I_Love,Hashimoto,Kanna) for(int I_Love=Hashimoto;I_Love<Kanna;I_Love++)
#define MP(Hashimoto,Kanna) make_pair(Hashimoto,Kanna)
#define REV(Kanna) reverse(Kanna.begin(),Kanna.end());
#define SORT(Kanna) sort(Kanna.begin(),Kanna.end());
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

string s;
ll a[55555],b[55555];
bool is[55555];
priority_queue<pli,vpli,greater<pli> >q;

int I_Love_Hashimoto_Kanna() {
	fastio;
	cin>>s;
	ll ans=0;
	int check=0;
	FOR(i,0,s.size()) {
		if(s[i]=='?') {
			is[i]=1;
			s[i]='(';
			cin>>a[i]>>b[i];
			ans+=a[i];
		}
		if(s[i]=='('){
			check++;
		}
		else check--;
	}
	if(check<0){
		cout<<-1<<endl;
		return 0;
	}
	int cnt=0;
	for(int i=s.size()-1; i>=0; i--) {
		if(s[i]==')')cnt++;
		else cnt--;
		if(is[i]) {
			q.push({b[i]-a[i],i});
		}
		while(cnt<0) {
			if(q.empty()) {
				cout<<-1<<endl;
				return 0;
			}
			ans+=q.top().first;
			s[q.top().second]=')';
			q.pop();
			cnt+=2;
		}
	}

	while(cnt<0) {
		if(q.empty()) {
			cout<<-1<<endl;
			return 0;
		}
		ans+=q.top().first;
		s[q.top().second]=')';
		q.pop();
		cnt++;
	}
	cout<<ans<<endl;
	Tone(s);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
int c=0;
int pos[55555];
pii pnt[55555];
int ans=0;
char cc[55555];

bool cmp(pii a,pii b){
    return a.first<b.first;
}

int main() {
	ios::sync_with_stdio(false);
	scanf("%s",cc);
	s=cc;
	int n=s.size();
	FOR(i,0,n) {
		if(s[i]=='?') {
			pos[c]=pnt[c].second=i;
			int a,b;
			scanf("%d%d",&a,&b);
			pnt[c].first=a-b;
			c++;
			s[i]=')';
			ans+=b;
		}
	}
	int cnt=0;
	int bound=0;
	pos[c]=pos[c+1]=pos[c+2]=inf;
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
			if(cnt<0) {
			    while(pos[bound]<=i)bound++;
				if(bound==0) {
				    printf("-1\n");
					return 0;
				}
				sort(pnt,pnt+bound,cmp);
				int p=pnt[0].second,cst=pnt[0].first;
				if(cst==inf) {
				    printf("-1\n");
					return 0;
				}
				cnt+=2;
				ans+=cst;
				s[p]='(';
				pnt[0].first=inf;
			}
		}
	}
	if(cnt!=0) {
		printf("-1\n");
		return 0;
	}
	FOR(i,0,n) {
		if(s[i]=='(')cnt++;
		else {
			cnt--;
		}
		if(cnt<0) {

			printf("-1\n");
			return 0;
		}
	}
	printf("%d\n",ans);
	printf("%s\n",s.c_str());

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;
typedef vector<vi> mat;

inline ll read() {
	ll Hashimoto=0,Kanna=1;
	char I_Love=getchar();
	while(I_Love<'0'||I_Love>'9') {
		if(I_Love=='-')Kanna=-1;
		I_Love=getchar();
	}
	while(I_Love>='0'&&I_Love<='9') {
		Hashimoto=Hashimoto*10+I_Love-'0';
		I_Love=getchar();
	}
	return Hashimoto*Kanna;
}

#define I_Love_Hashimoto_Kanna main
#define fastio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define filein(s) freopen(s,"r",stdin);
#define fileout(s) freopen(s,"w",stdout);
#define file freopen("I_Love_Hashimoto_Kanna.out","w",stdout);
#define RE cout<<"I_Love_Hashimoto_Kanna"<<endl;
#define Tone(Kanna) cout<<Kanna<<endl;
#define Tall(Hashimoto,Kanna) for(int I_Love=0;I_Love<Kanna;I_Love++)cout<<Hashimoto[I_Love]<<(I_Love==Kanna-1?"\n":" ");
#define FOR(I_Love,Hashimoto,Kanna) for(int I_Love=Hashimoto;I_Love<Kanna;I_Love++)
#define MP(Hashimoto,Kanna) make_pair(Hashimoto,Kanna)
#define REV(Kanna) reverse(Kanna.begin(),Kanna.end());
#define SORT(Kanna) sort(Kanna.begin(),Kanna.end());
#define inf (int)1e9
#define linf (ll)1e18
#define mod (inf+7)

string s;
ll a[55555],b[55555];
bool is[55555];
priority_queue<pli,vpli,greater<pli> >q;

int I_Love_Hashimoto_Kanna() {
	fastio;
	cin>>s;
	ll ans=0;
	int check=0;
	FOR(i,0,s.size()) {
		if(s[i]=='?') {
			is[i]=1;
			s[i]='(';
			cin>>a[i]>>b[i];
			ans+=a[i];
		}
		if(s[i]=='('){
			check++;
		}
		else check--;
	  if(check<0){
		  cout<<-1<<endl;
		  return 0;
	  }
	}
	int cnt=0;
	for(int i=s.size()-1; i>=0; i--) {
		if(s[i]==')')cnt++;
		else cnt--;
		if(is[i]) {
			q.push({b[i]-a[i],i});
		}
		while(cnt<0) {
			if(q.empty()) {
				cout<<-1<<endl;
				return 0;
			}
			ans+=q.top().first;
			s[q.top().second]=')';
			q.pop();
			cnt+=2;
		}
	}

	while(cnt<0) {
		if(q.empty()) {
			cout<<-1<<endl;
			return 0;
		}
		ans+=q.top().first;
		s[q.top().second]=')';
		q.pop();
		cnt++;
	}
	cout<<ans<<endl;
	Tone(s);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iomanip>
#include<vector>
#include<queue>
#include<deque>
#include<string>
#include<utility>
#include<algorithm>
#include<cctype>
#include<set>
#include<map>
#include<bitset>
#include<stack>
#include<ctime>

using namespace std;

typedef long long ll;
typedef short int si;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<pli> vpli;

#define st first
#define nd second
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REV(a) reverse(a.begin(),a.end())
#define SORT(a) sort(a.begin(),a.end())
#define inf (int)1e9
#define linf (ll)1e18

string s;
pii cost[55555];
int cnt[55555];
int rcnt[55555];
int num=0;
int qcnt[55555];
int rqcnt[55555];
ll ans=linf;
string anss;
int ct=0;
ll cst=0;
void dfs(int i) {
	if(i==s.size()){
		if(ct==0)
			if(ans>cst) {
				ans=cst;
				anss=s;
			}
			return;
	}
	if(s[i]!='?') {
		dfs(i+1);
		return;
	}
	int a,b;
	a=cost[i].first;
	b=cost[i].second;
	{
		int x;
		x=ct+1;
		x+=cnt[s.size()-1];
		x-=rqcnt[i+1];
		if(x<=0) {
			ct++;
			cst+=a;
			s[i]='(';
			dfs(i+1);
			s[i]='?';
			cst-=a;
			ct--;
		}
	}
	{
		int x;
		x=ct-1;
		x+=cnt[s.size()-1];
		x+=rqcnt[i+1];
		if(x>=0) {
			ct--;
			cst+=b;
			s[i]=')';
			dfs(i+1);
			s[i]='?';
			cst-=b;
			ct++;
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin>>s;
	FOR(i,0,s.size()) {
		if(s[i]=='?'){
			cin>>cost[i].first>>cost[i].second;
		}
	}
	for(int i=s.size()-1; i>=0; i--) {
		rqcnt[i]=rqcnt[i+1]+(s[i]=='?');
	}
	for(int i=s.size()-1; i>=0; i--) {
		rcnt[i]=(s[i]=='('?1:(s[i]==')'?-1:0));
		rcnt[i]+=rcnt[i+1];
		if(rcnt[i]-rqcnt[i]>0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	FOR(i,0,s.size()) {
		cnt[i]=(s[i]=='('?1:(s[i]==')'?-1:0));
		cnt[i]+=(i==0?0:cnt[i-1]);
		qcnt[i]=(s[i]=='?'?1:0);
		qcnt[i]+=(i==0?0:qcnt[i-1]);
		if(qcnt[i]+cnt[i]<0) {
			cout<<-1<<endl;
			return 0;
		}
	}
	dfs(0);
	cout<<ans<<endl;
	cout<<anss<<endl;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    priority_queue< pair<long long,long long> >p;
    long long int i,j,n=s.size(),x,y,a,b,t=0;
    long long int sum=0;
    for(i=0;i<n;i++)
    {
        if(s[i]=='(')
        {
            t++;
        }
        else if(s[i]==')')
        {
            t--;
        }
        else
        {
            cin>>a>>b;
            p.push({b-a,i});
            sum+=b;
            s[i]=')';
            t--;
        }
        if(t<0)
        {
            if(p.empty())
                break;
            pair<long,long>q=p.top();
            s[q.second]='(';
            sum-=q.first;
            p.pop();
            t+=2;
        }
    }
    if(t)
        cout<<"-1"<<endl;
    else
        cout<<sum<<endl<<s<<endl;
}


#include<bits/stdc++.h>
using namespace std;
stack<char>st;
long maxi=INT_MAX,result=0,pos=0;
string old="",gold;
void fun(string s,long i,long n,vector<pair<long,long> > v)
{
    long long int a,b;
    if(i<n)
    {
        if(s[i]==')')
        {
            if(st.size()==0||st.top()==')')
            {
                old+='(';
                st.push(')');
                fun(s,i+1,n,v);
                st.pop();
                old.pop_back();
            }
            else
            {
                st.pop();
                old+=')';
                fun(s,i+1,n,v);
                st.push('(');
                old.pop_back();
            }
        }
        else if(s[i]=='(')
        {
            old+='(';
            st.push('(');
            fun(s,i+1,n,v);
            st.pop();
            old.pop_back();
        }
        else
        {
            s[i]='(';
            result+=v[pos].first;
            pos++;
            fun(s,i,n,v);
            pos--;
            result-=v[pos].first;
            s[i]=')';
            result+=v[pos].second;
            pos++;
            fun(s,i,n,v);
            pos--;
            result-=v[pos].second;
        }
    }
    else
    {
        if(st.size()==0)
        {
            maxi=min(maxi,result);
            if(maxi==result)
                gold=old;
        }
    }
}
int main()
{
    string s;
    cin>>s;
    long int i,j,n=s.size(),x,y;
    vector<pair<long,long>>v;
    for(i=0;i<n;i++)
    {
        if(s[i]=='?')
        {
            cin>>x>>y;
            v.push_back({x,y});
        }
    }
    fun(s,0,n,v);
    if(maxi==INT_MAX)
        cout<<"-1"<<endl;
    else
        cout<<maxi<<endl<<gold<<endl;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    cin>>s;
    priority_queue< pair<long,long> >p;
    long int i,j,n=s.size(),x,y,a,b,sum=0,t=0;
    for(i=0;i<n;i++)
    {
        if(s[i]=='(')
        {
            t++;
        }
        else if(s[i]==')')
        {
            t--;
        }
        else
        {
            cin>>a>>b;
            p.push({b-a,i});
            sum+=b;
            s[i]=')';
            t--;
        }
        if(t<0)
        {
            if(p.empty())
                break;
            pair<long,long>q=p.top();
            s[q.second]='(';
            sum-=q.first;
            p.pop();
            t+=2;
        }
    }
    if(t)
        cout<<"-1"<<endl;
    else
        cout<<sum<<endl<<s<<endl;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <set>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
char s[50010];
bool p[50010];
int n;
set<pii>ss;
int ans;
int main(){
	cin>>s;
	int cnt=0;
	for(int i=0;s[i];i++){
		if(s[i]=='(')cnt++;
		else if(s[i]==')')cnt--;
		else{
			int a,b;
			cin>>a>>b;
			ans+=b;
			s[i]==')';
			cnt--;
			ss.insert(mp(a-b,i));
		}
		while(cnt<0){
			if(ss.empty()){
				puts("-1");
				return 0;
			}
			ans+=ss.begin()->first;
			s[ss.begin()->second]='(';
			ss.erase(ss.begin());
			cnt+=2;
		}
	}
	if(cnt>0)puts("-1");
	else cout<<ans<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <set>
#define LL long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
char s[50010];
int n;
set<pii>ss;
int ans;
int cnt;
int main(){
	cin>>s;
	for(int i=0;s[i];i++){
		if(s[i]=='(')cnt++;
		else if(s[i]==')')cnt--;
		else{
			int a,b;
			cin>>a>>b;
			ans+=b;
			s[i]=')';
			cnt--;
			ss.insert(mp(a-b,i));
		}
		while(cnt<0){
			if(ss.empty()){
				puts("-1");
				return 0;
			}
			ans+=ss.begin()->first;
			s[ss.begin()->second]='(';
			ss.erase(ss.begin());
			cnt+=2;
		}
	}
	if(cnt>0)puts("-1");
	else cout<<ans<<endl<<s<<endl;
	return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdio>
#include <set>
#define int long long
#define pb push_back
#define mp make_pair
#define INF 0x3f3f3f3f
#define Inf 1000000000000000000LL
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
char s[50010];
int n;
set<pii>ss;
int ans;
int cnt;
signed main(){
	cin>>s;
	for(int i=0;s[i];i++){
		if(s[i]=='(')cnt++;
		else if(s[i]==')')cnt--;
		else{
			int a,b;
			cin>>a>>b;
			ans+=b;
			s[i]=')';
			cnt--;
			ss.insert(mp(a-b,i));
		}
		while(cnt<0){
			if(ss.empty()){
				puts("-1");
				return 0;
			}
			ans+=ss.begin()->first;
			s[ss.begin()->second]='(';
			ss.erase(ss.begin());
			cnt+=2;
		}
	}
	if(cnt>0)puts("-1");
	else cout<<ans<<endl<<s<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define mp make_pair
#define LL long long
#define F first
#define S second
using namespace std;
typedef pair<int,int>pii;
int lowbit(int x){return x&(-x);}
const int maxn=4e5+10;
const LL INF=1e11+19;
map<int,LL>dp[maxn];
map<int,int>ans[maxn];
string s;
int l[maxn],r[maxn];
LL a[maxn],b[maxn];
LL getf(int x,int y){
	if(y<0||y>r[x])return INF;
	if(dp[x].find(y)!=dp[x].end())return dp[x][y];
	if(x==0){
		if(y==0)return dp[x][y]=0;
		return dp[x][y]=INF;
	}
	dp[x][y]=INF;
	if(s[x]=='(')dp[x][y]=getf(x-1,y-1);
	else if(s[x]==')')dp[x][y]=getf(x-1,y+1);
	else{
		if(getf(x-1,y-1)+a[x]<dp[x][y]){
			dp[x][y]=getf(x-1,y-1)+a[x];
			ans[x][y]=0;
		}
		if(getf(x-1,y+1)+b[x]<dp[x][y]){
			dp[x][y]=getf(x-1,y+1)+b[x];
			ans[x][y]=1;
		}
	}
	return dp[x][y];
}
void solve(int x,int y){
	if(x==0)return;
	if(s[x]!='?'){
		if(s[x]=='(')solve(x-1,y-1);
		else solve(x-1,y+1);
		cout<<s[x];
	}else if(ans[x][y]==0){
		solve(x-1,y-1);
		cout<<'(';
	}else{
		solve(x-1,y+1);
		cout<<')';
	}
}
int main(){
	cin>>s;
	int len=s.size();
	s=" "+s;
	r[0]=0;
	for(int i=1;i<=len;i++){
		if(s[i]=='(')r[i]=r[i-1]+1;
		else if(s[i]==')')r[i]=r[i-1]-1;
		else{
			r[i]=r[i-1]+1;
			cin>>a[i]>>b[i];
		}
	}
	cout<<getf(len,0)<<endl;
	solve(len,0);
	cout<<endl;
	return 0;
}


#include<bits/stdc++.h>

using namespace std;
string s;
int n, m = 0;
pair<int, int> a[60000];
long long ans = LLONG_MAX;
bool foundans = false;
string sol;
void go(int id, int opencnt, int closecnt, long long sum) {
    if (closecnt > opencnt)return;
    if (id == n) {
        if (opencnt == closecnt) {
            ans = min(ans, sum);
            foundans = true;
            sol=s;
        }
        return;

    }
    if(s[id]=='?'){
        s[id]='(';
        go(id + 1, opencnt + 1, closecnt, sum + a[id].first);
        s[id]=')';
        go(id + 1, opencnt, closecnt+1, sum + a[id].second);
        s[id]='?';
    }else if(s[id]=='('){
        go(id + 1, opencnt + 1, closecnt, sum);
    }else if(s[id]==')'){
        go(id + 1, opencnt, closecnt+1, sum);
    }else{
        cerr<<"sdfsdsdfdsffds\n";
        exit(0);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> s;
    n = s.size();
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?') {
            m++;
            cin >> a[i].first >> a[i].second;
        } else {
            a[i].first = 0;
            a[i].second = 0;
        }
       // cout << a[i].first << " " << a[i].second << endl;
    }
    go(0, 0, 0, 0);
    if (foundans) {
        cout << ans << endl;
        cout<<sol<<endl;
    } else {
        cout << -1 << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    int n,m=0;
    cin >> s;
    n = s.size();
    int a,b;
    priority_queue<pair<int,int> >q;
    int cnt=0;
    long long ans=0ll;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?') {
            cin >> a >> b;
            q.push({b-a,i});
            ans+=b;
            s[i]=')';
            cnt--;
        }else if(s[i]==')'){
            cnt--;
        }else{
            cnt++;
        }
        if(cnt<0){
            if(q.empty()){
                cout<<-1<<endl;
                return 0;
            }else{
                auto x=q.top();
                q.pop();
                s[x.second]='(';
                cnt+=2;
                ans-=x.first;
            }
        }
      //  cout<<s<<endl;
    }
    if(cnt>0){
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    cout<<s<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    int n,m=0;
    cin >> s;
    n = s.size();
    int a,b;
    priority_queue<pair<int,int> >q;
    int cnt=0;
    long long ans=0ll;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?') {
            cin >> a >> b;
            q.push({b-a,i});
            ans+=b;
            s[i]=')';
            cnt--;
        }else if(s[i]==')'){
            cnt--;
        }else{
            cnt++;
        }
        if(cnt<0){
            if(q.empty()){
                cout<<-1<<endl;
                return 0;
            }else{
                auto x=q.top();
                s[x.second]='(';
                cnt+=2;
                ans-=x.first;
                q.pop();
            }
        }
    }
    cout<<ans<<endl;
    cout<<s<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
string s;
int n, m = 0;
pair<int, int> a[60000];
long long ans = LLONG_MAX;
bool foundans = false;
string sol;
void go(int id, int opencnt, int closecnt, long long sum) {
    if (closecnt > opencnt)return;
    if (id == n) {
        if (opencnt == closecnt) {
            if(sum<ans){
                sol=s;
                ans = min(ans, sum);
                foundans = true;
            }
        }
        return;

    }
    if(s[id]=='?'){
        s[id]='(';
        go(id + 1, opencnt + 1, closecnt, sum + a[id].first);
        s[id]=')';
        go(id + 1, opencnt, closecnt+1, sum + a[id].second);
        s[id]='?';
    }else if(s[id]=='('){
        go(id + 1, opencnt + 1, closecnt, sum);
    }else if(s[id]==')'){
        go(id + 1, opencnt, closecnt+1, sum);
    }else{
        cerr<<"sdfsdsdfdsffds\n";
        exit(0);
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> s;
    n = s.size();
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?') {
            m++;
            cin >> a[i].first >> a[i].second;
        } else {
            a[i].first = 0;
            a[i].second = 0;
        }
       // cout << a[i].first << " " << a[i].second << endl;
    }
    go(0, 0, 0, 0);
    if (foundans) {
        cout << ans << endl;
        cout<<sol<<endl;
    } else {
        cout << -1 << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    int n,m=0;
    cin >> s;
    n = s.size();
    int a,b;
    priority_queue<pair<int,int> >q;
    int cnt=0;
    long long ans=0ll;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?') {
            cin >> a >> b;
            q.push({b-a,i});
            ans+=b;
            s[i]=')';
            cnt--;
        }else if(s[i]==')'){
            cnt--;
        }else{
            cnt++;
        }
        if(cnt<0){
            if(q.empty()){
                cout<<-1<<endl;
                return 0;
            }else{
                auto x=q.top();
                s[x.second]='(';
                cnt+=2;
                ans-=x.first;
            }
        }
    }
    cout<<ans<<endl;
    cout<<s<<endl;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
    freopen("in", "r", stdin);
    //freopen("out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s;
    int n,m=0;
    cin >> s;
    n = s.size();
    int a,b;
    priority_queue<pair<int,int> >q;
    int cnt=0;
    long long ans=0ll;
    for (int i = 0; i < s.size(); ++i) {
        if (s[i] == '?') {
            cin >> a >> b;
            q.push({b-a,i});
            ans+=b;
            s[i]=')';
            cnt--;
        }else if(s[i]==')'){
            cnt--;
        }else{
            cnt++;
        }
        if(cnt<0){
            if(q.empty()){
                cout<<-1<<endl;
                return 0;
            }else{
                auto x=q.top();
                s[x.second]='(';
                cnt+=2;
                ans+=x.first;
            }
        }
    }
    cout<<ans<<endl;
    cout<<s<<endl;
    return 0;
}


#pragma GCC optimize ("03")
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const int MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
long long dp[50002][25002];
char str[60000];
long long  a[60000];
long long  b[60000];

int main()
{

    scanf("%s",str);
    memset(dp , INF , sizeof(dp));
    int len = strlen(str);
    int m = 0;
    for(int i = 0 ; i < len ;++i)
    {
        if(str[i] == '?')
        {
            ++ m ;
            scanf("%lld%lld",&a[i],&b[i]);
        }
        else if(str[i] == '(')
        {
            a[i] = 0;
            b[i] = INF;
        }
        else
        {
            a[i]= INF;
            b[i] = 0;
        }
    }
    dp[0][1] = a[0] ;
    int now = 1, pre = 0;
    for(int i = 1; i < len ; ++ i)
    {
        for(int j = 0 ; j <= i + 1 && j <= len / 2 ; ++ j)
        {
            if(j == 0) {dp[now][j] = dp[pre][j+1] + b[i];continue ;}
            dp[now][j] = min(dp[pre][j - 1] + a[i], dp[pre][j+1] + b[i]) ;
        }
        for(int j = 0 ;j <= len ; ++ j)printf("%d " , dp[now][j]);cout<<endl;
        for(int j = 0; j <= len ; ++ j)
            dp[pre][j] = dp[now][j];
        now = pre ;
        pre = pre^1;
    }
    if(dp[now][0]>100000)cout<<-1<<endl;
    else cout<<dp[now][0]<<endl;
}


#pragma GCC optimize ("03")
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const int MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
char str[60000];
struct Node
{
    LONG  a, b ;
    LONG  cost ;
    int p ;
    bool operator < (const Node &x) const
    {
        if(cost == x.cost )return p<x.p ;
        else return cost > x.cost ;
    }

}node[60000];
int main()
{

    scanf("%s",str);
    int len = strlen(str);
    int m = 0;
    LONG ans = 0 ;
    for(int i = 0 ; i < len ;++i)
    {
        if(str[i] == '?')
        {
            ++ m ;
            scanf("%lld%lld",&node[i].a,&node[i].b);
            ans += node[i].b ;
        }
        else if(str[i] == '(')
        {
            node[i].a = 0;
            node[i].b = INF;
        }
        else
        {
            node[i].a= INF;
            node[i].b = 0;
        }
        node[i].cost = node[i].a - node[i].b ;
        node[i].p = i;
    }
    priority_queue <Node > que ;
    while(!que.empty()) que.pop();
    int sum = 0 ;
    int judge = 1 ;
    for(int i = 0 ; i< len ;++ i)
    {
        if(str[i] == '?' )
        {
            que.push(node[i]) ;
            str[i] = ')';
            sum ++ ;
        }
        else if(str[i] == '(')
            sum --;
        else if(str[i] == ')')
            sum ++ ;
        if( sum > 0)
        {
            if(que.empty())
            {
                judge = 0 ;
                break ;
            }
            Node tmp = que.top() ;
            que.pop() ;
            str[tmp.p] = '(';
            ans += tmp.cost ;
            sum -= 2 ;
        }

//        printf("%s\n",str);
    }
    if(!judge || sum != 0 )
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    printf("%s\n",str);
}


#pragma GCC optimize ("03")
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<time.h>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<iostream>
using namespace std;
#define  LONG long long
const int   INF=0x3f3f3f3f;
const int MOD=1e9+7;
const double PI=acos(-1.0);
#define clrI(x) memset(x,-1,sizeof(x))
#define clr0(x) memset(x,0,sizeof x)
#define clr1(x) memset(x,INF,sizeof x)
#define clr2(x) memset(x,-INF,sizeof x)
#define EPS 1e-10
char str[60000];
struct Node
{
    LONG  a, b ;
    LONG  cost ;
    int p ;
    bool operator < (const Node &x) const
    {
        if(cost == x.cost )return p<x.p ;
        else return cost > x.cost ;
    }

}node[60000];
int main()
{

    scanf("%s",str);
    int len = strlen(str);
    int m = 0;
    LONG ans = 0 ;
    for(int i = 0 ; i < len ;++i)
    {
        if(str[i] == '?')
        {
            ++ m ;
            scanf("%lld%lld",&node[i].a,&node[i].b);
            ans += node[i].b ;
        }
        else if(str[i] == '(')
        {
            node[i].a = 0;
            node[i].b = INF;
        }
        else
        {
            node[i].a= INF;
            node[i].b = 0;
        }
        node[i].cost = node[i].a - node[i].b ;
        node[i].p = i;
    }
    priority_queue <Node > que ;
    int sum = 0 ;
    int judge = 1 ;
    for(int i = 0 ; i< len ;++ i)
    {
        if(str[i] == '?' )
        {
            que.push(node[i]) ;
            str[i] = ')';
            sum ++ ;
        }
        else if(str[i] == '(')
            sum --;
        else if(str[i] == ')')
            sum ++ ;
        if( sum > 0)
        {
            if(que.empty())
            {
                judge = 0 ;
                break ;
            }
            Node tmp = que.top() ;
            que.pop() ;
            str[tmp.p] = '(';
            ans += tmp.cost ;
            sum -= 2 ;
        }

//        printf("%s\n",str);
    }
    if(!judge )
    {
        cout<<-1<<endl;
        return 0;
    }
    cout<<ans<<endl;
    printf("%s\n",str);
}


#include<bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f;
long long dp[2][60000];
char str[60000];
long long  a[60000];
long long  b[60000];
int main()
{
    scanf("%s",str);
    memset(dp , INF , sizeof(dp));
    int len = strlen(str);
    int m = 0;
    for(int i = 0 ; i < len ;++i)
    {
        if(str[i] == '?')
        {
            ++ m ;
            scanf("%lld%lld",&a[i],&b[i]);
        }
        else if(str[i] == '(')
        {
            a[i] = 0;
            b[i] = INF;
        }
        else
        {
            a[i]= INF;
            b[i] = 0;
        }
    }
    dp[0][1] = a[0] ;
    int now = 1, pre = 0;
    for(int i = 1; i < len ; ++ i)
    {
        for(int j = 0 ; j<= len ; ++ j)
        {
            if(j == 0) {dp[now][j] = dp[pre][j+1] + b[i];continue ;}
            dp[now][j] = min(dp[pre][j - 1] + a[i], dp[pre][j+1] + b[i]) ;
        }
//for(int j = 0 ;j <= len ;++ j)printf("%d " , dp[now][j]);cout<<endl;
        for(int j =0; j<= len ; ++ j)
            dp[pre][j] = dp[now][j];
        now = pre ;
        pre = pre^1;
    }
    if(dp[now][0]>100000)cout<<-1<<endl;
    else cout<<dp[now][0]<<endl;
}


#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>

using namespace std;

class comp{
public:
    bool operator()(pair<int,int> &a, pair<int,int> &b){
        return a.first > b.first;
    }
};

int main(){
    string s;
    cin>>s;
    int a,b;
    long long int ans = 0;
    int cnt = 0;
    bool temp = false;
    priority_queue<pair<int,int> , vector<pair<int,int> >, comp> myQueue;
    for(size_t i = 0; i < s.size(); i++){
        if(s[i] == '(')cnt++;
        else if(s[i] == ')')cnt--;
        else{
            s[i] = ')';
            cnt--;
            cin>>a>>b;
            ans += b;
            myQueue.push(make_pair(a-b, i));
        }
        if(cnt < 0){
            if(myQueue.empty()){
                temp = true;
            }else{
                pair<int,int> y = myQueue.top();
                myQueue.pop();
                ans += y.first;
                cnt += 2;
                s[y.second] = '(';
            }
        }
    }
    if(temp || cnt != 0){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
        cout<<s<<endl;
    }
}


#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>

using namespace std;

class comp{
public:
    bool operator()(pair<int,int> &a, pair<int,int> &b){
        return a.first > b.first;
    }
};

int main(){
    string s;
    cin>>s;
    int a,b;
    long long int ans = 0;
    int cnt = 0;
    bool temp = false;
    priority_queue<pair<int,int> , vector<pair<int,int> >, comp> myQueue;
    for(size_t i = 0; i < s.size(); i++){
        if(s[i] == '(')cnt++;
        else if(s[i] == ')')cnt--;
        else{
            s[i] = ')';
            cnt--;
            cin>>a>>b;
            ans += b;
            myQueue.push(make_pair(a-b, i));
        }
        if(cnt < 0){
            if(myQueue.empty()){
                temp = true;
            }else{
                pair<int,int> y = myQueue.top();
                myQueue.pop();
                ans += y.first;
                cnt += 2;
                s[y.second] = '(';
            }
        }
    }
    if(temp){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <climits>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>

using namespace std;

class comp{
public:
    bool operator()(pair<int,int> &a, pair<int,int> &b){
        return a.first > b.first;
    }
};

int main(){
    string s;
    cin>>s;
    int a,b;
    long long int ans = 0;
    int cnt = 0;
    bool temp = false;
    priority_queue<pair<int,int> , vector<pair<int,int> >, comp> myQueue;
    for(size_t i = 0; i < s.size(); i++){
        if(s[i] == '(')cnt++;
        else if(s[i] == ')')cnt--;
        else{
            s[i] = ')';
            cnt--;
            cin>>a>>b;
            ans += b;
            myQueue.push(make_pair(a-b, i));
        }
        if(cnt < 0){
            if(myQueue.empty()){
                temp = true;
            }else{
                pair<int,int> y = myQueue.top();
                myQueue.pop();
                ans += y.first;
                cnt += 2;
                s[y.second] = '(';
            }
        }
    }
    if(temp){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
        cout<<s<<endl;
    }
}


///nice one sweepline greedy

#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

const int NMAX = 5e4;

string s;

int nr;
pair <int, int> cost[NMAX + 2];

vector <char> ans;
priority_queue < pair <int, int> > pq;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> s;

    ans.resize(s.size());

    for(auto it : s)
        if(it == '?')
            nr++;

    for(int i = 1; i <= nr; i++)
        cin >> cost[i].first >> cost[i].second;

    /*
    cout << "=====\n";
    cout << nr << '\n';
    for(int i = 1; i <= nr; i++)
        cout << cost[i].first << ' ' << cost[i].second << '\n';
    */

    int depth = 0, ind = 0, q = 0;
    long long sum = 0;

    for(auto it : s)
    {
        if(it == '(')
        {
            depth++;
            ans[ind] = '(';
        }
        else if(it == ')')
        {
            depth--;
            ans[ind] = ')';
        }
        else
        {
            q++;

            ans[ind] = ')';
            depth--;
            sum += cost[q].second;

            pq.push({cost[q].second - cost[q].first, ind});
        }

        if(depth < 0)
            {
                if(pq.empty())
                    break;

                pair <int, int> p = pq.top();
                pq.pop();

                depth += 2;
                sum -= 1LL * p.first;

                ans[p.second] = '(';
            }

        ind++;
    }

    if(depth != 0)
    {
        cout << -1;
        return 0;
    }

    cout << sum << '\n';
    for(auto it : ans)
        cout << it;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <queue>

using namespace std;

const int NMAX = 5e4;

string s;

int nr;
pair <int, int> cost[NMAX + 2];

vector <char> ans;
priority_queue < pair <int, int> > pq;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> s;

    ans.resize(s.size());

    for(auto it : s)
        if(it == '?')
            nr++;

    for(int i = 1; i <= nr; i++)
        cin >> cost[i].first >> cost[i].second;

    /*
    cout << "=====\n";
    cout << nr << '\n';
    for(int i = 1; i <= nr; i++)
        cout << cost[i].first << ' ' << cost[i].second << '\n';
    */

    int depth = 0, ind = 0, q = 0;
    long long sum = 0;

    for(auto it : s)
    {
        if(it == '(')
        {
            depth++;
            ans[ind] = '(';
        }
        else if(it == ')')
        {
            depth--;
            ans[ind] = ')';
        }
        else
        {
            q++;

            ans[ind] = ')';
            depth--;
            sum += cost[q].second;

            pq.push({cost[q].second - cost[q].first, ind});
        }

        if(depth < 0)
            {
                if(pq.empty())
                    break;

                pair <int, int> p = pq.top();
                pq.pop();

                depth += 2;
                sum -= 1LL * p.first;

                ans[p.second] = '(';
            }

        ind++;
    }

    if(depth != 0)
    {
        cout << -1;
        return 0;
    }

    cout << sum << '\n';
    for(auto it : ans)
        cout << it;

    return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<double,pair<int,int>> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define fi first
#define se second

int dp[5010][2510];
string s;
int n;
int a[5010];
int b[5010];
vector<char> ans;

void re(int x, int y) {
  if (x == 0) return;
  if (s[x-1] == ')') {
    re(x-1,y+1);
    ans.pb(')');
  } else if (s[x-1] == '(') {
    re(x-1,y-1);
    ans.pb('(');
  } else {
    if (dp[x][y] == dp[x-1][y-1] + a[x-1]) {
      re(x-1,y-1);
      ans.pb('(');
    } else {
      re(x-1,y+1);
      ans.pb(')');
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;
  n = s.size();
  REP(i,n+2) REP(j,2510) dp[i][j] = inf;
  REP(i,n) if (s[i] == '?') cin >> a[i] >> b[i];
  dp[0][0] = 0;
  REP(i,n) REP(j,n) {
    if (s[i] == ')') {
      if (j <= n/2) dp[i+1][j] = min(dp[i+1][j], dp[i][j+1]);
    } else if (s[i] == '(') {
      if (j != 0 && j <= n/2) dp[i+1][j] = min(dp[i+1][j], dp[i][j-1]);
    } else {
      if (j != 0 && j <= n/2)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j-1] + a[i]);
      if (j <= n/2)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j+1] + b[i]);
    }
  }
  cout << dp[n][0] << endl;
  re(n, 0);
  REP(i,n) cout << ans[i];
  cout << endl;
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll a[50010], b[50010];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P,vector<P>,greater<P>> pq;
  REP(i,sz(s)) {
    if (s[i] == '(') {
      a[i] = 0;
      b[i] = INF;
    } else if (s[i] == ')') {
      a[i] = 0;
      b[i] = INF;
    }
    if (s[i] == '?') {
      s[i] = '(';
      cin >> a[i] >> b[i];
      sum += a[i];
    }
  }
  int k = 0;
  for (int i = sz(s) - 1; i >= 0; i--) {
    if (s[i] == ')') k++;
    else {
      k--;
      pq.push({b[i] - a[i], i});
      if (k < 0) {
        P tp = pq.top(); pq.pop();
        if (tp.fi == INF) {
          cout << -1 << endl;
        }
        sum += tp.fi;
        s[tp.se] = ')';
        k+=2;
      }
    }
  }
  if (k > 0) cout << -1 << endl;
  else {
  cout << sum << endl;
  cout << s << endl;
  }
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll a[50010], b[50010];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P,vector<P>,greater<P>> pq;
  REP(i,sz(s)) {
    if (s[i] == '(') {
      a[i] = 0;
      b[i] = inf;
    } else if (s[i] == ')') {
      a[i] = 0;
      b[i] = inf;
    }
    if (s[i] == '?') {
      s[i] = '(';
      cin >> a[i] >> b[i];
      sum += a[i];
    }
  }
  int k = 0;
  for (int i = sz(s) - 1; i >= 0; i--) {
    if (s[i] == ')') k++;
    else {
      k--;
      pq.push({b[i] - a[i], i});
      if (k < 0) {
        P tp = pq.top(); pq.pop();
        sum += tp.fi;
        s[tp.se] = ')';
        k+=2;
      }
    }
  }
  cout << sum << endl;
  cout << s << endl;
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<double,pair<int,int>> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define fi first
#define se second

ll dp[5010][5010];
string s;
int n;
ll a[5010];
ll b[5010];
vector<char> ans;

void re(int x, int y) {
  if (x == 0) return;
  if (s[x-1] == ')') {
    re(x-1,y+1);
    ans.pb(')');
  } else if (s[x-1] == '(') {
    re(x-1,y-1);
    ans.pb('(');
  } else {
    if (dp[x][y] == dp[x-1][y-1] + a[x-1]) {
      re(x-1,y-1);
      ans.pb('(');
    } else {
      re(x-1,y+1);
      ans.pb(')');
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;
  n = s.size();
  REP(i,n+2) REP(j,n+2) dp[i][j] = INF;
  REP(i,n) if (s[i] == '?') cin >> a[i] >> b[i];
  dp[0][0] = 0;
  REP(i,n) REP(j,n) {
    if (s[i] == ')') {
      if (j != n) dp[i+1][j] = min(dp[i+1][j], dp[i][j+1]);
    } else if (s[i] == '(') {
      if (j != 0) dp[i+1][j] = min(dp[i+1][j], dp[i][j-1]);
    } else {
      if (j != 0)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j-1] + a[i]);
      if (j != n)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j+1] + b[i]);
    }
  }
  cout << dp[n][0] << endl;
  re(n, 0);
  REP(i,n) cout << ans[i];
  cout << endl;
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll a[50010], b[50010];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P,vector<P>,greater<P>> pq;
  REP(i,sz(s)) {
    if (s[i] == '(') {
      a[i] = 0;
      b[i] = INF;
    } else if (s[i] == ')') {
      a[i] = 0;
      b[i] = INF;
    }
    if (s[i] == '?') {
      s[i] = '(';
      cin >> a[i] >> b[i];
      sum += a[i];
    }
  }
  int k = 0;
  for (int i = sz(s) - 1; i >= 0; i--) {
    if (s[i] == ')') k++;
    else {
      k--;
      pq.push({b[i] - a[i], i});
      if (k < 0) {
        P tp = pq.top(); pq.pop();
        if (tp.fi == INF) {
          cout << -1 << endl;
          return 0;
        }
        sum += tp.fi;
        s[tp.se] = ')';
        k+=2;
      }
    }
  }
  if (k > 0) cout << -1 << endl;
  else {
  cout << sum << endl;
  cout << s << endl;
  }
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P> pq;
  REP(i,sz(s)) {
    if (s[i] == '?') {
      s[i] = '(';
      ll a, b;
      cin >> a >> b;
      sum += a;
      pq.push({a-b,i});
    }
  }
  while (!pq.empty()) {
    P tp = pq.top(); pq.pop();
    s[tp.se] = ')';
    bool f = 1;
    int st = 0;
    REP(i,sz(s)) {
      if (s[i] == '(') st++;
      else {
        st--;
        if (st < 0) {f = 0; break;}
      }
    }
    if (f) {
      sum -= tp.fi;
      if (st == 0) {
        cout << sum << endl;
        cout << s << endl;
        return 0;
      }
    } else {
      s[tp.se] = '(';
    }
  }
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 4e18;
const ll MOD = 1e9+7;
typedef pair<double,pair<int,int>> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define pb push_back
#define fi first
#define se second

int dp[50100][25100];
string s;
int n;
int a[5010];
int b[5010];
vector<char> ans;

void re(int x, int y) {
  if (x == 0) return;
  if (s[x-1] == ')') {
    re(x-1,y+1);
    ans.pb(')');
  } else if (s[x-1] == '(') {
    re(x-1,y-1);
    ans.pb('(');
  } else {
    if (dp[x][y] == dp[x-1][y-1] + a[x-1]) {
      re(x-1,y-1);
      ans.pb('(');
    } else {
      re(x-1,y+1);
      ans.pb(')');
    }
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s;
  n = s.size();
  REP(i,n+2) REP(j,25100) dp[i][j] = inf;
  REP(i,n) if (s[i] == '?') cin >> a[i] >> b[i];
  dp[0][0] = 0;
  REP(i,n) REP(j,n) {
    if (s[i] == ')') {
      if (j <= n/2) dp[i+1][j] = min(dp[i+1][j], dp[i][j+1]);
    } else if (s[i] == '(') {
      if (j != 0 && j <= n/2) dp[i+1][j] = min(dp[i+1][j], dp[i][j-1]);
    } else {
      if (j != 0 && j <= n/2)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j-1] + a[i]);
      if (j <= n/2)
        dp[i+1][j] = min(dp[i+1][j], dp[i][j+1] + b[i]);
    }
  }
  cout << dp[n][0] << endl;
  re(n, 0);
  REP(i,n) cout << ans[i];
  cout << endl;
  return 0;
}


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

const double PI = acos(-1);
using ll = long long;
using ull = unsigned long long;
const int inf = 2e9;
const ll INF = 2e18;
const ll MOD = 1e9+7;
typedef pair<ll,int> P;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) FOR(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second

ll a[50010], b[50010];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  string s;
  cin >> s;
  ll sum = 0;
  priority_queue<P,vector<P>,greater<P>> pq;
  REP(i,sz(s)) {
    if (s[i] == '(') {
      a[i] = 0;
      b[i] = INF;
    } else if (s[i] == ')') {
      a[i] = 0;
      b[i] = INF;
    }
    if (s[i] == '?') {
      s[i] = '(';
      cin >> a[i] >> b[i];
      sum += a[i];
    }
  }
  int k = 0;
  for (int i = sz(s) - 1; i >= 0; i--) {
    if (s[i] == ')') k++;
    else {
      k--;
      pq.push({b[i] - a[i], i});
      if (k < 0) {
        P tp = pq.top(); pq.pop();
        sum += tp.fi;
        s[tp.se] = ')';
        k+=2;
      }
    }
  }
  if (sum >= INF || k > 0) cout << -1 << endl;
  else {
  cout << sum << endl;
  cout << s << endl;
  }
  return 0;
}