This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define int long long
using namespace std;
const int sz = 1e4 + 5, mod = 1e9 + 7;
int n, a[sz], mx[sz], dp[2][sz];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
dp[0][i] = 1;
cin >> a[i];
mx[i] = max(mx[i - 1], a[i]);
}
int ans = 1;
for(int i = n, cur = 1; i >= 1; i--, cur ^= 1)
{
ans += dp[cur ^ 1][mx[i - 1]] * (a[i] - 1);
ans %= mod;
for(int j = 1; j <= i; j++)
{
dp[cur][j] = (dp[cur ^ 1][j] * j + dp[cur ^ 1][j + 1]) % mod;
}
}
cout << ans;
}
// 1 1 1 1
// 1 1 1 2
// 1 1 1 3
// 1 1 2 1
// 1 1 2 2
// 1 1 2 3 *****
// 1 2 1 1
// 1 2 1 2
// 1 2 1 3
// 1 2 2 1
// 1 2 2 2
// 1 2 2 3
// 1 1 1
// 1 1 2 ---
// 1 2 1
// 1 2 2
// 1 2 3
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |