#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int>odp;
vector<pair<int,int>>licz;
int con1=0,con2=0;
for(int i=0;i<n;i++)
{
int a,b;
cin>>a>>b;
licz.push_back({a,b});
con1+=a;
con2+=b;
}
if(con1>=con2)
{
int p=1;
long long w=0;
while(licz[0].first<licz[0].second)
{
if(licz[p].first<=licz[p].second)
p++;
else
{
licz[p].first--;
licz[0].first++;
w+=p;
}
}
p=n-2;
/*for(int j=0;j<n;j++)
cout<<licz[j].first<<" "<<licz[j].second<<endl;
cout<<"xx--xx--xx"<<endl;*/
while(licz[n-1].first<licz[n-1].second)
{
if(licz[p].first<=licz[p].second)
p--;
else
{
licz[p].first--;
licz[n-1].first++;
w+=n-1-p;
}
}
/*for(int j=0;j<n;j++)
cout<<licz[j].first<<" "<<licz[j].second<<endl;
cout<<"xx--xx--xx"<<endl;*/
for(int i=1;i<n-1;i++)
{
int x,z;
x=i-1;
z=i+1;
while(licz[i].first<licz[i].second)
{
if(x!=0)
{
int o,u;
o=licz[x].first-licz[x].second;
u=licz[i].second-licz[i].first;
o=min(o,u);
w+=o*(i-x);
licz[i].first+=o;
licz[x].first-=o;
x--;
}
if(licz[i].first>=licz[i].second)
break;
if(z!=n-1)
{
int o,u;
o=licz[z].first;
u=licz[i].second-licz[i].first;
o=min(o,u);
w+=o*(z-i);
licz[i].first+=o;
licz[z].first-=o;
z++;
}
}
}
cout<<w<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
300 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
308 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
300 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
308 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
300 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
308 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
300 KB |
Output is correct |
3 |
Execution timed out |
1086 ms |
308 KB |
Time limit exceeded |