Advertisement
fooker

sum of chocolates

Mar 26th, 2023 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. int main(){
  5.     ll n,l;
  6.     cin>>n>>l;
  7.     ll a[n+1];
  8.     for (ll i=1; i<=n; i++){
  9.         cin>>a[i];
  10.     }
  11.     ll i=1;
  12.     ll sum=0;
  13.     for (ll j=1; j<=n; j++){
  14.         sum+=a[j];
  15.         while (sum>l){
  16.             sum-=a[i++];
  17.         }
  18.         if (sum==l){
  19.             for (ll k=i; k<=j; k++){
  20.                 cout<<a[k]<<" ";
  21.             }
  22.             return 0;
  23.         }
  24.     }
  25.     cout<<-1;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement