#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace atcoder; using namespace std; using ll=long long; using ull=unsigned long long; using P=pair; templateusing minque=priority_queue,greater>; templatebool chmax(T &a,const T &b){return (abool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} templateostream &operator<<(ostream &os,const pair&p){os<istream &operator>>(istream &is,pair&p){is>>p.first>>p.second;return is;} template ostream &operator<<(ostream &os,const vector &a){cout<<'{';for(int i=0;iistream &operator>>(istream &is,vector &a){for(auto &i:a)is>>i;return is;} #define reps(i,a,n) for(int i=(a);i<(n);i++) #define rep(i,n) reps(i,0,n) #define all(x) x.begin(),x.end() ll myceil(ll a,ll b){return (a+b-1)/b;} template void debug(const T1& x,const T2& ...y){ #ifdef LOCAL cout<>testcase; for(int i=0;i>n; vectora(n); cin>>a; vectorseen(n,false); vectorb; rep(i,n){ a[i]--; if(a[i]=n)a.pop_back(); int et=n-1; ll ans=0; debug(et); while(!b.empty()){ while(seen[et])et--; int x=b.back(); b.pop_back(); debug(x); ans+=x-et-(n-1-((int)a.size()-(lower_bound(all(a),et)-a.begin()))); debug(x); seen[et]=true; } cout<