O(n)
We know the value of k-th element. Elements before k (and the element itself) will appear in the line min(t[k], t[i]) times.\n\nAll elements after k will appear
We know the value of `k`-th element. Elements before `k` (and the element itself) will appear in the line `min(t[k], t[i])` times.\n\nAll elements after `k` will appear in the line  `min(t[k] - 1, t[i])` times.\n\n**Python 3**\n```python\nclass Solution:\n    def timeRequiredToBuy(self, t: List[int], k: int) -> int:\n        return sum(min(v, t[k] - (i > k)) for i, v in enumerate(t))\n```\n\nAlternative solution, matching the explanation above.\n```python\nclass Solution:\n    def timeRequiredToBuy(self, t: List[int], k: int) -> int:\n        return sum([min(t[i], t[k]) for i in range(k + 1)] + [min(t[i], t[k] - 1) for i in range(k + 1, len(t))])\n```\n**Java**\n```java\npublic int timeRequiredToBuy(int[] t, int k) {\n    return IntStream.range(0, t.length).map(i -> Math.min(t[i], t[k] - (i > k ? 1 : 0))).sum();\n}\n```\n**C++**\n```cpp\nint timeRequiredToBuy(vector<int>& t, int k) {\n    int res = 0;\n    for (int i = 0; i < t.size(); ++i)\n        res += min(t[k] - (i > k), t[i]);\n    return res;\n}\n```\nAlso a (slightly mouthful) functional version.\n```cpp\nint timeRequiredToBuy(vector<int>& t, int k) {\n    return accumulate(begin(t), begin(t) + k + 1, 0, [&](int s, int v){ return s + min(v, t[k]); })\n        + accumulate(begin(t) + k + 1, end(t), 0, [&](int s, int v){ return s + min(v, t[k] - 1); });\n}\n```
['C', 'Python', 'Java']