forked from chencorey/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatching Sets.cpp
More file actions
47 lines (44 loc) · 919 Bytes
/
Matching Sets.cpp
File metadata and controls
47 lines (44 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// https://www.hackerrank.com/contests/w22/challenges/box-moving
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
vector<long long> x;
vector<long long> y;
long long smaller = 0;
long long greater = 0;
for(int i = 0; i<n; i++)
{
long long t;
cin>>t;
x.push_back(t);
}
for(int i = 0; i<n; i++)
{
long long t;
cin>>t;
y.push_back(t);
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
for(int i = 0; i<n; i++)
{
if(x[i]<y[i])
{
smaller+=y[i]-x[i];
}
else
{
greater+=x[i]-y[i];
}
}
if(smaller!=greater)cout<<-1;
else cout<<greater;
return 0;
}