#include <bits/stdc++.h>
using namespace std;
const int k_N = 1e4 + 3;
const long long k_Mod = 1e6 + 7;
int n, a[k_N];
long long dp[2][k_N][2];
long long solve(int pos, int cnt, bool equal){
if(pos == n) return 1;
return dp[pos & 1][cnt][equal];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; ++i)
cin >> a[i];
for(int pos = n - 1; pos >= 0; --pos){
for(int cnt = 0; cnt <= pos; ++cnt){
for(short equal = 0; equal <= 1; ++equal){
auto &ans = dp[pos & 1][cnt][equal];
ans = 0;
if(!equal){
ans = (solve(pos + 1, cnt, false) * cnt + solve(pos + 1, cnt + 1, false)) % k_Mod;
continue;
}
ans = solve(pos + 1, cnt, false) * min(cnt, a[pos] - 1) % k_Mod;
if(a[pos] <= cnt) ans += solve(pos + 1, cnt, true);
else if(a[pos] == cnt + 1) ans += solve(pos + 1, cnt + 1, true);
else if(a[pos] > cnt + 1) ans += solve(pos + 1, cnt + 1, false);
ans %= k_Mod;
}
}
}
cout << solve(0, 0, true) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
448 ms |
740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
512 KB |
Output is correct |
2 |
Correct |
111 ms |
512 KB |
Output is correct |
3 |
Correct |
119 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
439 ms |
760 KB |
Output is correct |
2 |
Correct |
427 ms |
768 KB |
Output is correct |
3 |
Correct |
556 ms |
780 KB |
Output is correct |