fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e6 + 5;
  5.  
  6. bool is_prime[N];
  7. int a[N];
  8. long long prefix[N];
  9.  
  10. void sieve(int n) {
  11. fill(is_prime, is_prime + n + 1, true);
  12. is_prime[0] = is_prime[1] = false;
  13. for (int i = 2; i * i <= n; ++i) {
  14. if (is_prime[i]) {
  15. for (int j = i * i; j <= n; j += i)
  16. is_prime[j] = false;
  17. }
  18. }
  19. }
  20.  
  21. int main() {
  22. ios::sync_with_stdio(false);
  23. cin.tie(nullptr);
  24.  
  25. freopen("EASYSEQ.INP", "r", stdin);
  26. freopen("EASYSEQ.OUT", "w", stdout);
  27.  
  28. int n;
  29. cin >> n;
  30. for (int i = 1; i <= n; ++i) cin >> a[i];
  31.  
  32. // Tính mảng tổng cộng dồn
  33. prefix[0] = 0;
  34. for (int i = 1; i <= n; ++i)
  35. prefix[i] = prefix[i - 1] + a[i];
  36.  
  37. // Đánh dấu các chỉ số nguyên tố
  38. sieve(n);
  39.  
  40. long long res = LLONG_MIN;
  41.  
  42. for (int L = 2; L <= n; ++L) {
  43. if (!is_prime[L]) continue;
  44. for (int R = L; R <= n; ++R) {
  45. if (!is_prime[R]) continue;
  46. long long total = prefix[R] - prefix[L - 1];
  47. res = max(res, total);
  48. }
  49. }
  50.  
  51. cout << res << '\n';
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 7860KB
stdin
Standard input is empty
stdout
Standard output is empty