import std.stdio; import std.string; import std.conv; import std.algorithm; const ulong MAX_COST = 10UL ^^ 13; void main(){ auto N = readln.chomp.to!ulong; auto S = readln.chomp; auto A = readln.chomp.split.to!(ulong[]); auto prev = new ulong[N + 1]; prev[] = MAX_COST; prev[0] = 0; foreach(i, c; S){ auto cost = new ulong[N + 1]; cost[] = MAX_COST; for(auto j = 0; j < N + 1; j++){ if(j > 0){ cost[j] = prev[j - 1] + (c == '(' ? 0 : A[i]); } if(j < N){ cost[j] = min(cost[j], prev[j + 1] + (c == ')' ? 0 : A[i])); } } //stderr.writeln(cost); prev = cost; } prev[0].writeln; }