Summing the Product of Array-member combinations
Your performance problem here is related to a number of things, but the most significant is the algorithm you use.

Consider the following math:

ax + bx + cx + dx ... == x(a + b + c + d + ...)


Using that logic, the basic math for one array can be simplified.... for example, you can take the second array, sum up the values, and call that sum x.

Now you just need to take all the values in the first array, and multiply them by the sum of the second array, instead of summing the individual products.

As it happens, you can take the same logic, and apply it the other way as well. Bottom line, is that the solution is simply the product of the sums of the arrays:

#include &lt;iostream&gt;

int sum(int count) {
    int tmp, sum = 0;
    for (int i = 0; i &lt; count; i++) {
        std::cin &gt;&gt; tmp;
        sum += tmp;
    }
    return sum;
}

int main() {
    int N, M;
    std::cin &gt;&gt; N &gt;&gt; M;
    int suma = sum(N);
    int sumb = sum(M);
    int result = suma * sumb;
    std::cout &lt;&lt; result &lt;&lt; "\n";
    return 0;
}


Note, that:


the above code no longer uses "using namespace std;"
The sum method no loger uses static variables. Those are a bad idea especially when it comes to re-entrant code.
Output a newline after the last line.