This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3phCa4T ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
signed main(){
_3phCa4T;
int n;
cin>>n;
vi a(n);
rep(i,n){
cin>>a[i];
}
string s;
cin>>s;
const int m=sz(s);
vi rp(m);
vi stk;
rep(i,m){
if(s[i]=='('){
stk.pb(i);
}else if(s[i]==')'){
rp[stk.back()]=i;
stk.pop_back();
}
}
using ld=long double;
auto dfs=[&](auto self,int l,int r)->ld{
if(l==r-2){
return a[0];
}
vec(ld) rbts;
char expr;
for(int i=l+1;i<r;i++){
if(s[i]=='('){
rbts.pb(self(self,i,rp[i]));
i=rp[i];
}else{
expr=s[i];
}
}
ld sun=0;
for(auto v:rbts){
sun+=v;
}
ld need=a[sz(rbts)-1];
if(expr=='+'){
return min(need,sun);
}
if(sun>need){
sort(rbts.rbegin(), rbts.rend());
ld x=rbts[0];
rng(i,1,sz(rbts)+1){
if(i<sz(rbts) and sun-(x-rbts[i])*i>=need){
sun-=(x-rbts[i])*i;
x=rbts[i];
}else{
rep(j,i){
rbts[j]=x;
}
ld k=(sun-need)/i;
rep(j,i){
rbts[j]-=k;
}
break;
}
}
}
ld _prod=1;
for(auto v:rbts){
_prod*=v;
}
return _prod;
};
cout<<fixed<<setprecision(16);
ld res=dfs(dfs,0,rp[0]);
print(res);
}
Compilation message (stderr)
kvalitetni.cpp: In lambda function:
kvalitetni.cpp:60:3: warning: 'expr' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | if(expr=='+'){
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |