Блог пользователя dipesh_ait

Автор dipesh_ait, история, 20 месяцев назад, По-английски
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int32_t main()
{
    ll n,q;
    cin>>n>>q;
    ll a[n];
    for(ll i=0;i<n;i++)
    {
          cin>>a[i];
    }  
    sort(a,a+n);   
   vector<pair<ll,ll>> v_p;
   for(ll i=0;i<q;i++)
   {
         ll x,y;
         cin>>x>>y;
         v_p.push_back({x,y});
   }
   for(auto h:v_p)
   {
        ll x_temp = h.first;
        ll y_temp = h.second;
        ll x_n = n;
        ll y_n = x_temp;
         vector<ll> v;
         ll total = 0;
        while(x_temp--)
        {
           v.push_back(a[x_n-1]);
            x_n--;
        }
        while(y_temp--)
        {
             total+=v[y_n-1];
             y_n--;
        }
        cout<<total<<endl;
   }
	return 0;
}
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
20 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

181184901

In the above code i have sorted the price list and then i have calculated the prefix sum so we don't have to calculate the required sum again and again and then i took the required part of the array and provided it as output for each query.