#include <bits/stdc++.h>
using namespace std;
long int bezw(long int j)
{
if(j<0) j*=-1;
return j;
}
int main()
{
int N;
cin >> N;
long int A[N+1],B[N+1],roznica[N+1];
for(int i=0;i<N;i++)
{
cin >> A[i] >> B[i];
roznica[i]=A[i]-B[i];
}
long long int wynik=0;
for(int i=0;i<N;i++)
{
if(roznica[i]<0)
{
for(;roznica[i]<0;)
{
long int po_prawej=-1 , po_lewej=-1;
for(int l=i-1;l>=0;l--)
{
if(roznica[l]>0){po_lewej=l; break;}
}
for(int l=i+1;l<N;l++)
{
if(roznica[l]>0){po_prawej=l; break;}
}
if(po_lewej-i<i-po_prawej && po_lewej!=-1)
{
if(roznica[i] >= roznica[po_lewej])
{
wynik+=bezw(roznica[po_lewej])*(po_lewej-i);
roznica[i]+=roznica[po_lewej];
roznica[po_lewej]=0;
}
else
{
wynik+=bezw(roznica[i]*(po_lewej-i));
roznica[po_lewej]+=roznica[i];
roznica[i]=0;
}
}
else
{
if(roznica[i] >= roznica[po_prawej])
{
wynik+=bezw(roznica[po_prawej])*(i-po_prawej);
roznica[i]+=roznica[po_prawej];
roznica[po_prawej]=0;
}
else
{
wynik+=bezw(roznica[i]*(po_prawej-i));
roznica[po_prawej]+=roznica[i];
roznica[i]=0;
}
}
}
}
}
cout << wynik;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
4 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |