#include <cstdio>
#include <cassert>
#include <vector>
#include <stack>
#include <cstring>
#include <algorithm>
#define let const auto
#define rep(name,beg,end) for(auto lim_##name = end,name = beg;name <= lim_##name;name++)
#define per(name,beg,end) for(auto lim_##name = end,name = beg;name >= lim_##name;name--)
#define repn(lim) for(auto REPN_lIM = lim,REPN = 1;REPN <= REPN_lIM;REPN++)
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define trace() debug("line : %d, Function : %s\n",__LINE__,__FUNCTION__)
using type = double;
constexpr int maxn = 2e6 + 10;
char str[maxn];
std::vector<int> G[maxn];
int k,n,tot,lim[64],match[maxn],opt[maxn];
int build(const int l = 1,const int r = n){
if(str[l] == '(' && match[l] == r)return build(l + 1,r - 1);
if(l == r)return ++tot;
assert(match[l] + 1 <= r);
let c = str[match[l] + 1];
let x = ++tot;
opt[x] = c == '+' ? 1 : 2;
for(int i = l;i <= r;i = match[i] + 2)
G[x].push_back(build(i,match[i]));
return x;
}
type solve(const int x){
if(opt[x] == 0)return lim[1];
assert(2 <= G[x].size() && (int)G[x].size() <= k);
if(opt[x] == 1){
type res = 0;
for(let v : G[x])
res += solve(v);
return std::min(res,(type)lim[G[x].size()]);
}
if(opt[x] == 2){
std::vector<type> vec;
for(let v : G[x])vec.push_back(solve(v));
std::sort(vec.begin(),vec.end());
int rem = G[x].size();
type sum = lim[G[x].size()],res = 1.0;
for(let v : vec){
let t = std::min(v,sum / rem);
res *= t;
sum -= t;
rem--;
}
return res;
}
assert(false);
}
int main(){
// std::freopen("kvalitetni.in","r",stdin);
// std::freopen("kvalitetni.out","w",stdout);
std::scanf("%d",&k);
rep(i,1,k)std::scanf("%d",lim + i);
std::scanf("%s",str + 1);
n = std::strlen(str + 1);
std::stack<int> stk;
rep(i,1,n)
if(str[i] == '(')stk.push(i);
else if(str[i] == ')'){
match[stk.top()] = i;
match[i] = stk.top();
stk.pop();
}
let rt = build(1,n);
std::printf("%.9lf\n",solve(rt));
// std::fclose(stdin);
// std::fclose(stdout);
return 0;
}
Compilation message
kvalitetni.cpp: In function 'int main()':
kvalitetni.cpp:58:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | std::scanf("%d",&k);
| ~~~~~~~~~~^~~~~~~~~
kvalitetni.cpp:59:22: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | rep(i,1,k)std::scanf("%d",lim + i);
| ~~~~~~~~~~^~~~~~~~~~~~~~
kvalitetni.cpp:60:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | std::scanf("%s",str + 1);
| ~~~~~~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47204 KB |
Output is correct |
2 |
Correct |
22 ms |
47212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47244 KB |
Output is correct |
2 |
Correct |
22 ms |
47180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47304 KB |
Output is correct |
2 |
Correct |
23 ms |
47256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47308 KB |
Output is correct |
2 |
Correct |
26 ms |
47436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47208 KB |
Output is correct |
2 |
Correct |
25 ms |
47192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47316 KB |
Output is correct |
2 |
Correct |
26 ms |
47428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
47208 KB |
Output is correct |
2 |
Correct |
30 ms |
48968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
48008 KB |
Output is correct |
2 |
Correct |
31 ms |
49152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
48712 KB |
Output is correct |
2 |
Correct |
33 ms |
50372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
48280 KB |
Output is correct |
2 |
Correct |
38 ms |
49980 KB |
Output is correct |