[Java/Python 3] straightforward code with comment -- O(n) time and O(1) extra space
java\n    private int idx = 0;\n    private int[] A;\n\t\t\n    public RLEIterator(int[] A) { this.A = A; } // Injected A[]\n    \n    public int next(int n) {\
```java\n    private int idx = 0;\n    private int[] A;\n\t\t\n    public RLEIterator(int[] A) { this.A = A; } // Injected A[]\n    \n    public int next(int n) {\n        while (idx < A.length && n > A[idx]) { // exhaust as many terms as possible.\n            n -= A[idx]; // exhaust A[idx + 1] for A[idx] times. \n            idx += 2; // move to next term.\n        }\n        if (idx < A.length) { // not exhaust all terms.\n            A[idx] -= n;\n            return A[idx + 1];\n        }\n        return -1; // exhaust all terms but still not enough.\n    }\n```\nSimilar but clear logic version - credit to **@chenzuojing**\n```java\n    private int[] A;\n    private int index;\n    \n    public RLEIterator0(int[] A) {\n        this.A = A; // Injected A[]\n    }\n    \n    public int next(int n) {\n        while (index < A.length) { // all elements exhausted?\n            if (n <= A[index]) { // find the corresponding value.\n                A[index] -= n; // deduct n from A[index].\n                return A[index + 1]; // A[index + 1] is the nth value.\n            }\n            n -= A[index]; // not find the value yet, deduct A[index] from n.\n            index += 2; // next group of same values.\n        }\n        return -1; // not enough values remaining.\n    }\n```\n```python\n\n    def __init__(self, A: List[int]):\n        self.A = A\n        self.index = 0\n        \n    def next(self, n: int) -> int:\n        while self.index < len(self.A):\n            if n <= self.A[self.index]:\n                self.A[self.index] -= n\n                return self.A[self.index + 1]\n            n -= self.A[self.index]\n            self.index += 2\n        return -1\n```
[]