#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
struct seg
{
int t[800010];
void init(int n,int s,int e)
{
t[n]=e-s+1;
if(s==e)
return;
int m=s+(e-s)/2;
init(n*2,s,m);
init(n*2+1,m+1,e);
return;
}
void upd(int n,int s,int e,int x)
{
t[n]--;
if(s==e)
return;
int m=s+(e-s)/2;
if(x>m)
upd(n*2+1,m+1,e,x);
else
upd(n*2,s,m,x);
return;
}
int sum(int n,int s,int e,int E)
{
if(s>E)
return 0;
if(e<=E)
return t[n];
int m=s+(e-s)/2;
return sum(n*2,s,m,E)+sum(n*2+1,m+1,e,E);
}
int query(int n,int s,int e,int k)
{
if(s==e)
return s;
int m=s+(e-s)/2;
if(t[n*2]<k)
return query(n*2+1,m+1,e,k-t[n*2]);
return query(n*2,s,m,k);
}
}st;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vector<int>v(n);
for(int&t:v)
cin>>t;
v.insert(v.begin(),0);
st.init(1,1,n);
ll ans=0;
for(int i=n;i>0;i--)
{
int t=v[i];
int sm=st.sum(1,1,n,t);
if(sm==0)
return cout<<-1<<endl,0;
if(i>sm)
st.upd(1,1,n,st.query(1,1,n,sm)),ans+=i-sm;
else
st.upd(1,1,n,st.query(1,1,n,i));
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
2 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
2 ms |
384 KB |
Output is correct |
35 |
Correct |
2 ms |
384 KB |
Output is correct |
36 |
Correct |
2 ms |
512 KB |
Output is correct |
37 |
Correct |
2 ms |
384 KB |
Output is correct |
38 |
Correct |
2 ms |
384 KB |
Output is correct |
39 |
Correct |
2 ms |
384 KB |
Output is correct |
40 |
Correct |
3 ms |
384 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
2 ms |
384 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
100 ms |
3176 KB |
Output is correct |
47 |
Correct |
90 ms |
3184 KB |
Output is correct |
48 |
Correct |
85 ms |
3308 KB |
Output is correct |
49 |
Correct |
79 ms |
3176 KB |
Output is correct |
50 |
Correct |
89 ms |
3308 KB |
Output is correct |
51 |
Correct |
96 ms |
3184 KB |
Output is correct |
52 |
Correct |
124 ms |
3240 KB |
Output is correct |
53 |
Correct |
113 ms |
3184 KB |
Output is correct |
54 |
Correct |
96 ms |
3300 KB |
Output is correct |
55 |
Correct |
75 ms |
3304 KB |
Output is correct |
56 |
Correct |
85 ms |
3188 KB |
Output is correct |
57 |
Correct |
114 ms |
3176 KB |
Output is correct |
58 |
Correct |
117 ms |
3240 KB |
Output is correct |
59 |
Correct |
111 ms |
3380 KB |
Output is correct |
60 |
Correct |
103 ms |
3184 KB |
Output is correct |
61 |
Correct |
84 ms |
3184 KB |
Output is correct |
62 |
Correct |
73 ms |
3232 KB |
Output is correct |
63 |
Correct |
89 ms |
3184 KB |
Output is correct |
64 |
Correct |
107 ms |
3176 KB |
Output is correct |
65 |
Correct |
103 ms |
3160 KB |
Output is correct |
66 |
Correct |
99 ms |
3176 KB |
Output is correct |
67 |
Correct |
91 ms |
3304 KB |
Output is correct |
68 |
Correct |
95 ms |
3304 KB |
Output is correct |
69 |
Correct |
105 ms |
3240 KB |
Output is correct |
70 |
Correct |
102 ms |
3180 KB |
Output is correct |
71 |
Correct |
92 ms |
3180 KB |
Output is correct |
72 |
Correct |
90 ms |
3184 KB |
Output is correct |
73 |
Correct |
82 ms |
3188 KB |
Output is correct |
74 |
Correct |
84 ms |
3304 KB |
Output is correct |
75 |
Correct |
117 ms |
3176 KB |
Output is correct |
76 |
Correct |
92 ms |
3188 KB |
Output is correct |
77 |
Correct |
66 ms |
3184 KB |
Output is correct |
78 |
Correct |
69 ms |
3184 KB |
Output is correct |
79 |
Correct |
69 ms |
3184 KB |
Output is correct |
80 |
Correct |
90 ms |
3184 KB |
Output is correct |
81 |
Correct |
63 ms |
3184 KB |
Output is correct |
82 |
Correct |
69 ms |
3176 KB |
Output is correct |
83 |
Correct |
69 ms |
3184 KB |
Output is correct |
84 |
Correct |
110 ms |
3300 KB |
Output is correct |
85 |
Correct |
105 ms |
3340 KB |
Output is correct |
86 |
Correct |
99 ms |
3184 KB |
Output is correct |
87 |
Correct |
101 ms |
3304 KB |
Output is correct |
88 |
Correct |
114 ms |
3308 KB |
Output is correct |