Skip to content

Instantly share code, notes, and snippets.

@gbrls
Created May 27, 2020 19:50
Show Gist options
  • Save gbrls/de8e08170dc0aa633a78213ed3f27e8f to your computer and use it in GitHub Desktop.
Save gbrls/de8e08170dc0aa633a78213ed3f27e8f to your computer and use it in GitHub Desktop.
OBI 2014 fase 1 Fechadura
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,m;cin>>n>>m;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
int acc=0;
for(int i=0;i<n-1;i++) {
int mn = arr[i];
if(arr[i]==m) continue;
if(mn < m) {
arr[i] += m-mn;
arr[i+1] += m-mn;
acc += m-mn;
} else if( mn > m) {
arr[i] -= mn-m;
arr[i+1] -= mn-m;
acc += mn-m;
}
}
cout<<acc<<'\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment