#include #include // cout, endl, cin #include // string, to_string, stoi #include // vector #include // min, max, swap, sort, reverse, lower_bound, upper_bound #include // pair, make_pair #include // tuple, make_tuple #include // int64_t, int*_t #include // printf #include // map #include // queue, priority_queue #include // set #include // stack #include // deque #include // unordered_map #include // unordered_set #include // bitset #include // isupper, islower, isdigit, toupper, tolower #include #include #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++) typedef long long ll; typedef unsigned long long ull; const ll inf=1e18; using graph = vector > ; using P= pair; using vi=vector; using vvi=vector; using vll=vector; using vvll=vector; using vp=vector

; using vpp=vector; //string T="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //string S="abcdefghijklmnopqrstuvwxyz"; //g++ main.cpp -std=c++14 -I . //cout <> n >> q; vector a(n);rep(i,n)cin >> a[i]; int x=sqrt(n)+1; int si=n/x+2; vvi g(si); vvll sum(si,vll(x+1)); rep(i,n)g[i/x].push_back(a[i]); rep(i,si)sort(g[i].begin(),g[i].end()); rep(i,si)rep(j,g[i].size())sum[i][j+1]=sum[i][j]+g[i][j]; vll anss; auto f2=[&](int i,ll k){ int index=upper_bound(g[i].begin(),g[i].end(),k)-g[i].begin(); ll res=sum[i][index]+k*(g[i].size()-index); return res; }; auto f=[&](int l,int r,ll k){ ll res=0; if(l/x==r/x || r/x-l/x==1){ repi(i,l,r+1)res+=min(k,a[i]); } else { int now=l; while(now/x==l/x){ res+=min(k,a[now]); now++; } now=(r/x)*x; while(now<=r){ res+=min(k,a[now]); now++; } now=l/x+1; while(now> l >> r >> k; l--;r--; ll ans=f(l,r,k); anss.push_back(ans); } for(auto u:anss)cout << u << endl; }