Optimal way to split a array

Consider a array $ a$ with $ n$ elements .the goal is to divide the array into segments ,which are continuous,formally from $ 1$ to $ i$ ,then $ i+1$ to $ j$ and $ j+1$ to $ k$ and so on .Cost for making a new segment is $ x$ and also if a segment has a element that occurs $ i,i>1$ times then final cost increases by $ i$ .Find the minimum possible cost.

My idea is that at first i would need a segment so for $ a[0]$ i will make one and then i will proceed in array and add current element greedily and by greedily by adding $ a[i]$ the cost may not increase if it never occurred in current segment or if it has occurred then i will check if cost increased by this element and cost for a new segment ,if cost is minimum in adding this element to current segment i will do this or else i will make a new segment starting with $ a[i]$ . But my greedy algorithm is wrong ,can anyone help me why my algorithm is worng?