ConvergingArray

<?xml encoding="utf-8" ?><p>import java.util.*</p><p>&nbsp;</p><p>fun main() {</p><p>&nbsp; &nbsp; val sc = Scanner(System.`in`)</p><p>&nbsp; &nbsp; if (!sc.hasNextInt()) return</p><p>&nbsp; &nbsp; val n = sc.nextInt()</p><p>&nbsp; &nbsp; val c = IntArray(n) { sc.nextInt() }</p><p>&nbsp; &nbsp; val b = IntArray(n - 1) { sc.nextInt() }</p><p>&nbsp; &nbsp; val q = sc.nextInt()</p><p>&nbsp; &nbsp; val x = sc.nextLong()</p><p>&nbsp;</p><p>&nbsp; &nbsp; val MOD = 1_000_000_007</p><p>&nbsp;</p><p>&nbsp; &nbsp; // P_i = \sum_{j=1}^{i-1} (i-j) b_j = P_{i-1} + \sum_{j=1}^{i-2} b_j + (i-(i-1))b_{i-1} ? NO.</p><p>&nbsp; &nbsp; // P_1 = 0</p><p>&nbsp; &nbsp; // P_2 = b_1</p><p>&nbsp; &nbsp; // P_3 = 2*b_1 + b_2</p><p>&nbsp; &nbsp; // P_4 = 3*b_1 + 2*b_2 + b_3</p><p>&nbsp; &nbsp; // P_i = \sum_{j=1}^{i-1} \sum_{k=1}^{j} b_k</p><p>&nbsp; &nbsp; val P = LongArray(n + 1)</p><p>&nbsp; &nbsp; val prefB = LongArray(n)</p><p>&nbsp; &nbsp; for (i in 0 until n - 1) prefB[i + 1] = prefB[i] + b[i]</p><p>&nbsp; &nbsp; P[1] = 0</p><p>&nbsp; &nbsp; for (i in 2..n) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; P[i] = P[i - 1] + prefB[i - 1]</p><p>&nbsp; &nbsp; }</p><p>&nbsp;</p><p>&nbsp; &nbsp; // max sum is N * max(C) = 100 * 100 = 10000</p><p>&nbsp; &nbsp; val maxSum = n * 100</p><p>&nbsp; &nbsp; var dp = IntArray(maxSum + 1)</p><p>&nbsp; &nbsp; dp[0] = 1</p><p>&nbsp;</p><p>&nbsp; &nbsp; for (i in 1..n) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; val nextDp = IntArray(maxSum + 1)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; val prefDp = IntArray(maxSum + 2)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; for (s in 0..maxSum) prefDp[s + 1] = (prefDp[s] + dp[s]) % MOD</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; for (s in 0..maxSum) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Need: i*x - P_i &lt;= current_sum &lt;= s, current_sum = s_prev + a_i</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // a_i in [0, c[i-1]]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // max a_i is c[i-1]</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Need to check if s allows the condition \sum_{j=1}^i a_j &gt;= i*x - P_i</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (s &lt; i * x - P[i]) continue</p><p>&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val minPrevS = Math.max(0, s - c[i - 1])</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val maxPrevS = s</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (minPrevS &lt;= maxPrevS) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var sum = (prefDp[maxPrevS + 1] - prefDp[minPrevS]) % MOD</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (sum &lt; 0) sum += MOD</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextDp[s] = sum</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; }</p><p>&nbsp; &nbsp; &nbsp; &nbsp; dp = nextDp</p><p>&nbsp; &nbsp; }</p><p>&nbsp;</p><p>&nbsp; &nbsp; var total = 0L</p><p>&nbsp; &nbsp; for (s in dp) {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; total = (total + s) % MOD</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp; println(total)</p><p>}</p><p>&nbsp;</p><p>&nbsp;</p>