Problem p03168 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
// #include <stdafx.h>
// #include "targetver.h"
#include <stdio.h>
// #include <tchar.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif>

#define LL long long
#define MOD 998244353
#define INF 1000000000
#define LINF 1000000000000000000

using namespace std;

const int N = 2e3 + 10;
double a[N], sum;
double b[N];
double dp[N][N];
vector<int> vec;
int n;

int main() {
  cout << fixed << setprecision(10);

  double p1, p2;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    cin >> a[i];
  // scanf("%f", &a[i]);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      dp[i][j] = 1;

  dp[0][0] = 1.0 - a[0];
  dp[0][1] = a[0];

  for (int i = 1; i < n; i++)
    for (int j = 0; j <= i + 1; j++) {
      p1 = dp[i - 1][j] != 1 ? dp[i - 1][j] * (1.0 - a[i]) : 0;
      p2 = j > 0 ? dp[i - 1][j - 1] * a[i] : 0;
      dp[i][j] = p1 + p2;
    }

  for (int i = n / 2 + 1; i <= n; i++)
    sum += dp[n - 1][i];

  cout << sum;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
// #include <stdafx.h>
// #include "targetver.h"
#include <stdio.h>
// #include <tchar.h>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif>

#define LL long long
#define MOD 998244353
#define INF 1000000000
#define LINF 1000000000000000000

using namespace std;

const int N = 4e3 + 10;
double a[N], sum;
double b[N];
double dp[N][N];
vector<int> vec;
int n;

int main() {
  cout << fixed << setprecision(10);

  double p1, p2;
  scanf("%d", &n);

  for (int i = 0; i < n; i++)
    cin >> a[i];
  // scanf("%f", &a[i]);

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      dp[i][j] = 1;

  dp[0][0] = 1.0 - a[0];
  dp[0][1] = a[0];

  for (int i = 1; i < n; i++)
    for (int j = 0; j <= i + 1; j++) {
      p1 = dp[i - 1][j] != 1 ? dp[i - 1][j] * (1.0 - a[i]) : 0;
      p2 = j > 0 ? dp[i - 1][j - 1] * a[i] : 0;
      dp[i][j] = p1 + p2;
    }

  for (int i = n / 2 + 1; i <= n; i++)
    sum += dp[n - 1][i];

  cout << sum;
}