1 solutions

  • 0
    @ 2026-11-11 19:18:24

    C++ :

    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
    
        int s, n;
        cin >> s >> n;
    
        vector<int> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
    
        int left = 0;
        long long sum = 0;
        int ans = INT_MAX;
    
        for (int right = 0; right < n; right++) {
            sum += a[right];
    
            while (sum >= s) {
                ans = min(ans, right - left + 1);
                sum -= a[left];
                left++;
            }
        }
    
        if (ans == INT_MAX) {
            cout << 0 << endl;
        } else {
            cout << ans << endl;
        }
    
        return 0;
    }
    
    • 1

    Information

    ID
    42
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    10
    Tags
    # Submissions
    1
    Accepted
    1
    Uploaded By