Vault
There are $2M + 1$ kinds of weights, whose mass are $-M, -M + 1, \ldots, M - 1, M$. However, you do not have an infinite number of weights. For each mass $\ell$, there are only $A_{\ell}$ such weights. You want to choose some weights such that their total mass is exactly $L$. In this case, you want to maximize the number of weights you choose.
$1 \leq M \leq 300$
$0 \leq A_{\ell} \leq 10^{12}$
$-10^{18} \leq L \leq 10^{18}$
Tutorial
Intuition tells us that if we first determine the set of weights $\mathcal S$ whose total mass is close to $L$, then we can adjust $\mathcal S$ within an acceptable time complexity.
First, add all the weights to $\mathcal S$. Then we will continuously remove the maximum weight from $\mathcal S$ until the total mass in it is less than or equal to $L$. Thus the total mass well be within $\left[L - M + 1, L\right]$. However, if the total mass of all weights is less than $L$, we will remove the minimum weight from $\mathcal S$. Since both cases are symmetric, we will focus only on the first case.
Consider the adjustment method. If the current mass of $\mathcal S$ is less than $L$, then we will inevitably either add a positive weight or remove a negative weight later. we might as well do this in advance. We will find that the total mass will still be within $\left[L - M + 1, L + M - 1\right]$.
We will observe the following fact: if the current mass of $\mathcal S$ is $W_0$, it is irrational to add and remove weights to obtain another set $\mathcal S'$ with the same total mass $W_0$. Thus, we only reach the same total mass at most once. This means we have to add or remove weights at most $O\left(M\right)$ times, implying that if the order is not taken into consideration, the total mass of $\mathcal S$ is within $\left[L - M^2, L + M^2\right]$.
Therefore, we can perform DP in $\tilde O\left(M^3\right)$.