結果

問題 No.2595 Parsing Challenge
ユーザー Benjamin QiBenjamin Qi
提出日時 2024-12-29 10:24:24
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,186 ms / 6,000 ms
コード長 20,785 bytes
コンパイル時間 4,841 ms
コンパイル使用メモリ 203,876 KB
実行使用メモリ 315,556 KB
最終ジャッジ日時 2024-12-29 10:24:59
合計ジャッジ時間 30,503 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 2 ms
5,248 KB
testcase_04 AC 2 ms
5,248 KB
testcase_05 AC 2 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 AC 2 ms
5,248 KB
testcase_11 AC 2 ms
5,248 KB
testcase_12 AC 2 ms
5,248 KB
testcase_13 AC 2 ms
5,248 KB
testcase_14 AC 3 ms
5,248 KB
testcase_15 AC 2 ms
5,248 KB
testcase_16 AC 3 ms
5,248 KB
testcase_17 AC 3 ms
5,248 KB
testcase_18 AC 3 ms
5,248 KB
testcase_19 AC 3 ms
5,248 KB
testcase_20 AC 11 ms
6,820 KB
testcase_21 AC 11 ms
6,784 KB
testcase_22 AC 10 ms
6,656 KB
testcase_23 AC 8 ms
5,632 KB
testcase_24 AC 11 ms
6,784 KB
testcase_25 AC 73 ms
30,136 KB
testcase_26 AC 91 ms
37,656 KB
testcase_27 AC 90 ms
35,308 KB
testcase_28 AC 87 ms
35,772 KB
testcase_29 AC 89 ms
35,640 KB
testcase_30 AC 876 ms
289,908 KB
testcase_31 AC 910 ms
309,068 KB
testcase_32 AC 898 ms
315,556 KB
testcase_33 AC 757 ms
274,368 KB
testcase_34 AC 823 ms
297,788 KB
testcase_35 AC 1,171 ms
224,980 KB
testcase_36 AC 1,174 ms
225,280 KB
testcase_37 AC 1,173 ms
225,000 KB
testcase_38 AC 1,166 ms
225,568 KB
testcase_39 AC 1,186 ms
225,300 KB
testcase_40 AC 26 ms
10,760 KB
testcase_41 AC 26 ms
10,888 KB
testcase_42 AC 25 ms
10,892 KB
testcase_43 AC 185 ms
113,840 KB
testcase_44 AC 437 ms
174,416 KB
testcase_45 AC 428 ms
174,412 KB
testcase_46 AC 431 ms
174,280 KB
testcase_47 AC 463 ms
174,380 KB
testcase_48 AC 443 ms
174,284 KB
testcase_49 AC 748 ms
183,756 KB
testcase_50 AC 783 ms
183,628 KB
testcase_51 AC 751 ms
183,484 KB
testcase_52 AC 837 ms
272,800 KB
testcase_53 AC 868 ms
272,748 KB
testcase_54 AC 834 ms
272,760 KB
testcase_55 AC 869 ms
272,600 KB
testcase_56 AC 833 ms
272,712 KB
testcase_57 AC 966 ms
315,220 KB
testcase_58 AC 927 ms
315,088 KB
testcase_59 AC 959 ms
315,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

using ll = long long;
using db = long double;  // or double, if TL is tight
using str = string;      // yay python!

// pairs
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
#define mp make_pair
#define f first
#define s second

#define tcT template <class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT > using V = vector<T>;
tcT, size_t SZ > using AR = array<T, SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pi>;
using vpl = V<pl>;
using vpd = V<pd>;

// vectors
#define sz(x) int(size(x))
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()

#define lb lower_bound
#define ub upper_bound
tcT > int lwb(const V<T> &a, const T &b) { return int(lb(all(a), b) - bg(a)); }
tcT > int upb(const V<T> &a, const T &b) { return int(ub(all(a), b) - bg(a)); }

// loops
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define rep(a) F0R(_, a)
#define each(a, x) for (auto &a : x)

const int MOD = 998244353;  // 1e9+7;
const int MX = (int)2e5 + 5;
const ll BIG = 1e18;  // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4]{1, 0, -1, 0}, dy[4]{0, 1, 0, -1};  // for every grid problem!!
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

// bitwise ops
// also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html
constexpr int pct(int x) { return __builtin_popcount(x); }  // # of bits set
constexpr int bits(int x) {  // assert(x >= 0); // make C++11 compatible until
	                         // USACO updates ...
	return x == 0 ? 0 : 31 - __builtin_clz(x);
}  // floor(log2(x))
constexpr int p2(int x) { return 1 << x; }
constexpr int msk2(int x) { return p2(x) - 1; }

ll cdiv(ll a, ll b) {
	return a / b + ((a ^ b) > 0 && a % b);
}  // divide a by b rounded up
ll fdiv(ll a, ll b) {
	return a / b - ((a ^ b) < 0 && a % b);
}  // divide a by b rounded down

tcT > bool ckmin(T &a, const T &b) {
	return b < a ? a = b, 1 : 0;
}  // set a = min(a,b)
tcT > bool ckmax(T &a, const T &b) {
	return a < b ? a = b, 1 : 0;
}  // set a = max(a,b)

tcTU > T fstTrue(T lo, T hi, U f) {
	++hi;
	assert(lo <= hi);  // assuming f is increasing
	while (lo < hi) {  // find first index such that f is true
		T mid = lo + (hi - lo) / 2;
		f(mid) ? hi = mid : lo = mid + 1;
	}
	return lo;
}
tcTU > T lstTrue(T lo, T hi, U f) {
	--lo;
	assert(lo <= hi);  // assuming f is decreasing
	while (lo < hi) {  // find first index such that f is true
		T mid = lo + (hi - lo + 1) / 2;
		f(mid) ? lo = mid : hi = mid - 1;
	}
	return lo;
}
tcT > void remDup(vector<T> &v) {  // sort and remove duplicates
	sort(all(v));
	v.erase(unique(all(v)), end(v));
}
tcTU > void safeErase(T &t, const U &u) {
	auto it = t.find(u);
	assert(it != end(t));
	t.erase(it);
}

inline namespace IO {
#define SFINAE(x, ...)                                                         \
	template <class, class = void> struct x : std::false_type {};              \
	template <class T> struct x<T, std::void_t<__VA_ARGS__>> : std::true_type {}

SFINAE(DefaultI, decltype(std::cin >> std::declval<T &>()));
SFINAE(DefaultO, decltype(std::cout << std::declval<T &>()));
SFINAE(IsTuple, typename std::tuple_size<T>::type);
SFINAE(Iterable, decltype(std::begin(std::declval<T>())));

template <auto &is> struct Reader {
	template <class T> void Impl(T &t) {
		if constexpr (DefaultI<T>::value) is >> t;
		else if constexpr (Iterable<T>::value) {
			for (auto &x : t) Impl(x);
		} else if constexpr (IsTuple<T>::value) {
			std::apply([this](auto &...args) { (Impl(args), ...); }, t);
		} else static_assert(IsTuple<T>::value, "No matching type for read");
	}
	template <class... Ts> void read(Ts &...ts) { ((Impl(ts)), ...); }
};

template <class... Ts> void re(Ts &...ts) { Reader<cin>{}.read(ts...); }
#define def(t, args...)                                                        \
	t args;                                                                    \
	re(args);

template <auto &os, bool debug, bool print_nd> struct Writer {
	string comma() const { return debug ? "," : ""; }
	template <class T> constexpr char Space(const T &) const {
		return print_nd && (Iterable<T>::value or IsTuple<T>::value) ? '\n'
		                                                             : ' ';
	}
	template <class T> void Impl(T const &t) const {
		if constexpr (DefaultO<T>::value) os << t;
		else if constexpr (Iterable<T>::value) {
			if (debug) os << '{';
			int i = 0;
			for (auto &&x : t)
				((i++) ? (os << comma() << Space(x), Impl(x)) : Impl(x));
			if (debug) os << '}';
		} else if constexpr (IsTuple<T>::value) {
			if (debug) os << '(';
			std::apply(
			    [this](auto const &...args) {
				    int i = 0;
				    (((i++) ? (os << comma() << " ", Impl(args)) : Impl(args)),
				     ...);
			    },
			    t);
			if (debug) os << ')';
		} else static_assert(IsTuple<T>::value, "No matching type for print");
	}
	template <class T> void ImplWrapper(T const &t) const {
		if (debug) os << "\033[0;31m";
		Impl(t);
		if (debug) os << "\033[0m";
	}
	template <class... Ts> void print(Ts const &...ts) const {
		((Impl(ts)), ...);
	}
	template <class F, class... Ts>
	void print_with_sep(const std::string &sep, F const &f,
	                    Ts const &...ts) const {
		ImplWrapper(f), ((os << sep, ImplWrapper(ts)), ...), os << '\n';
	}
	void print_with_sep(const std::string &) const { os << '\n'; }
};

template <class... Ts> void pr(Ts const &...ts) {
	Writer<cout, false, true>{}.print(ts...);
}
template <class... Ts> void ps(Ts const &...ts) {
	Writer<cout, false, true>{}.print_with_sep(" ", ts...);
}
}  // namespace IO

inline namespace Debug {
template <typename... Args> void err(Args... args) {
	Writer<cerr, true, false>{}.print_with_sep(" | ", args...);
}
template <typename... Args> void errn(Args... args) {
	Writer<cerr, true, true>{}.print_with_sep(" | ", args...);
}

void err_prefix(str func, int line, string args) {
	cerr << "\033[0;31m\u001b[1mDEBUG\033[0m"
	     << " | "
	     << "\u001b[34m" << func << "\033[0m"
	     << ":"
	     << "\u001b[34m" << line << "\033[0m"
	     << " - "
	     << "[" << args << "] = ";
}

#ifdef LOCAL
#define dbg(args...) err_prefix(__FUNCTION__, __LINE__, #args), err(args)
#define dbgn(args...) err_prefix(__FUNCTION__, __LINE__, #args), errn(args)
#else
#define dbg(...)
#define dbgn(args...)
#endif

const auto beg_time = std::chrono::high_resolution_clock::now();
// https://stackoverflow.com/questions/47980498/accurate-c-c-clock-on-a-multi-core-processor-with-auto-overclock?noredirect=1&lq=1
double time_elapsed() {
	return chrono::duration<double>(std::chrono::high_resolution_clock::now() -
	                                beg_time)
	    .count();
}
}  // namespace Debug

inline namespace FileIO {
void setIn(str s) { freopen(s.c_str(), "r", stdin); }
void setOut(str s) { freopen(s.c_str(), "w", stdout); }
void setIO(str s = "") {
	cin.tie(0)->sync_with_stdio(0);  // unsync C / C++ I/O streams
	cout << fixed << setprecision(12);
	// cin.exceptions(cin.failbit);
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) setIn(s + ".in"), setOut(s + ".out");  // for old USACO
}
}  // namespace FileIO

#include <atcoder/convolution>
#include <string>
#include <vector>
const int DIGIT = 6;
const int BASE = 1000000;
struct positive_bigint {
	std::vector<int> d;
	positive_bigint() {}
	positive_bigint(long long X) {
		while (X > 0) {
			d.push_back(X % BASE);
			X /= BASE;
		}
	}
	positive_bigint(std::string S) {
		if (S == "0") { S = ""; }
		int L = S.size();
		d.resize((L + DIGIT - 1) / DIGIT, 0);
		for (int i = L - 1; i >= 0; i -= 6) {
			for (int j = std::max(i - 5, 0); j <= i; j++) {
				d[i / DIGIT] *= 10;
				d[i / DIGIT] += S[j] - '0';
			}
		}
		std::reverse(d.begin(), d.end());
	}
	bool empty() const { return d.empty(); }
	int size() const { return d.size(); }
	int &operator[](int i) { return d[i]; }
	int operator[](int i) const { return d[i]; }
};
std::string to_string(const positive_bigint &A) {
	int N = A.size();
	std::string ans;
	for (int i = N - 1; i >= 0; i--) {
		std::string tmp = std::to_string(A[i]);
		if (i < N - 1) { ans += std::string(DIGIT - tmp.size(), '0'); }
		ans += tmp;
	}
	if (ans.empty()) { ans = "0"; }
	return ans;
}
std::istream &operator>>(std::istream &is, positive_bigint &A) {
	std::string S;
	is >> S;
	A = positive_bigint(S);
	return is;
}
std::ostream &operator<<(std::ostream &os, positive_bigint &A) {
	os << to_string(A);
	return os;
}
int cmp(const positive_bigint &A, const positive_bigint &B) {
	int N = A.size();
	int M = B.size();
	if (N < M) {
		return -1;
	} else if (N > M) {
		return 1;
	} else {
		for (int i = N - 1; i >= 0; i--) {
			if (A[i] < B[i]) { return -1; }
			if (A[i] > B[i]) { return 1; }
		}
		return 0;
	}
}
bool operator==(const positive_bigint &A, const positive_bigint &B) {
	return cmp(A, B) == 0;
}
bool operator!=(const positive_bigint &A, const positive_bigint &B) {
	return cmp(A, B) != 0;
}
bool operator<(const positive_bigint &A, const positive_bigint &B) {
	return cmp(A, B) < 0;
}
bool operator>(const positive_bigint &A, const positive_bigint &B) {
	return cmp(A, B) > 0;
}
bool operator<=(const positive_bigint &A, const positive_bigint &B) {
	return cmp(A, B) <= 0;
}
bool operator>=(const positive_bigint &A, const positive_bigint &B) {
	return cmp(A, B) >= 0;
}
positive_bigint &operator+=(positive_bigint &A, const positive_bigint &B) {
	int N = A.size();
	int M = B.size();
	while (N < M) {
		A.d.push_back(0);
		N++;
	}
	for (int i = 0; i < M; i++) { A[i] += B[i]; }
	for (int i = 0; i < N - 1; i++) {
		if (A[i] >= BASE) {
			A[i] -= BASE;
			A[i + 1]++;
		}
	}
	if (N > 0) {
		if (A[N - 1] >= BASE) {
			A.d.push_back(1);
			A[N - 1] -= BASE;
		}
	}
	return A;
}
positive_bigint operator+(const positive_bigint &A, const positive_bigint &B) {
	positive_bigint A2 = A;
	A2 += B;
	return A2;
}
positive_bigint &operator-=(positive_bigint &A, const positive_bigint &B) {
	int N = A.size();
	int M = B.size();
	for (int i = 0; i < M; i++) { A[i] -= B[i]; }
	for (int i = 0; i < N - 1; i++) {
		if (A[i] < 0) {
			A[i] += BASE;
			A[i + 1]--;
		}
	}
	while (!A.empty()) {
		if (A.d.back() == 0) {
			A.d.pop_back();
		} else {
			break;
		}
	}
	return A;
}
positive_bigint operator-(const positive_bigint &A, const positive_bigint &B) {
	positive_bigint A2 = A;
	A2 -= B;
	return A2;
}
positive_bigint operator*(const positive_bigint &A, const positive_bigint &B) {
	if (A.empty() || B.empty()) { return 0; }
	int N = A.size();
	int M = B.size();
	std::vector<long long> a(N);
	for (int i = 0; i < N; i++) { a[i] = A[i]; }
	std::vector<long long> b(M);
	for (int i = 0; i < M; i++) { b[i] = B[i]; }
	std::vector<long long> C = atcoder::convolution_ll(a, b);
	for (int i = 0; i < N + M - 2; i++) {
		C[i + 1] += C[i] / BASE;
		C[i] %= BASE;
	}
	if (C[N + M - 2] >= BASE) {
		C.resize(N + M);
		C[N + M - 1] += C[N + M - 2] / BASE;
		C[N + M - 2] %= BASE;
	}
	positive_bigint ans;
	ans.d.resize(C.size());
	for (int i = 0; i < C.size(); i++) { ans[i] = C[i]; }
	return ans;
}
positive_bigint operator*=(positive_bigint &A, const positive_bigint &B) {
	A = A * B;
	return A;
}
struct bigint {
	bool neg = false;
	positive_bigint a;
	bigint() {}
	bigint(long long X) : neg(X < 0), a(abs(X)) {}
	bigint(const positive_bigint &X, bool neg = false) : neg(neg), a(X) {}
	bigint(const std::string &s) {
		if (!s.empty()) {
			if (s[0] == '-') {
				neg = true;
				a = positive_bigint(s.substr(1, s.size() - 1));
			} else {
				a = positive_bigint(s);
			}
		}
	}
	bool empty() const { return a.empty(); }
	int size() const { return a.size(); }
	int &operator[](int i) { return a[i]; }
};
std::string to_string(const bigint &A) {
	std::string ans;
	if (A.neg) { ans += '-'; }
	ans += to_string(A.a);
	return ans;
}
std::istream &operator>>(std::istream &is, bigint &A) {
	std::string S;
	is >> S;
	if (S != "0") { A = bigint(S); }
	return is;
}
std::ostream &operator<<(std::ostream &os, bigint A) {
	os << to_string(A);
	return os;
}
positive_bigint abs(const bigint &A) { return A.a; }
int cmp(const bigint &A, const bigint &B) {
	if (!A.neg) {
		if (!B.neg) {
			return cmp(A.a, B.a);
		} else {
			return 1;
		}
	} else {
		if (!B.neg) {
			return -1;
		} else {
			return cmp(B.a, A.a);
		}
	}
}
bool operator==(const bigint &A, const bigint &B) { return cmp(A, B) == 0; }
bool operator!=(const bigint &A, const bigint &B) { return cmp(A, B) != 0; }
bool operator<(const bigint &A, const bigint &B) { return cmp(A, B) < 0; }
bool operator>(const bigint &A, const bigint &B) { return cmp(A, B) > 0; }
bool operator<=(const bigint &A, const bigint &B) { return cmp(A, B) <= 0; }
bool operator>=(const bigint &A, const bigint &B) { return cmp(A, B) >= 0; }
bigint operator+(const bigint &A) { return A; }
bigint operator-(const bigint &A) {
	bigint A2 = A;
	if (!A2.empty()) { A2.neg = !A2.neg; }
	return A2;
}
bigint &operator+=(bigint &A, const bigint &B) {
	if (A.neg == B.neg) {
		A.a += B.a;
	} else {
		int c = cmp(A.a, B.a);
		if (c > 0) {
			A.a -= B.a;
		} else if (c < 0) {
			A.a = B.a - A.a;
			A.neg = !A.neg;
		} else {
			A = 0;
		}
	}
	return A;
}
bigint operator+(const bigint &A, const bigint &B) {
	bigint A2 = A;
	A2 += B;
	return A2;
}
bigint &operator-=(bigint &A, const bigint &B) {
	if (A.neg != B.neg) {
		A.a += B.a;
	} else {
		int c = cmp(A.a, B.a);
		if (c > 0) {
			A.a -= B.a;
		} else if (c < 0) {
			A.a = B.a - A.a;
			A.neg = !A.neg;
		} else {
			A = 0;
		}
	}
	return A;
}
bigint operator-(const bigint &A, const bigint &B) {
	bigint A2 = A;
	A2 -= B;
	return A2;
}
bigint operator*=(bigint &A, const bigint &B) {
	if (A.empty() || B.empty()) {
		A = 0;
	} else {
		if (B.neg) { A.neg = !A.neg; }
		A.a *= B.a;
	}
	return A;
}
bigint operator*(const bigint &A, const bigint &B) {
	bigint A2 = A;
	A2 *= B;
	return A2;
}

struct Node;

int GLOB_ID;
str s;
V<vi> g;
vpi children;
vi weight;
V<Node *> nodes_by_id;

struct Node {
	char op = '.';
	Node *l = nullptr, *r = nullptr;
	str val;
	int id;
	// bigint eval() {
	// 	if (op == '.') {
	// 		assert(sz(val));
	// 		return bigint(val);
	// 	}
	// 	assert(l && r);
	// 	bigint x = l->eval();
	// 	bigint y = r->eval();
	// 	if (op == '*') return x * y;
	// 	if (op == '-') return x - y;
	// 	assert(op == '+');
	// 	return x + y;
	// }
	void assign() {
		id = GLOB_ID++;
		nodes_by_id.pb(this);
		weight.pb(op == '.' ? sz(val) : 1);
		g.eb();
		children.pb({-1, -1});
		if (op != '.') {
			assert(l != nullptr);
			l->assign();
			r->assign();
			children.at(id) = {l->id, r->id};
			g.at(id) = {l->id, r->id};
		}
	}
};

Node *make_op(char c, Node *l, Node *r) { return new Node{c, l, r}; }

Node *combine(V<Node *> nodes, str ops) {
	assert(sz(ops) == sz(nodes) - 1);
	V<Node *> node_stk;
	str op_stk;
	F0R(i, sz(nodes)) {
		if (sz(op_stk) && op_stk.bk == '*') {
			assert(sz(node_stk));
			node_stk.bk = make_op(op_stk.bk, node_stk.bk, nodes[i]);
			op_stk.pop_back();
		} else {
			node_stk.pb(nodes[i]);
		}
		if (i < sz(ops)) { op_stk.pb(ops.at(i)); }
	}
	F0R(i, sz(op_stk)) {
		node_stk.ft = make_op(op_stk.at(i), node_stk.ft, node_stk.at(i + 1));
	}
	return node_stk.ft;
	// assert(sz(nodes));
	// dbg(sz(nodes), ops);
	// exit(0);
}

pair<int, Node *> dfs(int pos) {
	V<Node *> nodes;
	str ops;
	int parity = 0;
	while (true) {
		dbg(parity, pos);
		if (parity == 0) {
			if (s.at(pos) == '(') {
				++pos;
				auto [npos, node] = dfs(pos);
				pos = npos;
				assert(sz(nodes) == sz(ops));
				nodes.pb(node);
			} else if (s.at(pos) == '-' || isdigit(s.at(pos))) {
				int sgn = 1;
				while (s.at(pos) == '-') ++pos, sgn *= -1;
				str num;
				while (isdigit(s.at(pos))) num += s.at(pos++);
				if (sgn == -1) num = '-' + num;
				nodes.pb(new Node{'.', nullptr, nullptr, num});
			} else {
				assert(false);
			}
		} else {
			if (s.at(pos) == ')') {
				++pos;
				return {pos, combine(nodes, ops)};
			}
			assert(s.at(pos) == '*' || s.at(pos) == '+' || s.at(pos) == '-');
			ops += s.at(pos++);
		}
		parity ^= 1;
	}
}

enum Type { Vertex, Compress, Rake, AddEdge, AddVertex };

// g must be a rooted tree
struct StaticTopTree {
	vector<vector<int>> &g;
	int root;             // an index of the root in g
	int stt_root;         // an index of the root in static top tree
	vector<int> P, L, R;  // parent, left child, right child
	vector<Type> T;       // type of vertices
	int add_buf;          // a variable for the member function

	StaticTopTree(vector<vector<int>> &_g, int _root = 0) : g(_g), root(_root) {
		int n = g.size();
		P.resize(4 * n, -1), L.resize(4 * n, -1), R.resize(4 * n, -1);
		T.resize(4 * n, Type::Vertex);
		add_buf = n;
		build();
	}

  private:
	int dfs(int c) {
		int s = weight.at(c), best = 0;
		for (int &d : g[c]) {
			int t = dfs(d);
			s += t;
			if (best < t) best = t, swap(d, g[c][0]);
		}
		return s;
	}
	int add(int k, int l, int r, Type t) {
		if (k == -1) k = add_buf++;
		P[k] = -1, L[k] = l, R[k] = r, T[k] = t;
		if (l != -1) P[l] = k;
		if (r != -1) P[r] = k;
		return k;
	}
	pair<int, int> merge(const vector<pair<int, int>> &a, Type t) {
		if (a.size() == 1) return a[0];
		int u = 0;
		for (auto &[_, s] : a) u += s;
		vector<pair<int, int>> b, c;
		for (auto &[i, s] : a) (u > s ? b : c).emplace_back(i, s), u -= s * 2;
		auto [i, si] = merge(b, t);
		auto [j, sj] = merge(c, t);
		return {add(-1, i, j, t), si + sj};
	}
	pair<int, int> compress(int i) {
		vector<pair<int, int>> chs{add_vertex(i)};
		while (!g[i].empty()) chs.push_back(add_vertex(i = g[i][0]));
		return merge(chs, Type::Compress);
	}
	pair<int, int> rake(int i) {
		vector<pair<int, int>> chs;
		for (int j = 1; j < (int)g[i].size(); j++)
			chs.push_back(add_edge(g[i][j]));
		return chs.empty() ? make_pair(-1, 0) : merge(chs, Type::Rake);
	}
	pair<int, int> add_edge(int i) {
		auto [j, sj] = compress(i);
		return {add(-1, j, -1, Type::AddEdge), sj};
	}
	pair<int, int> add_vertex(int i) {
		auto [j, sj] = rake(i);
		return {add(i, j, -1, j == -1 ? Type::Vertex : Type::AddVertex),
		        sj + 1};
	}
	void build() {
		dfs(root);
		auto [i, n] = compress(root);
		stt_root = i;
	}
};

// #include "atcoder/modint.hpp"
// using mint = atcoder::modint998244353;

vector<int> A;
struct Path {
	bigint a, b;
};
using Point = bigint;

Path vertex(int v) {
	assert(nodes_by_id.at(v)->op == '.');
	return Path{0, nodes_by_id.at(v)->val};
}
Path compress(Path p, Path c) {
	// (c.a * x + c.b) * p.a + p.b
	return {c.a * p.a, c.b * p.a + p.b};
}
Path add_vertex(Point t, int v) {
	if (nodes_by_id.at(v)->op == '*') return {t, 0};
	if (nodes_by_id.at(v)->op == '+') return {1, t};
	if (nodes_by_id.at(v)->op == '-') {
		if (g.at(v).at(0) == children.at(v).f) { return {1, -t}; }
		assert(g.at(v).at(0) == children.at(v).s);
		return {-1, t};
	}
	assert(false);
}

Point rake(Point x, Point y) { assert(false); }
Point add_edge(Path t) { return t.b; }

int main() {
	// read read read
	setIO();
	def(int, N);
	re(s);
	s = s + ")";
	dbg(s);
	auto ret = dfs(0);
	assert(ret.f == sz(s));
	// ps(ret.s->eval());
	ret.s->assign();

	StaticTopTree stt{g};
	vector<Path> path(stt.L.size());
	vector<Point> point(stt.L.size());

	auto update = [&](int k) {
		if (stt.T[k] == Type::Vertex) {
			path[k] = vertex(k);
		} else if (stt.T[k] == Type::Compress) {
			path[k] = compress(path[stt.L[k]], path[stt.R[k]]);
		} else if (stt.T[k] == Type::Rake) {
			point[k] = rake(point[stt.L[k]], point[stt.R[k]]);
		} else if (stt.T[k] == Type::AddEdge) {
			point[k] = add_edge(path[stt.L[k]]);
		} else {
			path[k] = add_vertex(point[stt.L[k]], k);
		}
	};
	auto dfs = [&](auto rc, int k) -> void {
		if (stt.L[k] != -1) rc(rc, stt.L[k]);
		if (stt.R[k] != -1) rc(rc, stt.R[k]);
		update(k);
	};
	dfs(dfs, stt.stt_root);
	ps(add_edge(path[stt.stt_root]));
	// you should actually read the stuff at the bottom
}

/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smth instead of nothing and stay organized
 * WRITE STUFF DOWN
 * DON'T GET STUCK ON ONE APPROACH
 */
0