Company: Google

Difficulty: hard

Problem Statement

Minimum Cost to Remove All Stones There are N stones in a row, indexed from 0. Stone i has costs a[i] and b[i] . Remove all stones, one at a time. At the moment stone i is removed: if both of its original neighbours are still present, its cost is b[i] ; if exactly one original neighbour is still present, its cost is a[i] ; if neither original neighbour is still present, its cost is 0. An endpoint has only one original neighbour. Find the minimum total cost. Input The first line contains N . The second line contains a[0] ... a[N-1] . The third line contains b[0] ... b[N-1] . Output Print the minimum total cost. Constraints 1 <= N <= 50000 1 <= a[i], b[i] <= 1000 Example Input: 3 5 2 5 9 1 9 Output: 1 Notes Only neighbours in the original row matter; removing a stone does not make more distant stones become neighbours.

More Google OA questionsInterview experiences