この問題は簡単な場合分けを問います。
各おぼんには、少なくともお団子の数を つだけ増やす操作しかありません。そして という制約から以下のように操作をすることが適切です。
したがってこの計算を繰り返し構文を用いることにより、 で答えることができます。以下は解答例(それぞれC++,Python)です。
xxxxxxxxxx
//[0,n)
//[a,b)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<ll,ll>;
using pq = priority_queue<P,vector<P>,greater<P>>;
ll inf = 8e18;
int iinf = (int)1e9;
int mod9 = 998244353;
int mod1 = 1000000007;
struct Edge { int to; int cost; int from; };
bool compe(const Edge &e,const Edge &e2){ return e.cost < e2.cost; }
using Graph = vector<Edge>;
using SGraph = vector<set<ll>>;
template <typename T>
int siz(T& a){return (int)a.size();}
int main(void){
int n,l,r; cin >> n >> l >> r;
int ans = 0;
rep(_,n){
int a; cin >> a;
if(a <= l) ans += l-a;
else if(a <= r) ans += r-a;
}
cout << ans;
}
xxxxxxxxxx
n,l,r = map(int,input().split())
A = list(map(int,input().split()))
ans = 0
for a in A:
if a <= l:
ans += l-a
elif a <= r:
ans += r-a
print(ans)