Advertisement
Josif_tepe

Untitled

Apr 10th, 2022
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7.  
  8. int main() {
  9.     int t;
  10.     cin >> t;
  11.     while(t--) {
  12.         int n;
  13.         cin >> n;
  14.         vector<int> a(n), b(n);
  15.         for(int i = 0; i < n; i++) {
  16.             cin >> a[i];
  17.         }
  18.         for(int i = 0; i < n; i++) {
  19.             cin >> b[i];
  20.         }
  21.         ll sum = 0;
  22.         for(int i = 0; i + 1 < n; i++) {
  23.             ll m1 = abs(a[i] - a[i + 1]) + abs(b[i] - b[i + 1]);
  24.             ll m2  = abs(a[i + 1] - b[i]) + abs(b[i + 1] - a[i]);
  25.             sum += min(m1, m2);
  26.         }
  27.         cout << sum << endl;
  28.     }
  29.    
  30.     return 0;
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement