Answer: $A$ wins if $n=k^2+\{0,1,...,k-1\}$ and $B$ wins if $n=k^2+\{k,k+1,...,2k\}$
Claim: $A$ wins if $n$ is a perfect square.
Proof: Let $n=k^2$
First $A$ writes $1$. After that, if $B$ takes $m$ in the $2i.$ move, then $A$ takes $2i+1-m$ in $(2i+1).$ move. By this algorithm, the bag will have $k^2-
(1+3+...+(2i+1))$ balls after $(2i+1).$ move. After $(2k-1).$ move, there won't exist any ball in the bag and the last ball will be taken by $A$.
Claim: $A$ wins if $n=k^2+t$ where $0\leq t\leq k-1$.
Proof: $A$ does nearly the same strategy. $A$ can guarantee that the sum of the taken balls in moves $(2i)$ and $(2i+1)$ equals to $2i+2$. $A$ changes the sum from $2i+1$ to $2i+2$ for $t$ move pairs and wins.
Claim: $B$ wins if $n=k^2+k$
Proof: If $A$ takes $m$ balls in $(2i-1).$ move, then $B$ takes $2i-m$ balls in $(2i).$ move. So after $2k$ moves, there won't exist any ball in the bag because $k^2+k=k(k+1)=(2+4+...+2k)$
Claim: $B$ wins if $n=k^2+t$ where $k\leq t\leq 2k$.
Proof: Again $B$ does nearly the same strategy. $B$ can guarantee that the sum of the taken balls in moves $(2i-1)$ and $(2i)$ equals to $2i+1$. $B$ changes $t$ move pairs from $2i$ to $2i+1$ and wins.