QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#408549#8422. Tree Average WeightieeAC ✓18ms7816kbC++17906b2024-05-10 17:04:232024-05-10 17:04:27

Judging History

This is the latest submission verdict.

  • [2024-05-10 17:04:27]
  • Judged
  • Verdict: AC
  • Time: 18ms
  • Memory: 7816kb
  • [2024-05-10 17:04:23]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mk make_pair
#define lowbit(x) (x&(-x))
#define pb emplace_back
#define pr pair<int,int>
#define let const auto
#define all(A) A.begin(),A.end()
void chkmin(int &x,int y){x=min(x,y);}
void chkmax(int &x,int y){x=max(x,y);}
const int N=1e6+5;
#define i128 __int128
int read(){
	int x=0,f=1; char c=getchar();
	while(('0'>c||c>'9')&&c!='-') c=getchar();
	if(c=='-') f=0,c=getchar();
	while('0'<=c&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return f?x:-x;
}
int n,a[N];
int main(){
	n=read();
	for(int i=1; i<=n; i++) a[i]=read()-1;
	int sum=n-2,num=n;
	ll s=1ll*n*(n+1)/2;
	ll ans=1ll*n*(n+1)/2,res=0;
	for(int i=1; i<=n; i++) 
		if(a[i]>=0) s-=i,sum-=a[i],num--,res+=1ll*i*a[i]*n;
	if(num) res+=(i128)sum*s*n/num;
	ans+=res;
	printf("%lld\n",ans);
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3936kb

input:

5
1 -1 -1 -1 -1

output:

67

result:

ok 1 number(s): "67"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3704kb

input:

5
-1 -1 -1 -1 1

output:

52

result:

ok 1 number(s): "52"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

4
1 1 1 3

output:

42

result:

ok 1 number(s): "42"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3692kb

input:

4
1 1 2 2

output:

38

result:

ok 1 number(s): "38"

Test #5:

score: 0
Accepted
time: 0ms
memory: 3692kb

input:

2
-1 -1

output:

3

result:

ok 1 number(s): "3"

Test #6:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

3
-1 -1 -1

output:

12

result:

ok 1 number(s): "12"

Test #7:

score: 0
Accepted
time: 0ms
memory: 3780kb

input:

4
-1 -1 -1 -1

output:

30

result:

ok 1 number(s): "30"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

4
1 -1 -1 -1

output:

34

result:

ok 1 number(s): "34"

Test #9:

score: 0
Accepted
time: 0ms
memory: 3740kb

input:

4
2 -1 -1 -1

output:

26

result:

ok 1 number(s): "26"

Test #10:

score: 0
Accepted
time: 0ms
memory: 4008kb

input:

4
3 -1 -1 -1

output:

18

result:

ok 1 number(s): "18"

Test #11:

score: 0
Accepted
time: 0ms
memory: 3684kb

input:

8
-1 -1 -1 -1 -1 -1 -1 -1

output:

252

result:

ok 1 number(s): "252"

Test #12:

score: 0
Accepted
time: 0ms
memory: 3692kb

input:

9
-1 -1 -1 -1 -1 -1 -1 -1 -1

output:

360

result:

ok 1 number(s): "360"

Test #13:

score: 0
Accepted
time: 1ms
memory: 3664kb

input:

10
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1

output:

495

result:

ok 1 number(s): "495"

Test #14:

score: 0
Accepted
time: 0ms
memory: 3736kb

input:

2
1 1

output:

3

result:

ok 1 number(s): "3"

Test #15:

score: 0
Accepted
time: 0ms
memory: 4004kb

input:

2
-1 1

output:

3

result:

ok 1 number(s): "3"

Test #16:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

2
-1 -1

output:

3

result:

ok 1 number(s): "3"

Test #17:

score: 0
Accepted
time: 0ms
memory: 3936kb

input:

100
3 1 1 4 2 3 1 2 3 3 4 2 1 2 1 1 2 1 1 2 2 1 1 2 3 1 2 1 1 2 1 1 1 4 2 2 1 1 1 2 2 3 1 2 2 1 1 2 2 4 1 1 1 1 3 2 6 2 1 3 1 1 1 3 2 2 3 2 1 1 2 2 2 2 2 1 3 2 1 3 2 2 1 2 2 5 4 4 2 2 1 1 2 1 4 3 3 3 2 2

output:

543550

result:

ok 1 number(s): "543550"

Test #18:

score: 0
Accepted
time: 0ms
memory: 3700kb

input:

111
3 1 2 3 2 1 2 -1 1 3 -1 1 3 2 4 1 -1 3 1 1 2 -1 3 2 4 3 4 2 3 2 1 2 2 3 2 1 4 2 3 4 1 1 1 -1 2 1 1 1 2 1 3 1 3 2 1 3 2 1 1 1 3 2 -1 1 -1 2 2 1 -1 1 3 1 1 2 1 2 3 1 -1 2 2 2 -1 -1 2 -1 2 3 2 3 1 1 3 4 1 2 2 3 2 -1 2 2 1 3 1 2 1 4 1 4 1

output:

664813

result:

ok 1 number(s): "664813"

Test #19:

score: 0
Accepted
time: 0ms
memory: 3732kb

input:

111
-1 4 1 3 2 1 2 -1 3 -1 -1 -1 1 -1 -1 -1 -1 -1 1 2 2 3 -1 -1 3 -1 3 -1 1 1 1 3 -1 -1 -1 1 1 -1 -1 -1 2 2 2 1 3 -1 -1 2 1 -1 2 -1 -1 2 1 -1 -1 1 1 -1 2 2 1 -1 -1 -1 3 -1 -1 2 1 -1 -1 1 -1 4 2 -1 -1 -1 2 1 -1 1 2 3 -1 -1 -1 2 1 2 2 -1 -1 2 2 -1 -1 1 -1 -1 -1 -1 1 3 -1 1 2 3 1

output:

665295

result:

ok 1 number(s): "665295"

Test #20:

score: 0
Accepted
time: 0ms
memory: 3708kb

input:

113
1 -1 1 -1 2 -1 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 2 1 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 -1 -1 3 -1 -1 -1 2 -1 -1 -1 -1 2 -1 -1 -1 3 -1 2 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 -1 1 1 2 -1 -1 -1 -1 2 1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 2 -1 -1 -1 2 -1 -1 2 -1 -1 -1 -1 -1 -1 -1 -...

output:

719191

result:

ok 1 number(s): "719191"

Test #21:

score: 0
Accepted
time: 0ms
memory: 3656kb

input:

120
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1...

output:

863940

result:

ok 1 number(s): "863940"

Test #22:

score: 0
Accepted
time: 9ms
memory: 7632kb

input:

1000000
1 2 2 3 2 1 3 2 1 3 4 1 1 2 3 1 2 2 4 1 4 3 1 2 1 2 2 4 1 2 3 2 2 2 1 2 3 1 2 2 2 3 2 2 1 1 2 1 3 2 2 2 1 1 1 2 3 2 3 1 2 1 2 2 1 2 2 4 3 1 1 1 2 2 3 4 2 1 2 2 1 1 2 1 2 3 2 2 4 1 2 4 1 3 3 4 2 2 1 2 1 3 3 2 2 2 2 2 2 2 2 5 2 4 2 2 2 1 3 2 3 3 1 1 5 3 1 1 1 1 2 2 2 1 2 1 2 1 2 1 1 4 4 1 2 2 ...

output:

499668772865500000

result:

ok 1 number(s): "499668772865500000"

Test #23:

score: 0
Accepted
time: 7ms
memory: 7596kb

input:

1000000
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -...

output:

499999999999500000

result:

ok 1 number(s): "499999999999500000"

Test #24:

score: 0
Accepted
time: 9ms
memory: 7604kb

input:

1000000
3 2 1 1 1 4 2 2 2 2 1 1 -1 4 2 2 2 2 3 1 2 2 2 2 -1 1 5 1 2 3 2 3 3 3 1 1 2 3 2 2 1 1 1 1 3 1 2 1 3 -1 3 2 1 3 2 1 1 1 1 1 2 2 2 3 2 3 1 4 1 1 1 2 1 1 2 3 1 2 3 2 4 5 2 2 2 2 1 1 1 2 2 2 3 -1 2 2 3 1 2 3 1 2 2 2 2 1 2 2 1 2 3 1 4 3 1 1 1 2 2 1 2 2 1 1 1 3 2 3 1 1 1 2 2 1 4 2 1 2 3 2 1 3 4 4 ...

output:

499974321934095000

result:

ok 1 number(s): "499974321934095000"

Test #25:

score: 0
Accepted
time: 11ms
memory: 7816kb

input:

1000000
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -...

output:

499933882421758854

result:

ok 1 number(s): "499933882421758854"

Test #26:

score: 0
Accepted
time: 18ms
memory: 7592kb

input:

1000000
2 -1 -1 2 -1 -1 -1 2 -1 3 2 -1 2 -1 2 -1 -1 -1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 1 2 -1 1 2 -1 1 3 -1 3 -1 2 -1 2 -1 -1 -1 -1 -1 -1 3 -1 -1 -1 3 -1 -1 -1 -1 -1 -1 1 2 -1 1 1 2 1 -1 -1 3 -1 -1 -1 -1 -1 3 5 4 -1 -1 3 -1 -1 -1 1 -1 1 2 -1 1 2 -1 -1 -1 -1 1 -1 -1 2 -1 4 1 1 -1 1 1 2 1 2 -1 -1 -1 2 1 1...

output:

499955386620526549

result:

ok 1 number(s): "499955386620526549"

Test #27:

score: 0
Accepted
time: 0ms
memory: 7560kb

input:

999777
5 2 1 1 1 3 2 2 1 3 3 2 1 2 3 2 3 1 2 3 2 3 1 1 1 2 3 2 3 1 1 2 3 1 1 2 4 4 2 3 1 2 1 2 2 2 1 2 2 2 1 3 1 2 3 1 2 1 1 1 2 3 1 1 3 1 4 2 1 3 1 1 1 1 2 3 2 1 2 1 2 1 2 2 1 2 2 2 2 1 1 2 2 2 2 1 3 3 2 2 3 2 3 1 2 1 3 3 2 1 1 3 2 1 3 3 2 3 4 1 3 4 3 1 1 2 3 2 4 3 3 2 2 2 2 3 1 1 3 3 2 2 1 1 2 2 1...

output:

499765687006416622

result:

ok 1 number(s): "499765687006416622"

Test #28:

score: 0
Accepted
time: 3ms
memory: 5984kb

input:

245763
-1 -1 4 2 2 1 2 1 -1 2 2 1 -1 1 1 1 4 -1 2 1 3 -1 2 4 -1 1 -1 2 3 3 1 2 1 -1 2 1 2 -1 2 2 2 1 2 -1 1 -1 1 -1 -1 1 -1 3 -1 2 1 1 1 1 2 2 2 2 1 1 -1 -1 2 1 -1 2 1 1 1 1 2 -1 1 2 3 2 2 1 1 2 2 2 2 1 3 1 4 3 1 2 1 -1 1 1 1 1 -1 3 -1 2 1 4 -1 3 1 1 2 3 -1 3 4 -1 1 -1 2 1 2 2 -1 3 -1 -1 3 -1 1 1 2 ...

output:

7420520871157922

result:

ok 1 number(s): "7420520871157922"

Test #29:

score: 0
Accepted
time: 4ms
memory: 6760kb

input:

785985
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...

output:

242785867241318274

result:

ok 1 number(s): "242785867241318274"

Test #30:

score: 0
Accepted
time: 4ms
memory: 5272kb

input:

386257
1 3 5 3 3 1 2 1 2 2 1 2 3 3 2 1 2 2 2 2 1 -1 2 2 2 5 2 3 -1 1 1 1 2 3 2 1 2 2 4 1 1 1 7 2 2 2 2 2 2 2 3 3 3 1 2 2 1 3 2 2 2 -1 2 2 2 2 1 3 1 1 2 2 1 1 2 2 2 3 2 1 1 2 1 4 2 2 1 3 2 1 2 1 3 5 3 2 2 1 3 1 1 3 1 1 2 2 3 1 1 2 2 3 2 2 3 3 3 4 3 3 -1 3 3 3 1 -1 1 2 3 2 2 2 1 2 2 1 1 2 1 3 1 1 2 1 ...

output:

28826864352211423

result:

ok 1 number(s): "28826864352211423"

Test #31:

score: 0
Accepted
time: 8ms
memory: 7740kb

input:

974693
1 1 2 2 1 2 1 2 2 2 2 2 1 2 2 3 2 1 1 1 1 1 1 1 6 1 2 4 2 1 2 2 1 3 1 4 3 1 1 1 2 2 3 1 2 2 2 2 1 2 3 1 1 3 4 3 1 1 2 1 3 2 2 2 3 1 2 2 2 1 1 3 3 5 2 3 2 1 1 2 3 2 1 1 1 7 3 5 1 2 2 2 3 1 3 3 2 2 2 4 4 3 1 1 1 1 1 4 1 1 1 2 1 2 2 2 2 3 1 2 2 2 1 2 1 2 5 2 3 1 3 1 1 2 2 2 1 1 2 1 3 1 3 1 1 1 2...

output:

463309455987012033

result:

ok 1 number(s): "463309455987012033"

Test #32:

score: 0
Accepted
time: 8ms
memory: 7604kb

input:

1000000
2 -1 1 1 -1 -1 -1 2 1 2 1 2 2 1 2 4 2 -1 -1 -1 -1 1 3 1 1 2 3 2 1 2 2 3 4 -1 -1 1 2 3 1 1 2 2 1 1 1 2 2 3 1 3 1 1 4 -1 1 1 2 3 2 3 1 -1 -1 -1 -1 2 2 1 1 2 1 3 -1 3 3 3 3 2 2 -1 2 -1 2 -1 1 2 2 -1 1 1 1 1 2 2 2 -1 2 5 -1 1 2 1 2 2 1 2 2 1 3 1 2 2 -1 3 4 4 3 1 2 1 -1 2 2 2 3 2 2 2 3 3 1 1 2 2 ...

output:

499797136089087394

result:

ok 1 number(s): "499797136089087394"

Test #33:

score: 0
Accepted
time: 9ms
memory: 7680kb

input:

1000000
1 3 2 -1 1 2 -1 1 1 2 2 4 1 1 2 2 -1 -1 -1 1 4 2 -1 2 2 -1 3 1 2 2 3 2 2 1 2 2 1 2 2 1 1 3 1 2 2 2 -1 -1 2 -1 2 -1 -1 -1 2 1 2 3 1 2 1 -1 2 3 2 -1 1 1 -1 -1 1 -1 3 3 2 2 -1 1 1 -1 3 2 3 1 2 5 1 2 1 2 5 2 3 -1 1 2 2 2 1 3 2 2 4 1 1 -1 2 3 2 1 3 2 1 2 2 1 -1 -1 2 3 1 -1 1 -1 4 1 -1 2 1 2 2 1 2...

output:

499849497199243155

result:

ok 1 number(s): "499849497199243155"