// Code by Parsa Eslami
#include <bits/stdc++.h>
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define S second
#define F first
#define pb push_back
#define SZ(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define err(x) cout<<#x<<": "<<x<<'\n';
using namespace std;
const int MOD=1e6+7;
const int N=1e4+5;
int dp[2][N];
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
int ans=0;
int mx[n+1]={};
int a[n+1]={};
FOR(i,1,n){
cin>>a[i];
mx[i]=max(mx[i-1],a[i]);
}
ans=a[n];
dp[(n%2)][0]=1;
FOR(i,1,n){
dp[(n%2)][i]=(1LL*dp[(n%2)][i-1]*n)%MOD;
}
int ind=n-1;
FORR(j,n-1,1){
FOR(i,0,n){
if(i==0) {dp[(j%2)][i]=1; continue;}
dp[(j%2)][i]=(1LL*dp[(j+1)%2][i-1]+1LL*dp[(j%2)][i-1]*j)%MOD;
}
while(ind>0&&mx[ind-1]==j){
ans=(1LL*ans+1LL*(a[ind]-1)*dp[j%2][n-ind])%MOD;
ind--;
}
}
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
336 KB |
Output is correct |
2 |
Correct |
9 ms |
332 KB |
Output is correct |
3 |
Correct |
9 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
762 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
332 KB |
Output is correct |
2 |
Correct |
195 ms |
392 KB |
Output is correct |
3 |
Correct |
196 ms |
400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
772 ms |
460 KB |
Output is correct |
2 |
Correct |
760 ms |
472 KB |
Output is correct |
3 |
Correct |
752 ms |
496 KB |
Output is correct |