# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1092580 | Trisanu_Das | Cat Exercise (JOI23_ho_t4) | C++17 | 0 ms | 0 KiB |
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;
typedef long long ll;
typedef long double ld;
#define ff first
#define ss second
int n; vector<pair<int, int>> pos;
int f(ll l, ll r, ll nw){
if(r <= l) return abs(nw - r);
int ans = 0, ht;
for(int i = 0; i < n; i++){
if(l <= pos[i].ss && pos[i].ss <= r){
ht = pos[i].ss;
ans += abs(nw - ht);
break;
}
}
return ans + max(f(l, ht - 1, ht), f(ht + 1, r, ht));
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
p.resize(n);
for(int i=0; i<n; i++){
cin >> pos[i].ff;
pos[i].ss = i;
}
sort(pos.begin(), pos.end(), greater<pair<int, int> >());
int a, b; for(int i = 0; i < n - 1; i++) cin >> a >> b;
cout << f(0, n - 1, pos[0].ss) << '\n';
}