#include #include #include #include #include #include using namespace std; #define mod 1000000007 #define maxn 100000 int n, k, a[maxn], i, helper[maxn]; double dp[maxn]; priority_queue > pq; int main (int argc, char * const argv[]) { scanf("%d %d", &n, &k); // reading the number of streets and k assert(1 <= n && n <= 100000); // input validation assert(1 <= k && k <= n); // input validation for(i = 0; i < n; i++) { scanf("%d", &a[i]); // reading the special number of the i-th street assert(1 <= a[i] && a[i] <= 100000); // input validation } /* then we will use the following observation: a * b < c * d <=> log(a) + log(b) < log(c) + log(d) so we can store minimal logarithms' sum on the way to the i-th street in dp[i] helper[i] is the product modulo 1000000007 itself */ dp[0] = log(a[0]); // initializing dp for the first street helper[0] = a[0]; // initializing helper for the first street pq.push(make_pair(-dp[0], 0)); // using priority queue in order to the the optimal "from" street for(i = 1; i < n; i++) { while (i - pq.top().second > k) pq.pop(); // delete all the streets, from which we're no longer able to reach the current dp[i] = dp[pq.top().second] + log(a[i]); // take the optimal previous street to recalculate dp helper[i] = (helper[pq.top().second] * 1LL * a[i]) % mod; // and helper pq.push(make_pair(-dp[i], i)); // adding this street to the priority queue to be able to recalculate some dp values from it } printf("%d\n", helper[n - 1]); // outputting an answer return 0; }