What it looks like you're not paying sufficient attention to is that selection happens at the level of the gene, not the level of the individual.
It is absolutely not beneficial to the individual to put energy out to try to have sex, put energy into offspring, or to take risks for others. However all of those behaviors have tended to be beneficial for the genes that the individual has because the others who are helped are likely to have the genes producing that behavior. Therefore all of those behaviors have evolved notwithstanding the fact that they are inconvenient for individuals with those genes.
The truth is that the truth you allude to is only one of many truths, and I consider it my duty as a mansplainer to point out that there are other truths you failed to incorporate into your paragraph in such a way that makes me feel as if I have a bigger and more complete collection of truths because then I won't feel so bad that my pants make me feel ugly.
It is absolutely not beneficial to the individual to put energy out to try to have sex, put energy into offspring, or to take risks for others. However all of those behaviors have tended to be beneficial for the genes that the individual has because the others who are helped are likely to have the genes producing that behavior. Therefore all of those behaviors have evolved notwithstanding the fact that they are inconvenient for individuals with those genes.