Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are given an array of n integers.
- You want to modify the array so that it is increasing, i.e., every element is at least as large as the previous element.
- On each turn, you may increase the value of any element by one. What is the minimum number of turns required?
- Input:
- The first input line contains an integer n: the size of the array.
- Then, the second line contains n integers x1,x2,…,xn: the contents of the array.
- Output:
- Print the minimum number of turns.
- */
- #include <iostream>
- using namespace std;
- void minTurns(int n, long int* arr){
- long long turns = 0;
- for(int i=0;i<n;i++){
- if(arr[i+1]<arr[i]){
- turns+=arr[i]-arr[i+1];
- arr[i+1]=arr[i];
- }
- else
- continue;
- }
- cout<<turns;
- }
- int main(){
- int n;
- cin>>n;
- long* arr = new long[n];
- for(int j=0;j<n;j++)
- cin>>arr[j];
- minTurns(n,arr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement