N_002_AAAA - mảng con L max

Xem dạng PDF

Gửi bài giải


Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M
Input: stdin
Output: stdout

Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Go, Java, JavaScript, Kotlin, Pascal, Perl, PHP, PyPy, Python, Ruby, Rust, Scratch, Swift

Trong trường hợp đề bài hiển thị không chính xác, bạn có thể tải đề bài tại đây: Đề bài


Bình luận

Please read the guidelines before commenting.



  • 0
    nhatminecraftthanhgu  đã bình luận lúc 9, Tháng 6, 2026, 9:37

    include <bits/stdc++.h>

    using namespace std;

    int main() { ios::syncwithstdio(false); cin.tie(nullptr);

    int n, L;
    cin >> n >> L;
    
    vector&lt;long long> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    
    long long sum = 0;
    for (int i = 0; i < L; i++) {
        sum += a[i];
    }
    
    long long ans = sum;
    
    for (int i = L; i < n; i++) {
        sum += a[i];
        sum -= a[i - L];
        ans = max(ans, sum);
    }
    
    cout << ans;
    
    return 0;
    

    }


  • 0
    manh1122  đã bình luận lúc 6, Tháng 3, 2026, 15:10
    #include <bits/stdc++.h>
    using namespace std;
    
    #define ll long long
    #define ull unsigned long long
    #define ld long double
    
    #define pb push_back
    #define pf push_front
    #define fi first
    #define se second
    
    #define all(x) (x).begin(), (x).end()
    #define rall(x) (x).rbegin(), (x).rend()
    
    #define faster() ios::sync_with_stdio(false); cin.tie(nullptr);
    #define endl '\n'
    
    #define yes cout << "YES\n"
    #define no  cout << "NO\n"
    
    #define pii pair<int,int>
    #define pll pair<ll,ll>
    #define vi vector<int>
    #define vl vector<ll>
    const ll maxn = 1e6;
    ll a[maxn+10],n,L,pre[maxn+10];
    int main() {
        faster();
        cin>>n>>L;
        for(int i=1;i<=n;i++) {
            cin>>a[i];
            pre[i]=pre[i-1]+a[i];
        }
        ll ans=-1e18;
        for(int i=1;i<=n-L+1;i++) {
            ll s=pre[i+L-1]-pre[i-1];
            ans = max(ans,s);
        }
        cout<&lt;ans;
    }
    

  • 0
    kietjumper  đã bình luận lúc 15, Tháng 1, 2026, 15:11

    output đề sai ạ, phải là 10