AcWing 4262. 空调
原题链接
简单
作者:
春江花月夜ovo
,
2024-03-02 08:33:22
,
所有人可见
,
阅读 22
简单差分
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
ll d[N];
ll calc[N];
int n;
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i ++) cin >> d[i];
for (int i = 1; i <= n; i ++)
{
int x;
cin >> x;
d[i] = d[i] - x;
calc[i] = d[i] - d[i - 1];
}
ll x = 0, y = 0;
for (int i = 1; i <= n; i ++)
{
if (calc[i] >= 0) x += calc[i];
else y -= calc[i];
}
ll ans = abs(x - y) + min(x, y);
cout << ans << "\n";
return 0;
}